开发者

Selected Item issue while scrolling listview

i am using following code to display list of tags now when i select any item a tick mark is displayed before that selected item.

and i move to next activity. now problem is when i come to this activity again using back button and scroll on that listview tick marks are displayed before many tags instead of single one i selected.

any one guide me what mistake am i doing here?

ImageView selectedTickImageView;
getview{

    if (convertView == null) {
                        convertView = mInflater.inflate(R.layout.row_tags_archives, null);
                        holder = new ViewHolder();
                        holder.tickImageView= (ImageView)convertView.findViewById(R.id.tick_image_view);
                        holder.titletextView = ( TextView )convertView.findViewById(R.id.tv_tags_archives);
                        holder.tickImageView.setTag(position);

                        convertView.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View paramView) {
                                    if(selectedTickImageView != null){
                                        selectedTickImageView.setVisibility(View.GONE);
                                    }

                                    ImageView tickImageView = (ImageView)paramView.findViewWithTag(position);
                                    tickImageView.setVisibility(View.VISIBLE);

                                    selectedTickImageView =ti开发者_Python百科ckImageView;
                                    S=tags[position];
                                    S=S.replace(" ", "-");
                                    S=S.toLowerCase();
                                    Intent intent = new Intent(HPressTagsorUpdatesActivity.this ,HPressSummariesActivity.class);
                                    startActivity(intent);
                            }});

                        convertView.setTag(holder);

                      } else {
                        holder = (ViewHolder) convertView.getTag();
                      }


Even I had a similar problem, I had a list of contacts when I selected one others used to get set automatically..

To solve this problem, I added a ArrayList and added the position of the item which was clicked. And while drawing in getView of my adapter, I used to check if it is present in my arraylist.


a class with tag + selected fields would be more efficient way to solve this problem but i have used a global selected array of boolean to keep track of selected item.

if (convertView == null) {
                    //private Context _context = ;
                    LayoutInflater mInflater = LayoutInflater.from(HPressTagsorUpdatesActivity.this);
                    convertView = mInflater.inflate(R.layout.row_tags_archives, null);
                    holder = new ViewHolder();
                    holder.tickImageView= (ImageView)convertView.findViewById(R.id.tick_image_view);
                    holder.tickImageView.setTag(position);
                    holder.titletextView = ( TextView )convertView.findViewById(R.id.tv_tags_archives);




                    convertView.setTag(holder);

                  } else {
                    holder = (ViewHolder) convertView.getTag();
                  }


                if(selected[position] == true)
                {
                    holder.tickImageView.setVisibility(View.VISIBLE);
                }else
                {
                    holder.tickImageView.setVisibility(View.GONE);

                }


                convertView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View paramView) {

                        ImageView tickImageView = (ImageView)paramView.findViewWithTag(position);
                        tickImageView.setVisibility(View.VISIBLE);
                            selected[position] = true;

                            S=tags[position];
                            S=S.replace(" ", "-");
                            S=S.toLowerCase();
                            Intent intent = new Intent(HPressTagsorUpdatesActivity.this ,HPressSummariesActivity.class);
                            startActivity(intent);
                    }});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜