我正在开展一个项目,我正在显示一个车主名单。访问应用程序时,将显示一个空列表。用户搜索所有者,并显示匹配搜索条件的所有可用所有者。 我遇到的问题是所有者列表已更新,但它还显示“没有要显示的记录”,这是一个空列表应该显示的内容。我在这里缺少什么? 以下是相关的课程
public class OwnerDetail extends ListFragment {
ListView mListView;
ArrayList<Owner> listItem = new ArrayList<Owner>();
public OwnerAdapter ownerAdapter;
private String[] searchParameter = null;
public void SearchParameters(String[] parameters)
{
searchParameter = parameters;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v1 = inflater.inflate(R.layout.customize_layout,container, false);
mListView = (ListView) v1.findViewById(android.R.id.list);
return v1;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ownerAdapter = new OwnerAdapter(getActivity(), R.layout.owner_detail, listItem);
if(!(searchParameter == null))
{
ownerAdapter.clear();
ownerAdapter.addAll(listItem);
new OnwerAsyncTask().execute();
ownerAdapter.notifyDataSetChanged();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
mListView.setAdapter(ownerAdapter);
ownerAdapter.notifyDataSetChanged();
}
private class OnwerAsyncTask extends AsyncTask<Void, Void, List<Owner>> {
@Override
protected List<Owner> doInBackground(Void... params) {
try
{
Owner items = new Owner();
items.setFirstName("John");
items.setLastName("Smith");
items.setCarId("1");
listItem.add(items);
Owner item1 = new Owner();
item1.setFirstName("Samantha");
item1.setLastName("Right");
item1.setCarId("2");
listItem.add(item1);
Owner item2 = new Owner();
item2.setFirstName("Regie");
item2.setLastName("Miller");
item2.setCarId("3");
listItem.add(item2);
Owner item3 = new Owner();
item3.setFirstName("Mark");
item3.setLastName("Adam");
item3.setCarId("4");
listItem.add(item3);
} catch(Exception ex)
{
ex.toString();
}
return listItem;
}
@Override
protected void onPreExecute() {
listItem.clear();
ownerAdapter.clear();
super.onPreExecute();
}
@Override
protected void onPostExecute(List<Owner> result) {
/*OwnerDetail fragment = (OwnerDetail) mActivity.getSupportFragmentManager().findFragmentById(R.id.fOwnerDetail);
fragment.SetAdapter(result);*/
super.onPostExecute(result);
if(listItem.size() > 0)
{
ownerAdapter.notifyDataSetChanged();
}
}
}
}
public class OwnerAdapter extends ArrayAdapter<Owner> {
private Context context;
private int layoutResourceId;
private Owner data[] = null;
public OwnerAdapter(Context context, int textViewResourceId,Owner[] data) {
super(context, textViewResourceId, data);
this.context = context;
this.layoutResourceId = textViewResourceId;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
OwnerHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new OwnerHolder();
holder.tvFName = (TextView) row.findViewById(R.id.tvFirstName);
holder.tvLName = (TextView) row.findViewById(R.id.tvLastName);
holder.tvCId = (TextView) row.findViewById(R.id.tvCarID);
row.setTag(holder);
} else
{
holder = (OwnerHolder) row.getTag();
}
Owner item = data[position];
holder.tvFName.setText(item.getFirstName());
holder.tvLName.setText(item.getLastName());
holder.tvCId.setText(item.getCarId());
return row;
}
static class OwnerHolder
{
TextView tvFName;
TextView tvLName;
TextView tvCId;
}
}
编辑于08/06/2013 我更新了OwnerDetail类,并将asyncktask添加为私有内部类。它显示了列表,但它也显示了“无记录显示”。
owner_detail.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/tvFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
<TextView
android:id="@+id/tvLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
<TextView
android:id="@+id/tvCarID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
// customize_layout.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:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:text="No record to be displayed."
/>
</LinearLayout>
答案 0 :(得分:0)
您必须刷新适配器,因为在创建片段时owner
将为null
,因此片段将使用空行进行初始化。
修改您的SetAdapter(Owner[] o)
,如下所示:
public void SetAdapter(Owner[] o) {
owner = o;
ownerAdapter.clear();
ownerAdapter.addAll(o);
ownerAdapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
您没有在onPostExecute
中向适配器添加数据:
@Override
protected void onPostExecute(List<Owner> result) {
super.onPostExecute(result);
ownerAdapter.clear();
ownerAdapter.addAll(result);
ownerAdapter.notifyDataSetChanged();
Log.i("TESTING","Added " + result.size() + " items to adapter");
}
另外,我发现您使用的是自定义列表布局,请确保空TextView的标识为@android/id/empty
,并且可见性设置为gone
。