0

stackoverflow ユーザーの助けのおかげで、ある条件に基づいて特定の行の色を変更することができました。しかし、色を変えることは、私のニーズや期待に完全には適合しません。

それで私はウェブを調べ始め、それを変えようとしました。特定の行の TextView にテキストを設定したい。行の色を変更できるようにするには、Cursor を使用して SQLite から ListView に値を取得しているため、パーソナライズされた SimpleCursorAdapter クラスを作成するようにアドバイスされました。

それを読んでテストした後、これが私が思いついたものです:

public class MyCursorAdapter extends SimpleCursorAdapter {
    public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
        {   
            TextView price = (TextView)view.findViewById(R.id.priceInfo);
            price.setText("High");
        }
        else
            view.setBackgroundColor(0x00000000);
    }
}

ただし、現在取得しているのは、一部の行でテキストが重複しています (規則性があります)。私がどこかで読んだように、すべてのスクロールは ListView を更新していますが、(この TextView の insted) if 句を入れると、この行:view.setBackgroundColor(SELECTED_COLOR);すべてが正常に動作し、この 1 行だけが変更された可能性があります。

誰かがそれを機能させるために何をしなければならないか、または私の考えのどこが間違っているか教えてもらえますか?

4

2 に答える 2

1
public class MyCursorAdapter extends SimpleCursorAdapter {
    public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);         
        TextView price = (TextView)view.findViewById(R.id.priceInfo);
        if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
        {   
            price.setText("High");
            view.setBackgroundColor(/* red color? */);
        }
        else {
            price.setText("");
            view.setBackgroundColor(0x00000000);
        }
    }
}

「itemrecycling」のため、bindView のビューにはリサイクルされた色とテキストがあります。異なるリストビュー項目を変更する bindView 呼び出しごとに、すべてのプロパティを明示的に割り当てる必要があります。この場合は、背景色と価格ラベルです。

于 2012-07-20T05:56:53.570 に答える
0

getView メソッドをオーバーライドし、リストビュー項目の位置に基づいて convertView.setbackgroundColor を使用します。

このようなもの:

public class MyCustomAdapter extends ArrayAdapter<String> {

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);
if(postion == 1) //1st position in listview
 {
   convertView.setbackgroundColor(...)
 }

等々...

于 2012-07-17T13:10:22.370 に答える