0

I have got one activity, but 3 layouts. ListView is a part of layout that is being displayed after loging in. That's why I can't set my ArrayAdapter onCreate, bacause my ListView is not initialized yet. I tried following this tutorial, but I could not reproduce these steps. In this tutorial author did everything onCreate.

I tried doing it this way:

ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;

...

    public class getcontacts extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected Boolean doInBackground(Void... params) {

 ...HERE I AM FETCHING DATA...

 listItems.add(json_data.getString("login") + " " + derp);
 }

@Override
    protected void onPostExecute(final Boolean success) {
        getc = null;


        ac = new addcontacts();
        ac.execute(true);

And here is the code of addcontacts class:

 public class addcontacts extends ListActivity {


     protected void onCreate() {


         adapter=new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    listItems);
         setListAdapter(adapter);

         adapter.notifyDataSetChanged();

     }
     protected void execute(final Boolean success) {

    done = true;
     }


 }

And the layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >



<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="188dp"
    android:layout_weight="0.53"
    android:text="Fetching contact list" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="106dp" >
</ListView>

</LinearLayout>

ListView doesn't contain anything. I checked in debugger that listItems have all items, so there is something wrong with adapter.

2
  • You don't instantiate Activities via explicit Contstructor as you have done. You should also pay attention to what you're writing, onCreate() needs to get a Bundle as an argument. Use @Override annotations, it will make like easier. Also, please use proper naming conventions and show us how each class is set up. If both the AsyncTask and the ListActivity are part of the same file, don't separate them. Commented Feb 18, 2013 at 18:44
  • Here is full code: pastebin.com/dA6dcZRv Commented Feb 18, 2013 at 18:55

1 Answer 1

1

I suggest your following approach:

  1. In Activity class initialise ListView and Adapter with empty List.
  2. Create subclass of BaseAdapter to get more control over it.
  3. Pass Adapter and List via constructor of Adapter.
  4. Pass Adapter and List in your AsyncTask subclass.
  5. In your doInBackground() method perform fetching data from Server and add each item to List.
  6. If you want to create Lazy-Adapter so each item added to List, call publishProgress() and in onProgressUpdate() method call yourAdapter.notifyDataSetChanged().
  7. If not, after work is done, in onPostExecute() method call yourAdapter.notifyDataSetChanged().
  8. Now it should works.

You are done.

Sign up to request clarification or add additional context in comments.

4 Comments

Here is very similar example Android Custom ListView with Image and Text where data are parsed from XML and dynamically added into List with updating.
Seems complicated. Is there another simpler way?
its complicated yes but you don need to work with images and memory. You need only look how parsing and updating adapter, nothing more.
Okay thanks. I'll look into it, but I suppose it's going to take me few days. x.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.