从Listview中的对象显示两种不同的数据类型

时间:2013-12-02 16:51:47

标签: java android arraylist hashmap simpleadapter

基本上,我希望使用值“test”和值“createdAt”填充ListView。我已经想出如何在listview中显示任何一个,但不是两个。很显然,我不了解它是如何在相当基础的水平上运作的。我不知道如何将两种不同的数据类型(字符串和日期)一起显示在同一个ListView元素中。我知道SimpleAdapter需要一个包含地图的List,但如果我只能在地图中存储一个键值对,那么我如何一次显示多个值呢?

        public void done(List<ParseObject> results, ParseException e) {
            if (e == null) {
                ArrayList<HashMap<String, Date>> messages= new ArrayList<HashMap<String, Date>>();
                for (ParseObject result : results) {
                    HashMap<String, Date> testMap = new HashMap<String,Date>();
                    HashMap<String, String> dateMap = new HashMap<String,String>();

                    testMap.put("test",(String)result.get("test"));

                    dateMap.put(createdAtKey,(Date)result.getCreatedAt());

                    messages.add(message);
                                            //What do I do with the "test"values?

                }
                SimpleAdapter adapter = new SimpleAdapter(FeedActivity.this, messages,android.R.layout.simple_list_item_2, new String[] {testKey,createdAtKey}, new int[] {android.R.id.text1, android.R.id.text2 });
                setListAdapter(adapter);
            } else {
                Log.e(TAG, "Exception caught!", e);
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

我假设您将正确的数据传递到适配器中,但无法显示它。

首先 - 您需要创建一个XML文件,该文件将表示填充列表视图的布局。例如,假设text1和text2是应该显示结果的文本框的id,我会这样做:

<?xml version ="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layoutContainer" >
    <TextView 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/text1" />
</LinearLayout>

Inisde您的活动,使用String []值来实现目标格式:

String[] values = new String[testMap.size];
for(int i=0;i<testMap.size;i++) {
    values[i] = "[" + testMap.get(yourKey) + "]-[" + dateMap.get(yourKey) + "]" //replace yourKey with the appropriate key string
CustomAdapter myAdapter = new CustomAdapter(getActivity(), values);

然后实现一个CustomAdapter类,就像这样

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {
    private String[] data;
    private Context context;

    public CustomAdapter(Context context, String[] values) {
        this.context = context;
        this.data = values;
    }
    @Override
    public int getCount() {
        return data.length;
    }
    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return data[position];
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View adapterView = convertView;
        if (adapterView == null) {
            adapterView = inflater.inflate(R.layout.row, null);
        }
        TextView text1 = (TextView) adapterView.findViewById(R.id.text1);
        text1.setText(data[position]);
        return adapterView;
    }
}
相关问题