我遇到了在我的Android应用程序中创建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 :(得分:0)
例外情况是,如果没有wait()
阻止,您就无法致电notify()/notifyAll()
和synchronized
。
可能您想要拨打emAdapter.notifyDataSetChanged()
而不是emAdapter.notifyAll();
Object.notifyAll()
会唤醒等待object
的所有主题。这真的是你需要的吗?