Domanda

Sto usando un ListAdapter per popolare un elenco come questo:

static final String[] PROBLEMS = new String[] {"one", "two", "three" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);        

    setListAdapter(new ArrayAdapter<String>(this, R.layout.my_problems, PROBLEMS));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
.

E dopo che sto facendo una chiamata remota sul mio server per ottenere più dati per quell'elenco con una chiamata AsynctSask, e quando ritorni i dati dal server non so come popolare e ripristinare la vista.Finora ho qualcosa del genere:

    @Override
    protected void onPostExecute(String result) 
    {       
            // Unwrap the stuff from the JSON string                
            String problem_title = null;
            String problem_id = null;

            try
            {
                JSONArray obj = new JSONArray(result);
                JSONObject o = obj.getJSONObject(0);                    

                Log.d( "Title: " , "" + o.getString("problem_title") );       
                Log.d( "id: " , "" + o.getString("problem_id") );      

                problem_title = o.getString("problem_title");
                problem_id = o.getString("problem_id");
            }
            catch ( Exception e )
            {
            }

            // Now not sure what to do :)
            // How do I reset the list that I had set up above?
                }
.

Posso effettuare il risultato in dati strutturati in modo appropriato per ripristinare l'elenco ma non sono sicuro di come è fatto.Qualcuno può aiutarmi per favore?:)

È stato utile?

Soluzione

I use it this way,

    values = new ArrayList<String>();
    //put anything you want in values as start
    adapter = new ArrayAdapter<String>(this,R.layout.notification, values);
    setListAdapter(adapter);

then

    //change values array with your new data then update the adapter
    adapter.notifyDataSetChanged();

then the listview content will change at the time you execute this function

Altri suggerimenti

You do not need to reinitialize the adapter, if you just want to change the data set. Try the following code -

adapter.clear();
for(int i = 0;i<categoriesArray.length;i++){
    adapter.add(categoriesArray[i]);
} 

after that if required, you can notify the change to the adapter too, although it shouldn't be necessary.

adapter.notifyDataSetChanged();

if you're targetting API level 11 or above, you can use addAll() method on adapter. Since it's more efficient.

adapter.clear();
adapter.addAll(categoriesArray);
adapter.notifyDataSetChanged();

is good option to do this, but sometimes that wont work as we need. In that case you can setadapter again and your listview get refreshed. But this is not good option to do because it genrates whole listview again so this cause performance very down. And your app get slow.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top