1

カスタムリストビューでテキストビューを取得しました。リスト内のすべてのテキストビューが変更されるようにテキストの色を設定する方法を知っています。しかし今、私は特定のビューの1つに色を変更したいだけです。たとえば、リストビューに10個のアイテムがあり、2番目のテキストビューの色を変更して残りは同じままにします。何か案が?助けてくれてありがとう〜

public class CheckWinNoAdapter extends ArrayAdapter<String> {
private final Context context;
private String[] values;



public CheckWinNoAdapter(Context context, String[] values) {
    // TODO Auto-generated constructor stub
    super(context, R.layout.list_draw, values);
    this.context = context;
    this.values = values;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_draw, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);


    textView1.setText(values[position]);




}

}

4

4 に答える 4

6

メモリの浪費ListView避けるために、ビューをリサイクルします。したがって、呼び出されるたびに新しいビューを膨らませる代わりに、 それを前のパラメータに割り当てて、再利用します。たとえば、スクロールを押したビューです。したがって、スクロールを押したままのTextViewが、異なる色のテキストビューと同じである場合、defaultcolorを実行したテキストビューに「yourcolor」が表示される可能性があります。したがって、ここでは、getViewがテキストカラーと呼ばれるたびに設定する必要があります。getViewconvertViewconvertView

 @Override 
 public View getView(int position, View convertView, ViewGroup parent) {


    if (converView == null) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          converView  = inflater.inflate(R.layout.list_draw, parent, false);
    }

    TextView textView1 = (TextView) converView.findViewById(R.id.chk_tv1);

    int color = (position == YOUR_POSITION) ? yourcolor : defaultcolor;
    textView1.setTextColor(color);

    textView1.setText(values[position]);

}
于 2012-07-17T07:54:29.333 に答える
4

getViewをこれに変更できます。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_draw, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);

    if(position== 2){
        textView1.setColor(Color.White);//or whatever you like
    }
    textView1.setText(values[position]);




}

}

于 2012-07-17T07:57:08.313 に答える
3

Get Viewでは、次のようにします。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_draw, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.chk_tv1);


textView1.setText(values[position]);

if(Your Condition Goes Here){
textView.setTextColor(Color.RED); // Did you tried this???
}

}
于 2012-07-17T07:54:16.683 に答える
1

位置を渡す代わりに、テキストの色を次のように設定しようとしているリストビューの整数値を渡します。

if (position == INT_VALUE)//where INT_VALUE=the position at which u want to setcolor of yours
   textView.setTextColor(color);//which color u want to set 
else
  textView.setTextColor(defaultcolor);//to remaining texts
于 2012-07-17T07:59:50.700 に答える