android上的自定义列表视图

android上的自定义列表视图

本文介绍了填充自定义适配器,数组列表和HashMap android上的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表视图(如吹),我有与HashMap的填充数组列表

i have a custom list view(like blow) and i have an array list that fill with hashmap

现在我想,以填补自定义适配器,数组列表和HashMap自定义列表视图

now i want to fill custom list view with custom adapter,array list and hashmap

我尝试了很多,检查许多问题,但我不是成功

i try a lot and check many question but i'm not success

这是我的自定义适配器code

here's my custom adapter code

    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;
    }

}

这是我的猫的日志

and this is my cat log

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文件

custom list view xml file

    <?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>

感谢您

推荐答案

我有了新的适配器code。使用ViewHolder方法试试这个:

I have made new Adapter Code using ViewHolder Method Try this one:

  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;

    }
}

这篇关于填充自定义适配器,数组列表和HashMap android上的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:14