简体   繁体   English

Android中ListView的奇怪行为

[英]Strange behavior of listview in android

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater mInflater = (LayoutInflater) mContxt
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = mInflater.inflate(R.layout.row_link_product_distributor,
                null);
    }
    holder = new ViewHolder();
    holder.linktoprodisarraylist = listProduct.get(position);

    holder.imgProduct = (ImageView) view.findViewById(R.id.productimg);
    holder.txtProductName = (TextView) view
            .findViewById(R.id.txtproductname);
    holder.txtDistributorNametxt = (TextView) view
            .findViewById(R.id.txtdistributornametxt);
    holder.brand_name = (TextView) view.findViewById(R.id.barnd_nametxt);
    holder.manf_name = (TextView) view.findViewById(R.id.manf_nametxt);

    holder.txtDistributorNametxt.setTag(position);
    view.setTag(holder);
    holder.imgProduct.setImageResource(holder.linktoprodisarraylist
            .getProductImgID());
    holder.txtProductName.setText(holder.linktoprodisarraylist
            .getProductName());
    holder.txtDistributorNametxt.setText(holder.linktoprodisarraylist
            .getFavDistName());
    holder.brand_name.setText(holder.linktoprodisarraylist.getBrandName());
    holder.manf_name.setText(holder.linktoprodisarraylist.getManName());
    holder.txtDistributorNametxt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            final String productId = arraylist.get(position).getProductID();
            TextView disname = (TextView) v;
            String Disname_value = disname.getText().toString();
            if (Disname_value.equalsIgnoreCase("Select Distributer")) {
                alreadyassigndisid = "0";
            } else {
                alreadyassigndisid = getDistributorid(Disname_value);
            }

            final Dialog dialog = new Dialog(mContxt);
            dialog.setContentView(R.layout.row_distributor_name);
            dialog.setTitle("Select Distributer");
            dailogListView = (ListView) dialog.findViewById(R.id.disnamelv);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    mContxt, android.R.layout.simple_list_item_1,
                    listDistributorname);
            dailogListView.setAdapter(adapter);
            dailogListView
                    .setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {
                            TextView disname = (TextView) v;
                            disname.setText(listDistributorname
                                    .get((Integer) position));
                            int dsiId = Distributor_idlist.get(position)
                                    .getDisID();
                            insertFavProduct_DisIntoDb(userId,
                                    Integer.valueOf(alreadyassigndisid),
                                    dsiId, String.valueOf(productId),
                                    position);
                            dialog.dismiss();
                        }
                    });
            dialog.show();
        }
    });
    return view;
} 

I am using a listview in wich there is a textview. 我正在使用一个listview,其中有一个textview。
A dialog is opened when the textView is clicked and the user can set a new value to the textview, but when ever I scroll the changes get reverted, and the selected distributor name is not displayed. 单击textView时会打开一个对话框,用户可以为textview设置一个新值,但是每当我滚动更改时,这些更改都会还原,并且不会显示所选的分发服务器名称。
Can anybody tell me what is wrong in this code ? 谁能告诉我这段代码有什么问题吗?

Like said in the comments, your list is recycled when you scroll. 就像评论中所说的那样,滚动时会回收您的列表。 You need to udpdate the list sotred in your adapter when the user enter the value. 当用户输入值时,您需要udpdate适配器中存储的列表。

Firstly, you should know how listview and adapter work. 首先,您应该知道listview和适配器如何工作。 I recommended this post . 我推荐了这篇文章 The previous comment/answer are correct. 先前的评论/答案是正确的。 You have to store the value. 您必须存储值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM