当record = value时,如何检查HashMap中的值<string,string =“”>和setBackgroundResource?

时间:2016-05-06 08:08:39

标签: android listview arraylist hashmap android-studio-2.1

我刚刚开始使用Android Studio 2.1编写Android应用程序,我的应用程序是一种离线消息 - 这是使用SQLite数据库(基本上由两个表组成消息和联系人)。

我现在处于上一次活动中,我需要从创建的联系人中填充消息交换列表。我能够列出它们,但没有美学。我想设置不同的setBackgroundResource,只要类型是LEFT或RIGHT但是很​​难应用它。 以下是我的代码:

MessageRepo - SQLite DBhelper:

    public ArrayList<HashMap<String, String>> getMessageListById(int fromid, int toid) {
    SQLiteDatabase db = helper.getWritableDatabase();
    String toquery = "SELECT " + MessageModel.messageId +
            ", " + MessageModel.fromId +
            ", " + MessageModel.toId +
            ", " + MessageModel.messageContent +
            ", 'RIGHT' AS type FROM " + MessageModel.tableMessage +
            " WHERE " + MessageModel.toId +
            " = ? AND " + MessageModel.fromId +
            " = ? ";
    String fromquery = "SELECT " + MessageModel.messageId +
            ", " + MessageModel.fromId +
            ", " + MessageModel.toId +
            ", " + MessageModel.messageContent +
            ", 'LEFT' AS type FROM " + MessageModel.tableMessage +
            " WHERE " + MessageModel.fromId +
            " = ? AND " + MessageModel.toId +
            " = ? ";
    String query = toquery +
            " UNION " + fromquery +
            " ORDER BY " + MessageModel.messageId;

    ArrayList<HashMap<String, String>> messageList = new ArrayList<HashMap<String, String>>();

    Cursor cursor = db.rawQuery(query, new String[]{String.valueOf(fromid), String.valueOf(toid), String.valueOf(fromid), String.valueOf(toid)});
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> messages = new HashMap<String, String>();
            messages.put("messageId", cursor.getString(0));
            messages.put("messageFromId", cursor.getString(1));
            messages.put("messageToId", cursor.getString(2));
            messages.put("messageContent", cursor.getString(3));
            messages.put("type", cursor.getString(4));
            messageList.add(messages);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return messageList;
}

消息活动 - 这将显示在ListView中但不起作用,因为我打算显示它:

        ArrayList<HashMap<String, String>> messageList = messagehandler.getMessageListById(_fromid, _toid);
    if (messageList.size() != 0) {
        ListView listView = getListView();
        if(messageList.contains("RIGHT")) {
            listView.setBackgroundResource(R.drawable.right);
        } else {
            listView.setBackgroundResource(R.drawable.left);
        }

        ListAdapter adapter = new SimpleAdapter(MessageDetailActivity.this, messageList, R.layout.message, new String[]{"messageFromId", "messageToId", "messageContent"}, new int[]{R.id.FromId, R.id.ToId, R.id.message});
        setListAdapter(adapter);
        }

我查了很多帖子,但似乎无法让它对我有用: The constructor ArrayAdapter>>(Context, int, ArrayList>) is undefined Android Alternate row Colors in ListView

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

经过多方研究。我终于找到了答案,选择使用List和一个setter / getter类:

public class MessagesAdapter extends ArrayAdapter<ContactsAndMessages> {

public MessagesAdapter(Context context, List<ContactsAndMessages> contacts) {
    super(context, 0, contacts);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ContactsAndMessages contact = getItem(position);

    Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/helvetica.ttf");

    if(convertView == null) {
        if (contact.type.equalsIgnoreCase("me")) {....