getView没有正在从CustomAdapter称为 [英] getView not being called from CustomAdapter

查看:238
本文介绍了getView没有正在从CustomAdapter称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直虎视眈眈试图找到这个问题的答案了一段时间。我放。我需要一套新的眼睛。我查看我的CustomAdapter内getView不会被调用。

 公共类User_List_Fragment扩展ListFragment {
    查看视图。
    DBHelper分贝;
    清单< UTable>用户列表;
    ListView控件列表;
    TextView的ID,名字,姓氏,职务;
    串MID;    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        鉴于= inflater.inflate(R.layout.view_users_fragment,集装箱,FALSE);        名单=(ListView控件)view.findViewById(android.R.id.list);
        DB =新DBHelper(getActivity());
        用户列表= db.getUserList();        类CustomAdapter扩展ArrayAdapter<串GT; {
             私人上下文的背景下;            公共CustomAdapter(上下文的背景下,INT textViewResourceId,列表与LT; UTable>用户列表){
                    超(背景下,textViewResourceId);
                    this.context =背景;
             }            @覆盖
            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                查看排= convertView;
                如果(行== NULL){
                    LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    行= inflater.inflate(R.layout.custom_list_row,NULL);
                }                ID =(TextView中)row.findViewById(R.id.list_id);
                的firstName =(TextView中)row.findViewById(R.id.list_first_name);
                的lastName =(TextView中)row.findViewById(R.id.list_last_name);
                工作=(TextView中)row.findViewById(R.id.list_job);
                对于(UTable uTable:用户列表){
                    id.setText(将String.valueOf(uTable.getId()));
                    firstName.setText(uTable.getFirstName());
                    lastName.setText(uTable.getLastName());
                    job.setText(uTable.getJob());                    字符串MID =将String.valueOf(uTable.getId());
                    串MF = uTable.getFirstName();
                    串毫升= uTable.getLastName();
                    串J = uTable.getJob();                    Log.v(ID,MID);
                    Log.v(名,MF);
                    Log.v(姓,毫升);
                    Log.v(作业,J);
                 }
                返回行;
            }
        }
        list.setAdapter(新CustomAdapter(getActivity()getApplicationContext(),R.layout.custom_list_row,用户列表));
        db.close();
        返回视图。
    }
}

view_users_fragment.xml

 <?XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < ListView控件
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@机器人:ID /列表
            机器人:比重=中心/>        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/ NO_DATA
            机器人:ID =@机器人:ID /空
            机器人:比重=中心/>    < / LinearLayout中>

custom_list_row.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>    <的TextView
        机器人:ID =@ + ID / LIST_ID
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =10
        机器人:文字=@字符串/一/>    <的TextView
        机器人:ID =@ + ID / list_first_name
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =25
        机器人:文字=@字符串/ first_name_hint/>    <的TextView
        机器人:ID =@ + ID / list_last_name
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =25
        机器人:文字=@字符串/ last_name_hint/>    <的TextView
        机器人:ID =@ + ID / list_job
        机器人:layout_width =0dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =5
        机器人:文字=@字符串/ job_hint/>< / LinearLayout中>

默认的TextView 代替被调用。它发生在当列表为空,没有内容的说明。我已经测试了数据库查询和它的作品。如果我把我的for循环我getView之外它返回的结果。我想我搞乱了我的构造我的CustomAdapter,但我似乎无法弄清楚。任何帮助是AP preciated。

修改RE-因素code

User_List_Fragment.java

 公共类User_List_Fragment扩展ListFragment {
    查看视图。
    DBHelper分贝;
    清单< UTable>用户列表;
    ListView控件列表;
    CustomAdapter适配器;    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
        鉴于= inflater.inflate(R.layout.view_users_fragment,集装箱,FALSE);        DB =新DBHelper(getActivity());
        用户列表= db.getUserList();        名单=(ListView控件)view.findViewById(android.R.id.list);
        适配器=新CustomAdapter(getActivity()getApplicationContext(),R.layout.custom_list_row,用户列表。);
        list.setAdapter(适配器);        db.close();
        返回视图。
    }
}

CustomAdapter.java

 公共类CustomAdapter扩展ArrayAdapter< UTable> {
     私人上下文的背景下;    公共CustomAdapter(上下文的背景下,INT textViewResourceId,列表与LT; UTable>用户列表){
        超(背景下,textViewResourceId);
        this.context =背景;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        如果(convertView == NULL){
            convertView = View.inflate(背景下,R.layout.custom_list_row,NULL);
        }        TextView的ID =(TextView中)convertView.findViewById(R.id.list_id);
        TextView中的firstName =(TextView中)convertView.findViewById(R.id.list_first_name);
        TextView中的lastName =(TextView中)convertView.findViewById(R.id.list_last_name);
        TextView的工作=(TextView中)convertView.findViewById(R.id.list_job);        UTable uTable =的getItem(位置);
        如果(uTable!= NULL){
            id.setText(将String.valueOf(uTable.getId()));
            firstName.setText(uTable.getFirstName());
            lastName.setText(uTable.getLastName());
            job.setText(uTable.getJob());            Log.v(工作,uTable.getJob());
        }
        返回convertView;
    }
}


解决方案

好吧,我刚快看,你是误会了如何使用适配器。

getView由通过数据的每一行调用。与对象数据中获取视图中的字段,你应该只的getItem(位置),并填写

要更清楚 - 你不会需要一个for循环可言,只要你在某个地方层次结构延伸出的baseadapter循环将自动

示例

 类CustomAdapter扩展ArrayAdapter< UTable> {        公共CustomAdapter(上下文的背景下,INT textViewResourceId,列表与LT; UTable>用户列表){
                超(背景下,textViewResourceId,用户列表);
         }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            如果(convertView == NULL){
               convertView = View.inflate(R.layout.custom_list_row,NULL);
            }            //谷歌的观点持有者模式,当你更exerienced
            TextView的ID =(TextView中)row.findViewById(R.id.list_id);
            TextView中的firstName =(TextView中)row.findViewById(R.id.list_first_name);
            TextView中的lastName =(TextView中)row.findViewById(R.id.list_last_name);
            TextView的工作=(TextView中)row.findViewById(R.id.list_job);            UTable项目=的getItem(位置);
            job.setText(item.getJob());
            ...            返回convertView;
        }
    }

的东西被打破:

  // Java类应该被命名为喜欢UserListFragment
  公共类User_List_Fragment扩展ListFragment {     //你不应该在这里有这些观点,他们不是在fragmetn的一部分
     //它们在列表中的每个行的一部分,因此将继续改变!
     TextView的ID,名字,姓氏,职务;
     串MID;     公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){       ...           //你是onCreateView方法在这里里面 - 哟ucant decalre一个方法中的类!
           类CustomAdapter扩展ArrayAdapter< UTable> {

So I've been lurking around trying to find the answer to this for a while. I'm caving. I need a fresh set of eyes. My View getView inside my CustomAdapter is not being called.

public class User_List_Fragment extends ListFragment {
    View view;
    DBHelper db;
    List<UTable> userList;
    ListView list;
    TextView id, firstName, lastName, job;
    String mId;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.view_users_fragment, container, false);

        list = (ListView) view.findViewById(android.R.id.list);
        db = new DBHelper(getActivity());
        userList = db.getUserList();

        class CustomAdapter extends ArrayAdapter<String> {
             private Context context;

            public CustomAdapter(Context context, int textViewResourceId, List<UTable> userList) {
                    super(context, textViewResourceId);
                    this.context = context;
             }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                if (row == null) {
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.custom_list_row, null);
                }

                id = (TextView) row.findViewById(R.id.list_id);
                firstName = (TextView) row.findViewById(R.id.list_first_name);
                lastName = (TextView) row.findViewById(R.id.list_last_name);
                job = (TextView) row.findViewById(R.id.list_job);               
                for (UTable uTable : userList) {
                    id.setText(String.valueOf(uTable.getId()));
                    firstName.setText(uTable.getFirstName());
                    lastName.setText(uTable.getLastName());
                    job.setText(uTable.getJob());

                    String mId = String.valueOf(uTable.getId());
                    String mF = uTable.getFirstName();
                    String mL = uTable.getLastName();
                    String j = uTable.getJob();

                    Log.v("ID", mId);
                    Log.v("First Name", mF);
                    Log.v("Last Name", mL);
                    Log.v("Job", j);                    
                 }              
                return row;
            }           
        }
        list.setAdapter(new CustomAdapter(getActivity().getApplicationContext(), R.layout.custom_list_row, userList));
        db.close();
        return view; 
    }
}

view_users_fragment.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" >

        <ListView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@android:id/list"
            android:gravity="center" />

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_data"
            android:id="@android:id/empty"
            android:gravity="center"/>

    </LinearLayout>

custom_list_row.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="horizontal" >

    <TextView
        android:id="@+id/list_id" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".10"
        android:text="@string/one"/>

    <TextView
        android:id="@+id/list_first_name" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:text="@string/first_name_hint"/>

    <TextView
        android:id="@+id/list_last_name" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:text="@string/last_name_hint"/>

    <TextView
        android:id="@+id/list_job"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="@string/job_hint" />

</LinearLayout>

The default TextView is instead being called. Which happens when the list is empty and its stating "no data found". I have tested the query to the database and it works. If I place my for loop outside of my getView it returns the results. I think I'm messing up my constructor for my CustomAdapter, but I can't seem to figure it out. Any help is appreciated.

EDIT RE-FACTORED CODE

User_List_Fragment.java

public class User_List_Fragment extends ListFragment {
    View view;
    DBHelper db;
    List<UTable> userList;
    ListView list;
    CustomAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.view_users_fragment, container, false);

        db = new DBHelper(getActivity());
        userList = db.getUserList();

        list = (ListView) view.findViewById(android.R.id.list);
        adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.custom_list_row, userList);     
        list.setAdapter(adapter);

        db.close();
        return view; 
    }
}

CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<UTable> {
     private Context context;

    public CustomAdapter(Context context, int textViewResourceId, List<UTable> userList) {
        super(context, textViewResourceId);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView== null) {
            convertView = View.inflate(context, R.layout.custom_list_row, null);
        }

        TextView id = (TextView) convertView.findViewById(R.id.list_id);
        TextView firstName = (TextView) convertView.findViewById(R.id.list_first_name);
        TextView lastName = (TextView) convertView.findViewById(R.id.list_last_name);
        TextView job = (TextView) convertView.findViewById(R.id.list_job);

        UTable uTable = getItem(position);
        if (uTable != null) {
            id.setText(String.valueOf(uTable.getId()));
            firstName.setText(uTable.getFirstName());
            lastName.setText(uTable.getLastName());
            job.setText(uTable.getJob());

            Log.v("JOB", uTable.getJob());
        }
        return convertView;
    }           
}

解决方案

Ok, I've just taken a quick look, you are misunderstanding how to use an Adapter.

getView is called by each row of data passed in. Inside get view you should just getItem(position) and fill in the fields with that objects data

To be clearer - you wont need a for loop at all, as long as you are extending a baseadapter somewhere in the heirarchy the loop will be automatic

Example

  class CustomAdapter extends ArrayAdapter<UTable> {

        public CustomAdapter(Context context, int textViewResourceId, List<UTable> userList) {
                super(context, textViewResourceId, userlist);
         }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView== null) {
               convertView = View.inflate(R.layout.custom_list_row, null);
            }

            //google the view holder pattern when you are more exerienced
            TextView id = (TextView) row.findViewById(R.id.list_id);
            TextView firstName = (TextView) row.findViewById(R.id.list_first_name);
            TextView lastName = (TextView) row.findViewById(R.id.list_last_name);
            TextView job = (TextView) row.findViewById(R.id.list_job);               

            UTable item = getItem(position);
            job.setText(item.getJob());
            ...

            return convertView;
        }           
    }

Things that are broken:

  //Java classes should be named like UserListFragment 
  public class User_List_Fragment extends ListFragment {

     //You shouldnt have these views here, they arent part of the fragmetn
     //they are part of each row in the list and so will keep changing!
     TextView id, firstName, lastName, job;
     String mId;

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

       ...

           //You are inside the onCreateView method here - yo ucant decalre a class inside a method!
           class CustomAdapter extends ArrayAdapter<UTable> {

这篇关于getView没有正在从CustomAdapter称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆