ListView子访问问题

时间:2011-08-10 00:06:53

标签: android listview

我目前有以下适配器来设置我的列表视图:

    public class MyListAdapter extends ArrayAdapter<String> {

    private Context mContext;

    public MyListAdapter(Context context, int resource, int textViewResourceId,String[] objects) {
        super(context, resource, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Drawable cross = getResources().getDrawable(R.drawable.cross);
        Drawable arrow = getResources().getDrawable(R.drawable.arrow);

        View row = convertView;

         if(row == null){
          LayoutInflater inflater= getLayoutInflater();
          row = inflater.inflate(R.layout.list_view_format2, parent, false);
         }

         TextView text =(TextView)row.findViewById(R.id.list_content2);
         text.setText(list[position]);

         ImageView image = (ImageView)row.findViewById(R.id.rightArrow);
         TextView check = (TextView)row.findViewById(R.id.checker);

         if (item != null && text.getText().toString().equals(item)){
             text.setBackgroundDrawable(cross);
             image.setImageDrawable(arrow);
             check.setText("cross");
         }


         return row;

    }
}

R.layout.list_view_format2:自定义ListView格式。它包含一个ImageView和一个TextView R.id.list_content2:*在R.id.list_view_format2 *中的文本视图的ID *

如果搜索成功,我正在尝试更改孩子的某些部分。但是,当我向上或向下滚动ListView时,其他子项的布局也会被修改,而不是只有一个子项被修改。我不知道发生了什么。有人可以解释一下吗?

2 个答案:

答案 0 :(得分:1)

ArrayAdatper是一个非常有限的课程,请尝试转到SimpleAdapter,其中还有一些功能可以让您更轻松地完成自己想做的事情。而不是覆盖getView(),而是查看SimpleAdapter.ViewBinder的功能,这些功能将允许您根据基础数据对列表项进行大量修改。在这里,您只能使某些列表项看起来与其他列表项不同。

答案 1 :(得分:0)

我明白了。我只需将其他人设为null!我在else块中做到了。现在它完美无缺。

  @Override
   public View getView(int position, View convertView, ViewGroup parent) {
         // TODO Auto-generated method stub
         Drawable cross = getResources().getDrawable(R.drawable.cross);
         Drawable arrow = getResources().getDrawable(R.drawable.arrow);

     View row = convertView;

     if(row == null){
      LayoutInflater inflater= getLayoutInflater();
      row = inflater.inflate(R.layout.list_view_format2, parent, false);
     }

     TextView text =(TextView)row.findViewById(R.id.list_content2);
     text.setText(list[position]);

     ImageView image = (ImageView)row.findViewById(R.id.rightArrow);
     TextView check = (TextView)row.findViewById(R.id.checker);

     if (item != null && text.getText().toString().equals(item)){
         text.setBackgroundDrawable(cross);
         image.setImageDrawable(arrow);
         check.setText("cross");
     }



   else {    
      text.setBackgroundDrawable(null);
      image.setImageDrawable(null);
      check.setText("none");
   }


  return row;

}