所以我一直潜伏着试图找到答案。我正在探洞。我需要一双新鲜的眼睛。我的CustomAdapter中的My View getView未被调用。
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>
而是调用默认的TextView
。当列表为空并且声明“未找到数据”时会发生这种情况。我已经测试了数据库的查询,它的工作原理。如果我将我的for循环放在getView之外,它会返回结果。我想我正在弄乱我的CustomAdapter的构造函数,但我似乎无法搞清楚。任何帮助表示赞赏。
编辑重新确定的代码
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;
}
}
答案 0 :(得分:1)
好的,我刚看了一眼,你误解了如何使用适配器。
传入的每一行数据都会调用getView。在内部获取视图时,您应该getItem(position)
并使用该对象数据填写字段
更清楚 - 你根本不需要for循环,只要你在heirarchy中的某个地方扩展baseadapter就会自动循环
示例强>
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;
}
}
破碎的事情:
//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> {
答案 1 :(得分:0)
您需要将自定义适配器设置为listview。 list.setAdapter(customAdapter)
答案 2 :(得分:0)
您也需要将数据传递给构造函数,使用以下代码更改Constructor方法:
public CustomAdapter(Context context, int textViewResourceId, List<UTable> userList) {
super(context, textViewResourceId , userList);
this.context = context;
}
或使用BaseAdapter
类而不是ArrayAdapter
并覆盖GetCount()和GetItem()方法,并在GetCount
方法中返回列表的大小
答案 3 :(得分:0)
你的getview方法背后的原因是......
为了让适配器知道它有多少行,您必须从Constructor传递数据或重写getCount方法。
你没有做任何事情。所以适配器不知道任何数据。所以
使用以下任何构造函数
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20T[]%29
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20T[]%29
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20T[]%29
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter%28android.content.Context,%20int,%20int,%20java.util.List%3CT%3E%29
或 覆盖getCount方法
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getCount%28%29
你完成了。 希望有帮助