在android中列表视图的每个行项目的背景颜色的设置

时间:2014-03-26 10:23:43

标签: android mysql json listview

当数据使用jsonparser从mysql数据库动态传递并通过simpleAdapter设置列表时,如何设置listview中每个行项的背景颜色 vaule来自文本框,根据我必须在listview列表中设置每个roe的背景。

2 个答案:

答案 0 :(得分:1)

使用SimpleAdapter无法做到这一点。您应该从BaseAdapter或CursorAdapter派生一个新类,然后您可以自定义视图的颜色。

答案 1 :(得分:0)

您只需在Listview创建中使用此自定义适配器,然后您将在此块中编写代码...

在您的活动中使用此代码......

  CustomAdapter mAdapter = new CustomAdapter(this, R.layout.listitem, mListItems);
  mPullRefreshListView.setAdapter(mAdapter);

然后在这里替换你的布局......

public class CustomAdapter extends ArrayAdapter<Sample> {

public ArrayList<Sample> mlist;
public Context context;
public LayoutInflater inflater;

public CustomAdapter(Context context, int resource, ArrayList<Sample> mlist) {
    super(context, resource);
    this.mlist = mlist;
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getPosition(Sample item) {
    return super.getPosition(item);
}

@Override
public Sample getItem(int position) {
    return mlist.get(position);
}

@Override
public int getCount() {
    return mlist.size();
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.listitem, null);
    LinearLayout layout = (LinearLayout)convertView.findViewById(R.id.linearlayoutSample);;
    TextView text1 = (TextView) convertView.findViewById(R.id.item1);
    TextView text2 = (TextView) convertView.findViewById(R.id.item2);
    layout.setBackgroundColor(Color.GREEN);
    text1.setText(mlist.get(position).getListitem1());
    text2.setText(mlist.get(position).getListitem2());
    text2.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // you just put your Logic here And use this custom adapter to
            // load your Data By using this particular custom adapter to
            // your listview

        }
    });
    return convertView;
}

 }