簡體   English   中英

ListView子訪問問題

[英]ListView Child access problem

我目前有以下適配器來設置我的列表視圖:

    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 格式。 It contains an ImageView and a TextView R.id.list_content2: *Id of the Text View within R.id.list_view_format2*

如果搜索成功,我正在嘗試更改孩子的某些部分。 但是,當我向上或向下滾動 ListView 時,其他孩子的布局也會被修改,而不是只修改一個孩子。 我不完全明白發生了什么。 有人可以解釋一下嗎?

ArrayAdatper是一個非常有限的 class,請嘗試移至SimpleAdapter ,它具有更多功能,這將使您想做的事情更容易。 而不是覆蓋getView()查看SimpleAdapter.ViewBinder的功能,這將允許您根據基礎數據對列表項進行大量修改。 在這里,您將只能使某些列表項看起來與其他項不同。

我想到了。 我只需要將其他設置為 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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM