1

I have do this simple app, in this app when I click on start button I want to see the data in Listview, but it does't work. Can you help me?

mainActivity.java

public class MainActivity extends ListActivity {
public ArrayList<Map<String, String>> list= new ArrayList<Map<String, String>>();
public ListAdapter adapter;
public ProgressDialog progressDialog; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonAggiorna = (Button) findViewById(R.id.buttonStart);
    buttonAggiorna.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

        String[] from = { "name", "purpose" };
        int[] to = { android.R.id.text1, android.R.id.text2 };      
 adapter = new SimpleAdapter(MainActivity.this,     list,android.R.layout.simple_list_item_2, from, to);       

  progressDialog = ProgressDialog.show(MainActivity.this, "Getting data", "Loading...");
            taskBack t=new taskBack();
             t.execute();               
        }

    });// fine onClickListner


    Button buttonAllarm = (Button) findViewById(R.id.buttonAllarm);
    buttonAllarm.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {            
                AppLog.logString("list size:"+list.size()); 

        }
});


}

 private class taskBack 
 extends    AsyncTask<Void, Void, ArrayList<Map<String, String>>> {

    @Override
    protected ArrayList<Map<String, String>> doInBackground(Void... arg0) {
    ArrayList<Map<String, String>> l = new ArrayList<Map<String, String>>();
    l = startEla();
    return l;
    }

    protected void onPostExecute(ArrayList<Map<String, String>> result) {
        AppLog.logString("Parte onPostExecute");
        list=result;
        setListAdapter(adapter);    
        progressDialog.dismiss();

    }

    private ArrayList<Map<String, String>> startEla() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(putData("android","mobile"));
        list.add(putData("Windows7", "Windows7"));
        list.add(putData("iPhone", "iPhone"));
        return list;

    }

    private Map<String, String> putData(String name, String purpose) {
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("name", name);
        item.put("purpose", purpose);
        return item;

    }
}
}

In this link you can see the xml file enter link description here

Best Regads A.

2
  • 2
    Can you elaborate on "it does't work"? Nothing happens? It crashes? Your phone blew up?
    – Paul
    Aug 20, 2012 at 21:18
  • @Paul Hypothetically, what would your suggestion be if it was the latter (phone blew up)?
    – theblang
    Oct 25, 2013 at 15:00

3 Answers 3

2

Move these two lines to the main body of the ListActivity...

String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };      

Remove the following line from the onClick(...) listener in onCreate(...)...

adapter = new SimpleAdapter(MainActivity.this, list, android.R.layout.simple_list_item_2, from, to);

...and put it in the onPostExecute(...) method of the AsyncTask as follows...

protected void onPostExecute(ArrayList<Map<String, String>> result) {
    AppLog.logString("Parte onPostExecute");
    list=result;
    adapter = new SimpleAdapter(MainActivity.this, list, android.R.layout.simple_list_item_2, from, to);
    setListAdapter(adapter);    
    progressDialog.dismiss();
}
1

Check this: http://www.dailygeek.de/using-asynctask-to-update-a-listactivity/

Don't forget to call adapter.notifyDataSetChanged() in onPostExecute()

1

You had a couple issues with your code. First you're using a SimpleAdapter in conjunction with an Async task..typically if you're going to go through the work of doing an Async task its possible you'll be adding data to the list after initialization.. which if you are then you'll have to switch to an ArrayAdapter. I've adjusted your code to work for it.

You also need to set the ListView ID in the XML to android:id="@android:id/list"

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TwoLineListItem;

public class MainActivity extends ListActivity {
public ArrayList<BasicNameValuePair> list= new ArrayList<BasicNameValuePair>();
public ArrayAdapter<BasicNameValuePair> adapter;
public ProgressDialog progressDialog; 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //You should init your Adapter during onCreate or onStart, not on the onClick 
    String[] from = { "name", "purpose" };
    int[] to = { android.R.id.text1, android.R.id.text2 };   
    adapter = new ArrayAdapter<BasicNameValuePair>(this,android.R.layout.simple_list_item_2, list){       
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TwoLineListItem row;
        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);
        }else{
            row = (TwoLineListItem)convertView;
        }
        BasicNameValuePair data = list.get(position);
        row.getText1().setText(data.getName());
        row.getText2().setText(data.getValue());

        return row;
        }
    };
    setListAdapter(adapter);
    Button buttonAggiorna = (Button) findViewById(R.id.buttonStart);
    buttonAggiorna.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            taskBack t=new taskBack();
             t.execute();               
        }

    });// fine onClickListner


    Button buttonAllarm = (Button) findViewById(R.id.buttonAllarm);
    buttonAllarm.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {            
        }
});


}

 private class taskBack extends    AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

       // Commented out the below since you initlize the array list in startEla
        //ArrayList<Map<String, String>> l = new ArrayList<Map<String, String>>();
       startEla();
       return (Void)null;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        //Moved the progress dialog show to the onPreExecute
         progressDialog = ProgressDialog.show(MainActivity.this, "Getting data", "Loading...");
    }

    @Override
    protected void onPostExecute(Void result) {
        //AppLog.logString("Parte onPostExecute");
        //On dynamic adapters (Array adapters mainly) you need to notify them once the data set has changed
        MainActivity.this.adapter.notifyDataSetChanged();
        progressDialog.dismiss();

    }

    private void startEla() {
        list.add(new BasicNameValuePair("android", "mobile"));
        list.add(new BasicNameValuePair("Windows7", "Windows7"));
        list.add(new BasicNameValuePair("iPhone", "iPhone"));
    }

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.