我用recycleview连接了tablerow,我想添加行,但有一个条件

olqngx59  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(153)

我在做一个项目,增加了产品和它的价格根据其标识符
此应用程序的图片
我花了很多时间徒劳地寻找解决办法你能帮我吗?这是主要课程:

public class MainActivity extends AppCompatActivity {
    public Button btun, Btuun;
    private EditText code;
    private android.widget.TableLayout tl;
    RecyclerView recycler_view;
    paymentAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        // Buy Button Coding :
        btun = (Button) findViewById(R.id.buyBtn);
        btun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Casher.class);
                startActivity(intent);
            }
        });
        //log btun
        Btuun = (Button) findViewById(R.id.button2);
        Btuun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, log_page.class);
                startActivity(intent);
            }
        });

        recycler_view = findViewById(R.id.recycler_view);
        setReycelerView();

        code = findViewById(R.id.barcode);
        //code.addTextChangedListener(enter);
    }

    private void setReycelerView() {
        recycler_view.setHasFixedSize(true);
        recycler_view.setLayoutManager(new LinearLayoutManager(this));
       adapter = new paymentAdapter(this,getList());
        recycler_view.setAdapter(adapter);
    }
//here is the code for add : 

   private List<payment> getList() {
        List<payment> paymentList = new ArrayList<>();
            paymentList.add( new payment("Milk", "18"));
            paymentList.add(new payment("Shampo", "28"));
            paymentList.add(new payment("Cheese", "19"));
        return paymentList;
    }
}

我做了这样的事,但没用。。为了更清楚,我想这样做的想法

private List<payment> getList() {
        List<payment> paymentList = new ArrayList<>();
if(code.getText().toString().trim().equals("659245631"))
{paymentList.add( new payment("Milk", "18"));}
else if  (code.getText().toString().trim().equals("1592456523"){
            paymentList.add(new payment("Shampo", "28"));}
        return paymentList;
 }

如果你需要看的话,这是另一节课

public class payment {
    String name;
    String price;

    public payment(String name, String price) {
            this.name = name;
            this.price =price;
        }

    public String getName() {
        return name;
    }

    public String getPrice() {
        return price;
    }
}

还有这个

public class paymentAdapter extends RecyclerView.Adapter<paymentAdapter.ViewHolder> {
  Context context;
    List<payment> payment_list;

    public paymentAdapter(MainActivity context, List<payment> payment_list) {
        this.context = context;
        this.payment_list = payment_list; }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tablelayot,parent,false);
        return new ViewHolder(view); }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (payment_list !=null && payment_list.size() > 0){
            payment model = payment_list.get(position);
            holder.pname_tv.setText(model.getName());
            holder.price_tv.setText(model.getPrice());
        }
    }
    @Override
    public int getItemCount() {
        return payment_list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView pname_tv,price_tv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            pname_tv= itemView.findViewById(R.id.pname_tv);
            price_tv= itemView.findViewById(R.id.price_tv);
        }}}

请帮助我:(

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题