在android上用自定义适配器,数组列表和hashmap填充自定义列表视图

时间:2014-10-12 11:08:13

标签: android listview arraylist hashmap

我有一个自定义列表视图(像打击),我有一个填充hashmap的数组列表 custom listview staructure

现在我想用自定义适配器,数组列表和hashmap

填充自定义列表视图

我尝试了很多并且检查了很多问题,但我没有成功

这是我的自定义适配器代码

    public class messages_Adapter extends ArrayAdapter<HashMap<String, String>>
{
    ArrayList<HashMap<String, String>> MessageAdapter=new ArrayList<HashMap<String,String>>();
    Context context;
    int layoutResourceId;

    public messages_Adapter(Context context,int layoutResourceId,ArrayList<HashMap<String, String>> MessageAdapter)
    {
        super(context, layoutResourceId, MessageAdapter);
        this.MessageAdapter=MessageAdapter;
        this.context=context;
        this.layoutResourceId=layoutResourceId;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parrent)
    {
        HashMap<String, String>hashmap_Current;
        View view=convertView;

        if (view==null)
        {
            LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=layoutInflater.inflate(layoutResourceId, parrent, false);
        }
        hashmap_Current=new HashMap<String, String>();
        hashmap_Current=MessageAdapter.get(position);

        Log.e("Zdit", hashmap_Current.toString());

        TextView    lbl_MessageID=          (TextView)findViewById(id.xActMessages_LblMessageID);
        lbl_MessageID.setText(hashmap_Current.get("id").toString());
        view.setTag(lbl_MessageID);


        TextView    lbl_MessageContent=     (TextView)findViewById(id.xActMessages_LblMessageText);
        lbl_MessageContent.setText(hashmap_Current.get("message"));
        view.setTag(lbl_MessageContent);


        TextView    lbl_TimeAdd=            (TextView)findViewById(id.xActMessages_LblTimeAdd);
        lbl_TimeAdd.setText(hashmap_Current.get("time_add"));
        view.setTag(lbl_TimeAdd);

        TextView    lbl_DateAdd=            (TextView)findViewById(id.xActMessages_LblDateAdd);
        lbl_DateAdd.setText(hashmap_Current.get("date_add"));
        view.setTag(lbl_DateAdd);

        return view;
    }

}

这是我的猫咪日志

10-12 13:41:13.181: E/AndroidRuntime(2298): FATAL EXCEPTION: main
10-12 13:41:13.181: E/AndroidRuntime(2298): Process: com.TB.Zdit, PID: 2298
10-12 13:41:13.181: E/AndroidRuntime(2298): java.lang.NullPointerException
10-12 13:41:13.181: E/AndroidRuntime(2298):     at com.TB.Zdit.JAct_Messages$messages_Adapter.getView(JAct_Messages.java:243)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.ListView.onMeasure(ListView.java:1175)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.view.View.measure(View.java:16458)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
10-12 13:41:13.181: E/AndroidRuntime(2298):     at android.view.View.measure(View.java:16458)

消息适配器

do
            {
                hashMap_Messages=new HashMap<String, String>();
                hashMap_Messages.put("id", cursor_Fetcher.getString(0));
                hashMap_Messages.put("message", cursor_Fetcher.getString(1));
                hashMap_Messages.put("time_add", cursor_Fetcher.getString(2));
                hashMap_Messages.put("date_add", cursor_Fetcher.getString(3));

                arrayList_Messages.add(hashMap_Messages);
            }
            while (cursor_Fetcher.moveToNext());

            messages_Adapter messAdapter=new messages_Adapter(getApplicationContext(),R.layout.xact_messages, arrayList_Messages);
            lstViw_Messages.setAdapter(messAdapter);

自定义列表视图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" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5sp"
        android:background="@drawable/linerlayout_border"
        android:paddingTop="5sp"
        android:paddingBottom="5sp"
        android:layout_marginTop="5sp"
        android:layout_marginBottom="5sp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/xActMessages_LblMessageText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Message Content\nMessage Content\nMessage Content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:gravity="right"
                android:textColor="#000000" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="invisible" >

            <TextView
                android:id="@+id/xActMessages_LblMessageID"
                android:layout_width="0sp"
                android:layout_height="0sp"
                android:text="Message ID"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:gravity="right" />

        </LinearLayout>

        <LinearLayout
            android:baselineAligned="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="100"
            android:orientation="horizontal"
            android:paddingTop="20sp" >

            <LinearLayout
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="60" >

                <Button
                    android:id="@+id/button1"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@drawable/button_ok"
                    android:textColor="#FFFFFF"
                    android:drawableLeft="@drawable/sharethis_32_white"
                    android:paddingLeft="7dp"
                    android:layout_marginRight="5dp"  />

                <Button
                    android:id="@+id/button1"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@drawable/button_add"
                    android:textColor="#FFFFFF"
                    android:drawableLeft="@drawable/edit_32_white"
                    android:paddingLeft="7dp"
                    android:layout_marginRight="5dp" />

                <Button
                    android:id="@+id/button1"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:textColor="#FFFFFF"
                    android:background="@drawable/button_delete"
                    android:drawableLeft="@drawable/delete_32_delete"
                    android:paddingLeft="7dp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="0sp"
                android:layout_height="match_parent"
                android:layout_weight="40"
                android:orientation="vertical"
                android:weightSum="100" >


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0sp"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_weight="50" >

                    <TextView
                        android:id="@+id/xActMessages_LblTimeAdd"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Time"
                        android:textColor="#000000" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0sp"
                    android:orientation="vertical"
                    android:gravity="center"
                    android:layout_weight="50">

                    <TextView
                        android:id="@+id/xActMessages_LblDateAdd"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:text="Date"
                        android:textColor="#000000" />

                </LinearLayout> 
           </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

谢谢

1 个答案:

答案 0 :(得分:4)

我使用ViewHolder方法制作了新的适配器代码试试这个:

  public class messages_Adapter extends ArrayAdapter<HashMap<String, String>>
{
    ArrayList<HashMap<String, String>> MessageAdapter=new ArrayList<HashMap<String,String>>();
    Context context;
    int layoutResourceId;

    public messages_Adapter(Context context,int layoutResourceId,ArrayList<HashMap<String, String>> MessageAdapter)
    {
        super(context, layoutResourceId, MessageAdapter);
        this.MessageAdapter=MessageAdapter;
        this.context=context;
        this.layoutResourceId=layoutResourceId;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parrent)
    {
        HashMap<String, String>hashmap_Current;
        View view=convertView;
    final ViewHolder holder;

        if (view==null)
        {
            LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=layoutInflater.inflate(layoutResourceId, parrent, false);
        holder=new ViewHolder();
        holder.lbl_MessageID=(TextView)view.findViewById(id.xActMessages_LblMessageID);
        holder.lbl_MessageContent=(TextView)view.findViewById(id.xActMessages_LblMessageText);        
        holder.lbl_TimeAdd=(TextView)view.findViewById(id.xActMessages_LblTimeAdd);
        holder.lbl_DateAdd=(TextView)view.findViewById(id.xActMessages_LblDateAdd);
        view.setTag(holder);
    }else{

        holder=(ViewHolder)view.getTag();
    }

        hashmap_Current=new HashMap<String, String>();
        hashmap_Current=MessageAdapter.get(position);

        Log.e("Zdit", hashmap_Current.toString());


        holder.lbl_MessageID.setText(hashmap_Current.get("id").toString());       
        holder.lbl_MessageContent.setText(hashmap_Current.get("message"));       
        holder.lbl_TimeAdd.setText(hashmap_Current.get("time_add"));       
        holder.lbl_DateAdd.setText(hashmap_Current.get("date_add"));


        return view;
    }

    static class ViewHolder {

        TextView lbl_MessageID, lbl_MessageContent,lbl_TimeAdd,lbl_DateAdd;

    }
}