清单列表视图文本在列表视图加载前显示几秒钟

时间:2015-07-08 06:02:47

标签: android android-fragments android-listview

让我快速说明这里有一个与此类似的问题ListView shows empty message briefly before data is loaded

但是这会使用ListFragment并派生自CursorAdapter,而我正在使用"普通"片段和我的适配器派生自BaseAdapter。

我设置了一个空文本视图,以便在数据库中没有数据时显示,但在加载数据时会短暂显示文本视图。

enter image description here

在我的片段中,我有这段代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseManager = new DatabaseManager(getActivity());
    progressDialog = new ProgressDialog(getActivity());
    setHasOptionsMenu(true);
    mContext = getActivity();
}



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

    itemListView = (ListView) rootView.findViewById(R.id.itemListView);
    emptyText = (TextView) rootView.findViewById(R.id.empty);        
    registerForContextMenu(itemListView);
    itemListView.setClickable(true);
    itemListView.setEmptyView(emptyText);
    ActionBar actionBar = getActivity().getActionBar();
    if (actionBar != null) {
        actionBar.setTitle("Items");
    }

    new ItemListLoadAsync().execute();

    return rootView;
}

在我的派生BaseAdapter类中,我有:

 public class ItemViewAdapter extends BaseAdapter {

 @Override
public int getCount() {
    return items.size();
}


@Override
public Object getItem(int position) {
    return items.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
                mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_list_row, null);


    }

    itemNameTextView = (TextView) convertView.findViewById(R.id.itemNameTextView);
    descriptionTextView = (TextView) convertView.findViewById(R.id.descriptionTextView);


    itemNameTextView.setText(items.get(position).getItemName());
    descriptionTextView.setText(items.get(position).getItemDescription());

    return convertView;

}

我该如何纠正?我使用"对"我的清单适配器?

2 个答案:

答案 0 :(得分:0)

您可以稍后调用setEmptyView(),以便确定是否有要加载的数据。

答案 1 :(得分:0)

根据@ MDroid的评论,我删除了该行:

itemListView.setEmptyView(emptyText);

然后在我的ItemListAsyncTask的onPostExecute()方法中,我这样做了:

public void onPostExecute(List<Item> items) {
        long delayInMillis = 500;
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                progressDialog.dismiss();
            }
        }, delayInMillis);


        if(items.size() == 0) {
            itemListView.setEmptyView(emptyText);
        } else {
            ItemViewAdapter = new ItemViewAdapter(mContext, items);
            itemListView.setAdapter(itemViewAdapter);
        }

    }