填充自定义列表视图

时间:2012-12-02 12:07:45

标签: android

我想创建一个包含自定义行的列表视图。我创建了一个扩展ListActivity的类和每个行项的XML。我还添加了一个生成一些虚拟数据的方法。但这就是我被困住的地方。如何从我的主课程中调用此内容并在main.xml填充列表视图?

CustomListView.java

public class CustomListView extends ListActivity  {

private ArrayList<HashMap<String,String>> list;

public CustomListView(){

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

    SimpleAdapter adapter = new SimpleAdapter(
            this,
            list,
            R.layout.custom_list_item,
            new String[] {"pen","price","color"},
            new int[] {R.id.text1,R.id.text2, R.id.text3});

    populateList();

    setListAdapter(adapter);

}

private void populateList() {

    HashMap<String,String> temp = new HashMap<String,String>();
    temp.put("pen","MONT Blanc");
    temp.put("price", "200.00$");
    temp.put("color", "Silver, Grey, Black");
    list.add(temp);
    HashMap<String,String> temp1 = new HashMap<String,String>();
    temp1.put("pen","Gucci");
    temp1.put("price", "300.00$");
    temp1.put("color", "Gold, Red");
    list.add(temp1);
    HashMap<String,String> temp2 = new HashMap<String,String>();
    temp2.put("pen","Parker");
    temp2.put("price", "400.00$");
    temp2.put("color", "Gold, Blue");
    list.add(temp2);
    HashMap<String,String> temp3 = new HashMap<String,String>();
    temp3.put("pen","Sailor");
    temp3.put("price", "500.00$");
    temp3.put("color", "Silver");
    list.add(temp3);
    HashMap<String,String> temp4 = new HashMap<String,String>();
    temp4.put("pen","Porsche Design");
    temp4.put("price", "600.00$");
    temp4.put("color", "Silver, Grey, Red");
    list.add(temp4);

}

}

custom_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/text1" 
android:textSize="16sp" 
android:textStyle="bold" 
android:textColor="#FFFF00" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/>

<TextView android:id="@+id/text2"
android:textSize="12sp" 
android:textStyle="bold" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent"/>

<TextView android:id="@+id/text3" 
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/>

2 个答案:

答案 0 :(得分:2)

你完成了50%的一些步骤

1删除CustomListView的构造函数。不需要这个。

2覆盖onCreate方法

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    list = new ArrayList<HashMap<String, String>>();
    populateList();
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.custom_list_item, new String[] { "pen", "price",
                    "color" }, new int[] { R.id.text1, R.id.text2,
                    R.id.text3 });
    setListAdapter(adapter);
}

3在manifest文件中添加您的活动,例如

<activity
        android:name=".CustomListView"
        />

4如何从主Activity开始。你想点击按钮吗?然后你可以使用Intents

startActivity(new Intent(yourMainActivity.this,CustomListView.class))

答案 1 :(得分:1)

创建自定义列表单元格实际上意味着您要创建自定义适配器。

您可以为列表选择数据源类型,而不是为其创建适配器并根据需要显示信息。

通常情况下,您需要实施getView方法并为自定义视图而不是常规视图充气。

这是自定义适配器的代码示例:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a, String[] d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        TextView text=(TextView)vi.findViewById(R.id.text);;
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        return vi;
    }
}

尝试阅读vogalla's tutorial on listView and Adapter