2016-04-18 44 views
0

私にはListViewがあり、項目を比較したいと思います。 項目が同じ場合は、ListViewの項目の背景色を同じにしたいとします。 お願いします。ListViewの同じ項目の背景色を同じにする

+2

何をしましたか? –

+0

私はリストビュー(名前と年齢)に人のリストを持っています。私はこれらの人々の年齢を比較しています。同じ年齢の人が2人以上いる場合、その人のリストビューに同じ色を入れたいと思います。 –

+0

AlertDialog alertDialogObject = builder.create(); \t \t ListView listView = alertDialogObject.getListView(); \t \t listView.setDivider(new ColorDrawable(Color.BLUE)); \t \t \tプロデューサp1 = null、p2 = null; \t \t if(p1.getTour()。equals(p2.getTour())){ \t \t \t listView.setBackgroundColor(Color.BLUE); \t \t} –

答えて

0

リスト用のアダプタを作成する必要があります。 アダプタ内では、1行のレイアウトをカスタマイズするなどの作業を行うことができます

0

これは、ListView用のカスタムアダプタで実行できます。

次のアダプタ(ソースデータとしてSQL Liteデータベースクエリを使用するListViewのカーソルアダプタ)では、getViewメソッドで背景色が交互に設定されています。同じアイテムを検出するコードは、より複雑になり、データに依存する可能性があります。

/** 
* Created by Mike092015 on 17/02/2016. 
*/ 
public class Database_Inspector_ProductsDB_Adadpter extends CursorAdapter { 
    public Database_Inspector_ProductsDB_Adadpter(Context context, Cursor cursor, int flags) { 
     super(context, cursor, 0); 
    } 

    @Override 
    public View getView(int position, View convertview, ViewGroup parent) { 
     View view = super.getView(position, convertview, parent); 
     Context context = view.getContext(); 
     if (position % 2 == 0) { 
      view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven)); 
     } else { 
      view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd)); 
     } 
     return view; 
    } 


    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 

     TextView textviewproductid = (TextView) view.findViewById(R.id.adipe_productsdb_id); 
     TextView textviewproductname = (TextView) view.findViewById(R.id.adipe_productsdb_name); 
     TextView textviewproductorder = (TextView) view.findViewById(R.id.adipe_productsdb_order); 
     TextView textviewproductaisle = (TextView) view.findViewById(R.id.adipe_productsdb_aisle); 
     TextView textviewproductuses = (TextView) view.findViewById(R.id.adipe_productsdb_uses); 
     TextView textviewproductnotes = (TextView) view.findViewById(R.id.adipe_productsdb_notes); 

     textviewproductid.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_ID_INDEX)); 
     textviewproductname.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_NAME_INDEX)); 
     textviewproductorder.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_ORDER_INDEX)); 
     textviewproductaisle.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_AISLE_INDEX)); 
     textviewproductuses.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_USES_INDEX)); 
     textviewproductnotes.setText(cursor.getString(ShopperDBHelper.PRODUCTS_COLUMN_NOTES_INDEX)); 

    }; 
    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.activity_database_inspect_productsdb_entry,parent, false); 
    } 
} 
関連する問題