0

我遇到了在我的android应用程序中创建listview的问题。没有可用的数据listview

这是我的代码:

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中,直到此过程正在显示进度条。

有人可以指我正确的方向吗?

在此先感谢。

+0

发布您的完整布局代码。你采取了哪种布局? – 2013-05-02 11:19:30

回答

0

作为例外情况,您不能拨打wait()notify()/notifyAll()而不使用​​块。

可能要呼叫emAdapter.notifyDataSetChanged()而不是emAdapter.notifyAll();

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

+0

所以我要做的事情基本上把这个代码FileListView.setEmptyView(findViewById(R.id.emptyText)); emAdapter.notifyAll();在同步块权利? @blackbelt – bee81 2013-05-02 10:52:31

+0

可能你想调用它emAdapter.notifyDataSetChanged(); – Blackbelt 2013-05-02 10:54:35

+0

notifyDataSetChanged()未定义为ListAdapter @blackbelt – bee81 2013-05-02 10:58:08

相关问题