0

I ran into problem with creating listview in my android app.

This is my code:

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);

        }
    });

}

and this is my layout file

<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" />

'

What I am trying to achieve is showing the progress bar till the listview not filled with data the progress bar is displaying continuously. i am adding name of mp3 file from asset folder in my map and then adding that map in to ListItems and till this process is going i am showing a progress bar.

Can anybody point me to right direction?

Thanks in advance.

bee81
  • 35
  • 1
  • 7

1 Answers1

0

As the exception stands, you can not call wait() and notify()/notifyAll() without a synchronized block.

probably you want to call emAdapter.notifyDataSetChanged() instead of emAdapter.notifyAll();

Object.notifyAll() will wake up all the thread waiting on an object. Is that really what you need?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • so what i have to do basically put this code FileListView.setEmptyView(findViewById(R.id.emptyText)); emAdapter.notifyAll(); in synchronized block right? @blackbelt – bee81 May 02 '13 at 10:52
  • probably you want to call it emAdapter.notifyDataSetChanged(); – Blackbelt May 02 '13 at 10:54
  • notifyDataSetChanged() is undefined for ListAdapter @blackbelt – bee81 May 02 '13 at 10:58
  • change private ListAdapter emAdapter; to private SimpleAdapter emAdapter; – Blackbelt May 02 '13 at 11:00
  • Well thanks @blackbelt exception is gone but i got a new problem the progress bar is continues displaying and my list view is not showing the data. – bee81 May 02 '13 at 11:04