0

有谁知道如何在创建列表视图时为每行的背景着色?

我有一个从我的数据库中提取的数组列表,并在其中填充了一个带有列表视图的布局。

我怀疑可能有一种方法可以使用简单的适配器来做到这一点,但无法弄清楚。

任何帮助将非常感激 :)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_list);

        // Read var from Intent
        Intent intent= getIntent();
        final String ListID = intent.getStringExtra("ListID");
        golbalItemID = ListID;

        ivAdd = (ImageView) findViewById(R.id.ivAdd);
        ivCancel = (ImageView) findViewById(R.id.ivCancel);
        tvTotItems = (TextView) findViewById(R.id.tvTotItems);

        final myDBClass myDb = new myDBClass(this);
        final ArrayList<HashMap<String, String>> MebmerList = myDb.SelectAllItemData(ListID);       

        myData =  myDb.SelectItemData(Integer.parseInt(ListID.toString())); 

        // listView1
        final ListView lisView1 = (ListView)findViewById(R.id.listView1); 
        registerForContextMenu(lisView1);

        MyAdapter sAdap;
        sAdap = new MyAdapter(ListItems.this, MebmerList, R.layout.activity_column, new String[] {"Name", "Price", "Quan"}, new int[] {R.id.ColName, R.id.ColTel, R.id.ColQuan});      
        lisView1.setAdapter(sAdap); 

        lisView1.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
                  int iChk = 0;
                // Show Data
                String arrData[] = myDb.SelectItemData((MebmerList.get(position).get("ItemID").toString()));
                if(arrData != null)
                {
                    iChk = Integer.parseInt(arrData[4]);    
                }

                if(iChk == 1)
                {
                    ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 0);
                    change_color(lisView1, position, 255, 255, 255);

                    System.out.println("POSITION!ichk=1" + myAdapter.getItemAtPosition(position).toString());
                    setTitle(myAdapter.getItemAtPosition(position).toString());
                }

                else if(iChk == 0)
                {
                    ischkCheck(Integer.parseInt(MebmerList.get(position).get("ItemID").toString()), 1);
                    change_color(lisView1, position, 155, 155, 138);

                    System.out.println("POSITION!ichk=0" + myAdapter.getItemAtPosition(position).toString());
                }

              }});

        ivAdd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent newActivity = new Intent(ListItems.this,AddItem.class);
                newActivity.putExtra("ListID", ListID);
                startActivity(newActivity);
                finish();
            }
        });

        ivCancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent newActivity = new Intent(ListItems.this,MenuScreen.class);
                startActivity(newActivity); 
                finish();
            }
        });
4

2 回答 2

3

创建一个Adapter类,并控制其中每一行的颜色,然后将其设置为适配器ListView 这是我的一个项目中的示例代码,检查getView功能:

public class ListAdapter extends BaseAdapter {

    private LayoutInflater myInflater;
    private List<Poet> list;

    public ListAdapter(Context context) {
        super();
        myInflater = LayoutInflater.from(context);

        Log.d("Ganjoor", "Data passed to the adapter.");

    }

    static class ViewHolder {
        TextView tvName;
    }

    public void setData(List<Poet> list) {
        this.list = list;
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Poet getItem(int position) {

        return (null == list) ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_adapter, parent,
                    false);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvName.setTag(list.get(position).getId());
        holder.tvName.setText(list.get(position).getName());
        // Log.d("Ganjoor", "Adapter: " + list.get(position).getName());

        if (position % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.grad_blue);
        } else {
            convertView.setBackgroundResource(R.drawable.row_style);
        }

        return convertView;
    }

}
于 2012-11-20T15:59:11.207 回答
0

正如@Nikita Beloglazov 所说,您可以通过实现自定义 ArrayAdapter 来做到这一点,将您的配色方案放在 getView Override 方法中。请参阅ArrayAdapter 文档

于 2012-11-20T15:57:07.710 回答