Fragment

时间:2016-06-02 07:08:38

标签: android listview

我想在片段中创建一个ListView,用于聊天,就像WhatsApp一样,但是适配器显示为null。以下是我的代码:

ChatsFragment.java

                package com.houssup.userchat;

            import android.os.Bundle;
            import android.support.annotation.Nullable;
            import android.support.v4.app.Fragment;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.ListView;

            import java.util.ArrayList;


            public class ChatsFragment extends Fragment {

                ListView list;
                CustomChatAdapter adapter;
                public  ChatsFragment chatsFragment = null;
                public  ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();



                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                }

                @Override
                public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                    super.onActivityCreated(savedInstanceState);

                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                         Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.customrow_chat, container, false);
                    ListView lv= ( ListView )view.findViewById( R.id.list );  // List defined in XML ( See Below )

                    /**************** Create Custom Adapter *********/
                    setListData();
                    adapter=new CustomChatAdapter( getContext(), CustomListViewValuesArr);
                    lv.setAdapter(adapter);
                    return view;
                }

                public void setListData()
                {

                    for (int i = 0; i < 11; i++) {

                        final ListModel sched = new ListModel();

                        /******* Firstly take data in model object ******/
                        sched.setName("Swapnil"+i);
                        sched.setDesignation("Interior Designer"+i);

                        /******** Take Model Object in ArrayList **********/
                        CustomListViewValuesArr.add( sched );
                    }
                }


            }

CustomChatAdapter.java

package com.houssup.userchat;

        import android.app.Activity;
        import android.app.Fragment;
        import android.content.Context;
        import android.content.res.Resources;
        import android.support.v7.widget.RecyclerView;
        import android.util.Log;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.BaseAdapter;
        import android.widget.ImageView;
        import android.widget.ListView;
        import android.widget.TextView;

        import java.util.ArrayList;
        import java.util.HashMap;
        import java.util.List;

        import de.hdodenhof.circleimageview.CircleImageView;

        /**
         * Created by hp on 01-06-2016.
         */
        public class CustomChatAdapter extends BaseAdapter{

            private Context context;
            private ArrayList data;
            private static LayoutInflater inflater=null;
            ListModel tempValues=null;
            int i=0;

            /*************  CustomChatAdapter Constructor *****************/
            public CustomChatAdapter(Context context, ArrayList d) {

                /********** Take passed values **********/
                this.context=context;
                data=d;

                /***********  Layout inflator to call external xml layout () ***********/
                inflater = ( LayoutInflater )context.
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

            /******** What is the size of Passed Arraylist Size ************/
            public int getCount() {

                if(data.size()<=0)
                    return 1;
                return data.size();
            }

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

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

            /********* Create a holder Class to contain inflated xml file elements *********/
            public static class ViewHolder{

                public TextView name;
                public TextView designation;
                public CircleImageView image;

            }

            /****** Depends upon data size called for each row , Create each ListView row *****/
            public View getView(int position, View convertView, ViewGroup parent) {

                View vi = convertView;
                ViewHolder holder;

                if(convertView==null){

                    /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
                    vi = inflater.inflate(R.layout.customrow_chat, null);

                    /****** View Holder Object to contain tabitem.xml file elements ******/

                    holder = new ViewHolder();
                    holder.name = (TextView) vi.findViewById(R.id.name);
                    holder.designation=(TextView)vi.findViewById(R.id.designation);
                    holder.image= (CircleImageView) vi.findViewById(R.id.image);

                    /************  Set holder with LayoutInflater ************/
                    vi.setTag( holder );
                }
                else
                    holder=(ViewHolder)vi.getTag();

                if(data.size()<=0)
                {
                    holder.name.setText("No Data");

                }
                else
                {
                    /***** Get each Model object from Arraylist ********/
                    tempValues=null;
                    tempValues = ( ListModel ) data.get( position );

                    /************  Set Model values in Holder elements ***********/

                    holder.name.setText( tempValues.getName() );
                    holder.designation.setText( tempValues.getDesignation() );
                    holder.image.setImageResource(R.drawable.circuladp);

                    /******** Set Item Click Listner for LayoutInflater for each row *******/

                   /* vi.setOnClickListener(new OnItemClickListener( position ));*/
                }
                return vi;
            }

          /*  @Override
            public void onClick(View v) {
                Log.v("CustomChatAdapter", "=====Row button clicked=====");
            }*/

            /********* Called when Item click in ListView ************/
           /* private class OnItemClickListener  implements View.OnClickListener {
                private int mPosition;

                OnItemClickListener(int position){
                    mPosition = position;
                }

                *//*@Override
                public void onClick(View arg0) {


                    CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;

                    *//**//****  Call  onItemClick Method inside CustomListViewAndroidExample Class ( See Below )****//**//*

                    sct.onItemClick(mPosition);
                }*/
            }

fragment_chats.xml

        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.houssup.userchat.ChatsFragment">


        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </ListView>

    </FrameLayout>

customrow_chat.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="wrap_content"
            android:orientation="horizontal">


        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/circuladp"
            app:civ_border_width="2dp"
            app:civ_border_color="#FF000000"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="vertical">
        <TextView
            android:id="@+id/name"
            android:layout_below="@id/title"
            android:text="Yashvant Rai Bacchhan"
            android:layout_marginLeft="50dp"
            android:textSize="18dp"
            android:textColor="#000000"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/designation"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="6dp"
            android:layout_marginLeft="55dp"
            android:textSize="14dp"
            android:layout_height="wrap_content"
            android:text="Designer"/>

        </LinearLayout>

        </LinearLayout>

这个片段属于TabActivity。我不知道为什么它显示为null。任何帮助将不胜感激 !!

2 个答案:

答案 0 :(得分:0)

见到你的代码。 convertView始终为null。 改变它:

public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;

            if(convertView==null){

                /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
                convertView = inflater.inflate(R.layout.customrow_chat, null);

                /****** View Holder Object to contain tabitem.xml file elements ******/

                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.designation=(TextView)convertView.findViewById(R.id.designation);
                holder.image= (CircleImageView) convertView.findViewById(R.id.image);

                /************  Set holder with LayoutInflater ************/
                convertView.setTag( holder );
            }
            else
                holder=(ViewHolder)convertView.getTag();

            if(data.size()<=0)
            {
                holder.name.setText("No Data");

            }
            else
            {
                /***** Get each Model object from Arraylist ********/
                tempValues=null;
                tempValues = ( ListModel ) data.get( position );

                /************  Set Model values in Holder elements ***********/

                holder.name.setText( tempValues.getName() );
                holder.designation.setText( tempValues.getDesignation() );
                holder.image.setImageResource(R.drawable.circuladp);

                /******** Set Item Click Listner for LayoutInflater for each row *******/

               /* vi.setOnClickListener(new OnItemClickListener( position ));*/
            }
            return convertView;
        }

答案 1 :(得分:0)

代码显示您为布局的片段视图进行了充气&#39; R.layout.customrow_chat&#39;但是布局名称是&#39; fragment_chats.xml&#39;。错字?