【问题标题】:No data available listview没有可用的数据列表视图
【发布时间】:2013-05-02 10:43:48
【问题描述】:

我在我的 android 应用程序中创建列表视图时遇到了问题。

这是我的代码:

public class MainActivity extends ListActivity {
private SimpleAdapter emAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set layout as view
    setContentView(R.layout.main_ui_layout);
    final Utility utility = new Utility(this);
    utility.getFiles();
    final ListView FileListView = (ListView) findViewById(android.R.id.list);
    FileListView.setEmptyView(findViewById(R.id.progressBar));

    // Create a HashMap for items
    final ArrayList<HashMap<String, String>> ListItems = new ArrayList<HashMap<String, String>>();

    // we using thread to simulate the download of data
    Thread emThread = new Thread(new Runnable() {

        public void run() {
            // Waiting 3s
            try {
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // Looping through all item nodes <item>

            for (int em = 0; em < utility.FileMap.size(); em++) {

                HashMap<String, String> emMap = new HashMap<String, String>();
                File Files = new File(utility.FileMap.get("DuroodSharif"));
                emMap.put(Files.getName(), utility.FileMap.get("DuroodSharif"));
                ListItems.add(emMap);
            }
        }
    });
    // Stop Progress Bar
    utility.StopProgressBar();

    // Adding ListItems to ListView
    emAdapter = new SimpleAdapter(this, ListItems, R.layout.list_item, new String[] { getString(R.string.TITLE_ID),
            getString(R.string.DESCRIPTION_ID) }, new int[] { R.id.title, R.id.desciption });

    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // Set List Adapter
            FileListView.setAdapter(emAdapter);


        }
    });
    emThread.start();

    // Later there's no data
    Thread myThread = new Thread(new Runnable() {

        public void run() {
            // Waiting 3s
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            ListItems.clear();

            runOnUiThread(new Runnable() { // Run on the UI Thread to setthe
                                            // adapter to the list
                public void run() {
                    FileListView.setEmptyView(findViewById(R.id.emptyText));
                    emAdapter.notifyDataSetChanged();

                }
            });
        }
    });

    myThread.start();

    // Selecting single ListView item
    ListView emlistView = getListView();

    emlistView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // Getting values from selected ListItem
            String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(getString(R.string.TITLE_ID), title);
            in.putExtra(getString(R.string.DESCRIPTION_ID), description);

            startActivity(in);

        }
    });

}

这是我的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/app_background_portrait_type2"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:visibility="gone" />

<TextView
    android:id="@+id/emptyText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="No data available"
    android:visibility="gone" />

'

我想要实现的是显示进度条,直到列表视图没有填充进度条连续显示的数据。我正在从我的地图中的资产文件夹中添加 mp3 文件的名称,然后将该地图添加到 ListItems 中,直到这个过程进行,我正在显示一个进度条。

谁能指出我正确的方向?

提前致谢。

【问题讨论】:

  • 发布您的完整布局代码。你采用了哪种布局?

标签: java android android-layout android-listview android-progressbar


【解决方案1】:

作为例外,您不能在没有synchronized 块的情况下调用wait()notify()/notifyAll()

您可能想调用emAdapter.notifyDataSetChanged() 而不是emAdapter.notifyAll();

Object.notifyAll() 将唤醒所有在object 上等待的线程。这真的是你需要的吗?

【讨论】:

  • 所以我要做的基本上是把这段代码 FileListView.setEmptyView(findViewById(R.id.emptyText)); emAdapter.notifyAll();在同步块中对吗? @blackbelt
  • 可能你想叫它 emAdapter.notifyDataSetChanged();
  • notifyDataSetChanged() 未为 ListAdapter @blackbelt 定义
  • 更改私有 ListAdapter emAdapter;到私有 SimpleAdapter emAdapter;
  • 谢谢@blackbelt 异常消失了,但我遇到了一个新问题,进度条继续显示,我的列表视图没有显示数据。
猜你喜欢
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
相关资源
最近更新 更多