0

我正在一个项目中显示车主列表。访问应用程序时,会显示一个空列表。用户搜索所有者,我们会显示与搜索条件匹配的所有可用所有者。我遇到的问题是所有者列表已更新,但它也显示“没有要显示的记录”,这是一个空列表应该显示的内容。我在这里缺少什么?这是相关的类

   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>
4

2 回答 2

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 的 id@android/id/empty和可见性设置为gone.

于 2013-08-06T18:29:48.167 回答
0

您必须刷新适配器,因为当您创建片段时,片段owner将被null初始化为空行。

修改你SetAdapter(Owner[] o)的如下:

public void SetAdapter(Owner[] o) {
    owner = o;
    ownerAdapter.clear();
    ownerAdapter.addAll(o);
    ownerAdapter.notifyDataSetChanged();
}
于 2013-08-05T20:56:21.400 回答