2013-06-11 9 views
0

J'essaie de rafraîchir ma liste en cliquant sur un bouton. J'ai utilisé notifyDataSetChanged Mais ce n'est pas rafraîchissant.Actualiser la liste en cliquant sur un bouton sur android

static final String TAG_RESULTS="results"; 
static final String TAG_TWEET="text"; 
static final String TAG_USER_ID = "from_user_id_str"; 
static final String TAG_USER_NAME = "from_user"; 
static final String TAG_PIC_URL="profile_image_url"; 
static final String TAG_TWEET_ID="id_str"; 
//static final String URL="http://search.twitter.com/search.json?q="; 
static final String URL="https://api.twitter.com/1.1/search/tweets.json?q="; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tweet); 

    String url = getIntent().getStringExtra("url"); 
    JSONParser jParser = new JSONParser(); 
    JSONObject json = jParser.getJSONFromUrl(url); 
    Log.i("deneme", json.toString()); 

    try { 
     results = json.getJSONArray(TAG_RESULTS); 

     tweets=new String[results.length()]; 
     user_ids=new String[results.length()]; 
     user_names=new String[results.length()]; 
     pic_urls=new String[results.length()]; 
     tweet_ids=new String[results.length()]; 



     for(int i = 0; i < results.length(); i++){ 
      JSONObject c = results.getJSONObject(i); 

      tweets[i]= c.getString(TAG_TWEET); 
      user_ids[i]= c.getString(TAG_USER_ID); 
      user_names[i]= "@"+c.getString(TAG_USER_NAME); 
      pic_urls[i]= c.getString(TAG_PIC_URL); 
      tweet_ids[i]= c.getString(TAG_TWEET_ID); 


     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 


    lv= (ListView) findViewById(R.id.listView); 
    final LazyAdapter adapter = new LazyAdapter(this, pic_urls, user_names, tweets); 
    lv.setAdapter(adapter); 

    refreshButton= (ImageButton) findViewById(R.id.imageButton); 
    refreshButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(TweetActivity.this,"tık tık", Toast.LENGTH_LONG).show(); 

      adapter.notifyDataSetChanged(); 

    } 
}); 
}} 

Et aussi je l'ai écrit sur mon adaptateur

public void notifyDataSetChanged() { super.notifyDataSetChanged(); }

Quel est le problème?

+0

Vous n'avez pas modifié les données du bouton cliquez sur? Et utilisez Asynctask pour obtenir des données et des liens dans votre vue. – Oam

+0

@Oam Je reçois des données du service Web. Les données changent tout le temps, mais quand je clique sur le bouton, l'affichage de la liste ne change pas. Si vous me donnez plus d'informations sur la façon dont je devrais utiliser Asynctask, je serai heureux. – MSr

+0

Selon votre code, les données du service Web se chargent initialement et vous n'appelez pas le service Web lorsque vous cliquez sur le bouton, il semble donc qu'il ne soit pas actualisé. Mettez 'asynctask', appelez le webservice dans' doInBackground' et réglez l'adaptateur dans 'onPostExecute' et cliquez sur le bouton, appelez à nouveau votre asynctask. Vérifiez cela pour asynctask http://stackoverflow.com/questions/9671546/asynctask-android-example – Oam

Répondre

0

Je nettoie un peu votre code. Essayez ceci:

private static final String TAG_RESULTS="results"; 
private static final String TAG_TWEET="text"; 
private static final String TAG_USER_ID = "from_user_id_str"; 
private static final String TAG_USER_NAME = "from_user"; 
private static final String TAG_PIC_URL="profile_image_url"; 
private static final String TAG_TWEET_ID="id_str"; 
private static final String URL="https://api.twitter.com/1.1/search/tweets.json?q="; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tweet); 

    mListView = (ListView) findViewById(R.id.listView); 
    mRefreshButton = (ImageButton) findViewById(R.id.imageButton); 

    if (mRefreshButton != null) { 
     mRefreshButton.setOnClickListener(this); 
    } 

    if (getIntent() != null) { 
     String url = getIntent().getStringExtra("url"); 
    } 

    JSONParser jParser = new JSONParser(); 
    JSONObject json = jParser.getJSONFromUrl(url); 
    Log.i("deneme", json.toString()); 

    try { 
     results = json.getJSONArray(TAG_RESULTS); 

     tweets = new String[results.length()]; 
     user_ids = new String[results.length()]; 
     user_names = new String[results.length()]; 
     pic_urls = new String[results.length()]; 
     tweet_ids = new String[results.length()]; 

     for (int i = 0; i < results.length(); i++) { 
      JSONObject c = results.getJSONObject(i); 

      tweets[i] = c.getString(TAG_TWEET); 
      user_ids[i] = c.getString(TAG_USER_ID); 
      user_names[i] = "@"+c.getString(TAG_USER_NAME); 
      pic_urls[i] = c.getString(TAG_PIC_URL); 
      tweet_ids[i] = c.getString(TAG_TWEET_ID); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    LazyAdapter adapter = new LazyAdapter(this, pic_urls, user_names, tweets); 
    mListView.setAdapter(adapter); 
} 

@Override 
public void onClick(View view) { 
    if (view == mRefreshButton) { 
     Toast.makeText(this,"tık tık", Toast.LENGTH_LONG).show(); 
     ((LazyAdapter) mListView.getListAdapter()).notifyDataSetChanged(); 
    } 
} 
+0

Il n'est toujours pas rafraîchissant:/@baptisterobert – MSr

+1

Désolé, essayez ceci: ((LazyAdapter) mListView.getListAdapter()). NotifyDataSetChanged () – baptisterobert

Questions connexes