单击列表视图项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单击列表视图项相关的知识,希望对你有一定的参考价值。

我的应用程序中有一个ListViewListView中的每个项目都包含一个按钮和项目计数。当点击每个项目中的按钮时,我想显示与EditText的对话框以输入相应项目的新计数并更新具有我从对话EditText字段获得的值的项目。

我创建了对话框以在按钮单击时输入新计数,但无法更新值。 我的适配器

public class MyAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return planList.size();
    }

    @Override
    public Object getItem(int position) {
        return planList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final SalesModel db_data = planList.get(position);

        if (convertView == null) {
            convertView = View.inflate(getApplicationContext(), R.layout.return_confirm, null);
        }

        TextView name = (TextView) convertView.findViewById(R.id.name);
        final TextView stock = (TextView) convertView.findViewById(R.id.stock);
        TextView amount = (TextView) convertView.findViewById(R.id.amount);
        ImageView minus = (ImageView) convertView.findViewById(R.id.minus);

        Double count = Double.parseDouble(db_data.getStock());
        Double price = Double.parseDouble(db_data.getSprice());
        Double s_price = count*price;
        String set_amount = s_price.toString();

        name.setText(db_data.getName());
        stock.setText(db_data.getStock());
        amount.setText(set_amount);

        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                stock_return = ShowDialogue();

                Double n_stock = Double.parseDouble(db_data.getStock())-Double.parseDouble(stock_return);

                stock.setText(n_stock.toString());
            }
        });

        return convertView;
    }
}

功能显示对话

public String ShowDialogue(){

    String stk_val;
    stock_return = "0.0";
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
    final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
    Button ok = (Button)dialogLayout.findViewById(R.id.later );
    Button close = (Button)dialogLayout.findViewById(R.id.close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            dialog.dismiss();
        }
    });

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            if(stk.getText().toString().trim().isEmpty()){
                stk.setError("Enter quantity");
            }
            else {
                stock_return = stk.getText().toString().trim();
            }
        }
    });

    dialog.setView(dialogLayout,0,0,0,0);
    dialog.show();

    return stock_return;

}
答案

你做错了什么。在输入ok按钮之前,对话框无法返回。

试试这样:

minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                stock_return = ShowDialogue(this, position);

            }
        });

像这样改变对话框。

public void ShowDialogue(MyAdapter myAdapter, int position){

    String stk_val;
    stock_return = "0.0";
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
    View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
    final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
    Button ok = (Button)dialogLayout.findViewById(R.id.later );
    Button close = (Button)dialogLayout.findViewById(R.id.close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            dialog.dismiss();
        }
    });

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            if(stk.getText().toString().trim().isEmpty()){
                stk.setError("Enter quantity");
            }
            else {

                planList.get(position).setStock(stk.getText().toString().trim());
                dialog.dismiss();
                myAdapter.notifyDataSetChanged();
            }
        }
    });

    dialog.setView(dialogLayout,0,0,0,0);
    dialog.show();

}

以上是关于单击列表视图项的主要内容,如果未能解决你的问题,请参考以下文章

OnItemClick 如何从列表视图中获取单击项目的文本值

Android片段中的问题:仍然单击上一个片段

未创建 Android listview 内容视图

用另一个列表替换主活动中的列表并从视图中删除旧列表

一旦单击带有 in 片段的回收器列表项,如何将片段意向活动,以及如何获取回收器项目值?

如何从使用视图寻呼机创建的选项卡片段转移到另一个片段