且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

后ListView项点击将图片添加到列表视图

更新时间:2023-12-06 16:53:52

试试这个得到你想要的东西。

在公共区域中添加这个变量

 公共SparseBooleanArray检查=新SparseBooleanArray();

然后 onItemClick

  list.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,
                    长ID){
                布尔STAT = checked.get(位置,FALSE);
                checked.put(位置,STAT!);
                adapter.setChecked();
                的System.out.println(位置是:+位置);
            }
        });

和适配器类

如果在activiy电源适配器没有子类公共区域添加变量

 私人SparseBooleanArray检查=新SparseBooleanArray();

和此方法添加到类

 公共无效setChecked(SparseBooleanArray CH){
    检查= CH;
   notifyDataSetChanged();
}

或者,如果它的子类中使用我们的 cheked 变量,我们定义了它

然后 getView

 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
    查看VI = convertView;
    如果(convertView == NULL)
        VI = inflater.inflate(R.layout.method_item,NULL);    TextView的文本=(TextView的)vi.findViewById(R.id.textView1);
    text.setText(preparationSteps \\ [位置\\]);    ImageView的图像=(ImageView的)vi.findViewById(R.id.imageView2);
    如果(checked.get(位置,FALSE)){
          image.setImageResource(0);
    }
    其他{
            image.setImageResource(R.drawable.select_right);
    }
    返回VI;
}

请让我知道如果这个帮你。

I am developing an app which has a listView having 1 textView which displays the content and one imageView. What i want is, when i click the listView i want to set the imageView with the tick mark image which i have. Its working fine. Once i click the listView, the tick mark image is loaded on that listview item on which i clicked.

The problem arises when i scroll. When i scroll, i could see some other listview item down below has a tick mark loaded. not sure how the image was set on that position.

I have read somewhere that when the listView is scrolled, the view is refreshed. Is that causing the problem?

Can anyone help how can iresolve this? I want the image to be shown(loaded) on the listView item on which i clicked and now other listView item.

Below is the xml stating listView items

method_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white"
        android:text="This is a check box button which reacts upon the check that user clicks. I am testing it with the big string and checking how it looks" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="34dp" />

</RelativeLayout>

Below is the part of class snippet:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.method_list_main);

    final ListView list = (ListView)findViewById(R.id.listView1);

    adapter=new MethodLazyAdapter(this,ARRAY.preparationSteps,ARRAY.timeToPrepare,ARRAY.arrowValue,noOfSteps,list);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
             RelativeLayout ll = (RelativeLayout) view;
              ImageView image = (ImageView)ll.findViewById(R.id.imageView2);

              if(tick[position]){
                  tick[position] = false;
                    image.setImageResource(0);                      
              }
              else{
                  tick[position] = true;
                      image.setImageResource(R.drawable.select_right);  
              }

            System.out.println("Position is: "+position);


        }
    });
}

Initially all tick[i] value is set to false.

Below is the getView function of adapter class

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.method_item, null);

    TextView text = (TextView)vi.findViewById(R.id.textView1);
    text.setText(preparationSteps\[position\]);
    return vi;
}

Try this to get what you want

in public area add this variable

public SparseBooleanArray checked = new SparseBooleanArray();

then in onItemClick

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                boolean stat = checked.get(position, false);
                checked.put(position, !stat);
                adapter.setChecked();
                System.out.println("Position is: "+position);


            }
        });

and in Adapter Class

If adapter not sub class in activiy add variable in public area

private SparseBooleanArray checked = new SparseBooleanArray();

and add this method to class

public void setChecked(SparseBooleanArray ch){
    checked = ch;
   notifyDataSetChanged();
}

Or if it sub class use our cheked variable we defined it up

then in getView method

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.method_item, null);

    TextView text = (TextView)vi.findViewById(R.id.textView1);
    text.setText(preparationSteps\[position\]);

    ImageView image = (ImageView)vi.findViewById(R.id.imageView2);
    if(checked.get(position, false)){
          image.setImageResource(0);                      
    }
    else{
            image.setImageResource(R.drawable.select_right);  
    }
    return vi;
}

Please let me know if this help you.