我从服务器收到消息我希望在列表视图中显示它。用户阅读消息后,我想将文本视图的颜色更改为灰色,因为我想区分读取和未读消息。
答案 0 :(得分:0)
您需要创建CustomAdapter
。并在getView()
中为已读/未读邮件设置条件,并根据需要更改文字颜色。
试试这个
<强> CustomListAdapter.java:强>
public class CustomListAdapter extends BaseAdapter {
private Context context;
private ArrayList<MessageData> dataList;
public CustomListAdapter(Context context, ArrayList<MessageData> arr) {
super();
this.context = context;
this.dataList = arr;
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listitem_spinner, null);
}
TextView textView = (TextView) view.findViewById(R.id.txt_spinner_item);
textView.setText("" + dataList.get(position).getMessage());
// here you can get Read/Unread status for all message
boolean isRead = dataList.get(position).isRead();
if (isRead) {
textView.setTextColor(Color.GRAY);
} else {
textView.setTextColor(Color.BLACK);
}
return view;
}
}
然后从mainactivity调用
<强> MainActivity.java:强>
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.requestview);
listView = (ListView) findViewById(R.id.mylist_pendingtask);
ArrayList<MessageData> arrMessageData = new ArrayList<MainActivity.MessageData>();
MessageData data1 = new MessageData();
data1.setMessage("message 1");
data1.setRead(true);
MessageData data2 = new MessageData();
data2.setMessage("message 2 (unread)");
data2.setRead(false);
MessageData data3 = new MessageData();
data3.setMessage("message 3");
data3.setRead(true);
arrMessageData.add(data1);
arrMessageData.add(data2);
arrMessageData.add(data3);
CustomListAdapter adapter = new CustomListAdapter(this, arrMessageData);
listView.setAdapter(adapter);
}
public class MessageData {
String message = "";
boolean isRead = false;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isRead() {
return isRead;
}
public void setRead(boolean isRead) {
this.isRead = isRead;
}
}
}