Android更改List的imageview图像

时间:2013-03-09 08:51:00

标签: android android-layout listview

我想在点击列表时更改图像。单击列表中的位置将更改图像。另一个列表位置的ImageView图像将是相同的,但我没有成功。

当我选择一个列表项图像时,视图显示右图标图像,当我取消选择此位置时,ImageView会显示箭头图像。

   public View getView(final int position, View convertView,
        final ViewGroup parent) {
    // TODO Auto-generated method stub

    View row = convertView;
    if (row == null) {

        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(mLayoutResourceId, parent, false);

        mHolder = new BusinessHolder();

        mHolder.mTxt_STDNAME = (TextView) row
                .findViewById(R.id.txt_stdName);

        mHolder.mTxt_STDCOURSE = (TextView) row
                .findViewById(R.id.txt_stdCourse);

        mHolder.mTxt_STDYEAR = (TextView) row
                .findViewById(R.id.txt_stdYear);        

        mHolder.mImageViewSelection = (ImageView) row
                .findViewById(R.id.img_selection);

        mHolder.mLin_layout=(LinearLayout)       row.findViewById(R.id.Layout_listText);


        row.setTag(mHolder);

    } else {

        mHolder = (BusinessHolder) row.getTag();
    }

        mHolder.mTxt_STDNAME.setText(FacultyDatabaseHalper.mStudentStudentName.get(position));

        mHolder.mTxt_STDCOURSE.setText(FacultyDatabaseHalper.mStudentCourse.get(position));

        mHolder.mTxt_STDYEAR.setText(FacultyDatabaseHalper.mStudentYear.get(position));


        mHolder.mLin_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                if(myposition.contains(String.valueOf(position))){


                     myposition.remove(String.valueOf(position));


                     mHolder.mImageViewSelection.setImageResource(R.drawable.arrow);


                    }else{


                     myposition.add(String.valueOf(position));




                       mHolder.mImageViewSelection.setImageResource(R.drawable.check);

                    }

                    notifyDataSetChanged();

            }
        });

    return row;
    }

    public class BusinessHolder {

    TextView mTxt_STDNAME, mTxt_STDYEAR,
            mTxt_STDCOURSE; 
    ImageView mImageViewSelection;

    LinearLayout mLin_layout;

    }

    }

1 个答案:

答案 0 :(得分:0)

你需要在listView上有听众,而不是在视图上,这也会给你一个位置

Listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

       Object o = prestListView.getItemAtPosition(position);
       prestationEco str=(prestationEco)o;//As you are using Default String Adapter
       Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
        }
    });