0

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

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

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)

message adapter

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

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>

thank you

9
  • what is line no.243 in getview method? Commented Oct 12, 2014 at 11:14
  • @Dhaval Gondaliya lbl_MessageID.setText(hashmap_Current.get("id").toString()); Commented Oct 12, 2014 at 11:17
  • Instead of ` id.xActMessages_LblMessageID ` use ` R.id.xActMessages_LblMessageID `. Commented Oct 12, 2014 at 11:20
  • @Dhaval Gondaliya same error; not working Commented Oct 12, 2014 at 11:28
  • data from hashmap is null, put them in log and check it. Log.e("ID", hashmap_Current.get("id").toString()); Commented Oct 12, 2014 at 11:31

1 Answer 1

4

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;

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you alot dear but it has error in line holder.lbl_MessageID.setText(hashmap_Current.get("id").toString()); catlog 10-12 15:27:46.229: E/AndroidRuntime(1927): Process: com.TB.Zdit, PID: 1927 10-12 15:27:46.229: E/AndroidRuntime(1927): java.lang.NullPointerException 10-12 15:27:46.229: E/AndroidRuntime(1927): at com.TB.Zdit.JAct_Messages$messages_Adapter.getView(JAct_Messages.java:254)
Sloved; i have two small mistake 1:i send incorrect layout to class 2: i do not use view before findViewById; thankyou a lot DEAR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.