मेरे पास एक बहुत ही सरल प्रश्न है लेकिन Google मुझे उचित उत्तर नहीं दिखाता है। मुझे अपने listview में प्रत्येक आइटम के लिए वेरिएबल वैल्यू के आधार पर बैकग्राउंड कलर सेट करना होगा। handle स्थिति आइटम एक रंग का होना चाहिए और done दूसरे रंग का होना चाहिए।

सूचीदृश्य अनुकूलक

class ListViewAdapter extends BaseAdapter{

Context context;
LayoutInflater inflater;
String[] time;
String[] clientName;
String[] district;
String[] address;
String[] goods;
String[] price;
String[] status;

public ListViewAdapter(Context context, String[] time, String[] clientName, String[] district, String[] address, String[] goods, String[] price, String[] status) {
    this.context = context;
    this.time = time;
    this.clientName = clientName;
    this.district = district;
    this.address = address;
    this.goods = goods;
    this.price = price;
    this.status = status;
}


@Override
public int getCount() {
    return clientName.length; // было 0
}

@Override
public Object getItem(int position) {
    return null;
}


@Override
public long getItemId(int position) {
    return 0;
}

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

    TextView txtclientName;
    TextView txttime;
    TextView txtdistrict;
    TextView txtaddress;
    TextView txtgoods;
    TextView txtprice;

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);

    // Locate the TextViews in listview_item.xml
    txtclientName = (TextView) itemView.findViewById(R.id.clientNameSingle);
    txttime = (TextView)itemView.findViewById(R.id.timeSingle);
    txtdistrict = (TextView)itemView.findViewById(R.id.districtSingle);
    txtaddress = (TextView) itemView.findViewById(R.id.addressSingle);
    txtgoods = (TextView)itemView.findViewById(R.id.goodsSingle);
    txtprice = (TextView)itemView.findViewById(R.id.priceSingle);

    // Capture position and set to the TextViews
    txtclientName.setText(clientName[position]);
    txttime.setText(time[position]);
    txtdistrict.setText(district[position]);
    txtaddress.setText(address[position]);
    txtgoods.setText(goods[position]);
    txtprice.setText(price[position]);

    itemView.setBackgroundColor(Color.parseColor("#00FF7F"));

    return itemView;
}
}

आदेश सूची वर्ग

public class OrderList extends Activity {

ListView list;
ListViewAdapter adapter;
String[] time;
String[] clientName;
String[] district;
String[] address;
String[] goods;
String[] price;
String[] status;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_main);

    time = new String[]{"09:00", "09:00"};
    clientName = new String[]{"Анна", "Ольга"};
    district = new String[]{"Калининский", "Калининский"};
    address = new String[]{"Морская набережная д35", "Проспект Непокоренных 49"};
    goods = new String[]{"Елка 1.8м", "Сосна 2м"};
    price = new String[]{"1499", "2299"};
    status = new String[]{"done", "handle"};

    list = (ListView) findViewById(R.id.listview);

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, time, clientName, district, address, goods, price, status);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Animation animation1 = new AlphaAnimation(0.3f, 1.0f);
            animation1.setDuration(2000);
            view.startAnimation(animation1);


            Intent intent = new Intent(OrderList.this, SingleItemView.class);
            intent.putExtra("time", time);
            intent.putExtra("clientName", clientName);
            intent.putExtra("district", district);
            intent.putExtra("address", address);
            intent.putExtra("goods", goods);
            intent.putExtra("price", price);
            intent.putExtra("position", position);
            intent.putExtra("status", status);
            startActivity(intent);

        }

    });
}
}
-1
Александр Косырев 9 नवम्बर 2017, 00:14

1 उत्तर

आशा है कि आप अपने listview_item लेआउट फ़ाइल के अंदर लेआउट का उपयोग कर रहे हैं।

int[] color = {R.color.colorPrimary,R.color.colorAccent,R.color.colorPrimaryDark};

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.listview_item);

    relativeLayout.setBackgroundColor(color[2]);
0
Ashokkumar Adichill 9 नवम्बर 2017, 07:41