我正在一个项目中,我正在显示一个车主列表。在访问应用程序时,会显示一个空列表。用户搜索所有者,并显示符合搜索条件的所有可用所有者。 我遇到的问题是所有者列表已更新,但它也显示“没有要显示的记录”,这是一个空列表应该显示的内容。我在这里错过了什么? 下面是相关的类对2013年8月6日 我更新了OwnerDetail类,并增加了asyncktask为私有内部类自定义ListAdapter不更新
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;
}
}
编辑。它显示了列表,但它也显示“无记录”。
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>
我将其更改为在onPostExecute中添加适配器,并将textview可见性设置为消失,但仍未清除“没有要显示的记录”。 @Override protected void onPostExecute(List result){ listItem =(ArrayList )result; super.onPostExecute(result); ()listItem.size()> 0) { ownerAdapter.notifyDataSetChanged(); }} –
Gagouche
@Gagouche放置一个日志(请参阅更新的代码答案),看看它是否在运行时显示在logcat输出中。如果不是,任务ID根本没有完成。 –
我编辑原始故事以添加自定义xml文件。它显示添加到适配器的4个项目。我可以看到列表中的4个项目。但是,它也显示“没有要显示的记录”,它是空列表显示的默认文本。 – Gagouche