2014-10-30 43 views
0

我遇到了很多麻煩refreshing a list viewcustom adapter。我似乎無法找到任何解決方案,這將使我的列表視圖刷新。我試過notifyDataSetChanged,還有listView.invalidate,但似乎沒有任何工作。任何幫助將大大appreaciated。謝謝如何更新或刷新列表視圖

以下是代碼。

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Get extra data included in the Intent 
     Toast.makeText(getApplicationContext(), "Update Chat Ui", 
       Toast.LENGTH_LONG).show(); 
     commentList.invalidateViews(); 
     commentList.setAdapter(adapter); 
     adapter.notifyDataSetChanged(); 
     //adapter.notifyDataSetInvalidated(); 
    // adapter.notifyDataSetChanged(); 

    } 
}; 

protected void onPause() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(
      mMessageReceiver); 
    super.onPause(); 
}; 

異步任務onPOST等():

@Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     try { 
      adapter = new ListViewAdapter(PrivateMessages.this, arraylist); 

      commentList.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 

     } catch (Exception e) { 
     } finally { 

     } 

    } 

自定義適配器:

public class ListViewAdapter extends BaseAdapter { 

// Declare Variables 
UserFunctions mUserFunctions; 
Context context; 
ArrayList<HashMap<String, String>> data; 

Preferences mPreferences; 
public static int actualPosition; 
private static HashMap<String, String> resultp = new HashMap<String, String>(); 

boolean likeState; 

public ListViewAdapter(Context context, 
     ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data = arraylist; 

    mPreferences = new Preferences(context); 
    mUserFunctions = new UserFunctions(); 

} 

@Override 
public int getCount() { 
    return data.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // Declare Variables 

    ViewHolder holder = new ViewHolder(); 
    likeState = false; 
    resultp = data.get(position); 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (resultp.get("message_admin").equals(mPreferences.getProfileId())) { 

     convertView = inflater.inflate(R.layout.comment_listview_item_user, 
       null); 
    } else { 
     convertView = inflater 
       .inflate(R.layout.comment_listview_item, null); 

    } 

    holder.userComment = (TextView) convertView 
      .findViewById(R.id.comment_item); 
    holder.commentTime = (TextView) convertView 
      .findViewById(R.id.time_of_comment); 

    holder.userComment.setText(resultp.get("message")); 
    holder.commentTime.setText(resultp.get("message_time")); 


    return convertView; 
} 

static class ViewHolder { 
    TextView userComment; 
    TextView commentTime; 

} 
} 
+0

爲什麼你總是在做convertView = inflater.inflate?列表視圖的重點是它使用循環視圖,您應該在檢查convertview是否爲null之前檢​​查其是否爲空。這可能不直接與您的問題相關,但您仍應該修復它。 – JanBo 2014-10-30 07:59:08

+0

@JanBo謝謝...我知道這一點,但不知怎的,我需要在不同的條件下充氣2種不同的佈局。這就是爲什麼我厭倦它。另外做另一個事情就是給我一個空指針。那麼,我仍然會嘗試再次這樣做。 – addy123 2014-10-30 08:11:22

+0

你可以膨脹不同的佈局,並仍然使用covertView;)只需要更多一點就可以了。 – JanBo 2014-10-30 08:15:43

回答

0

感謝所有,但我終於可以通過下面的代碼來解決這個問題:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     new InitiateChatTask().execute(wish_user_id, 
       mPreferences.getProfileId()); 
     commentList.invalidateViews();   

    } 
}; 
0

你可以做以下。

  1. arrayList.clear();
  2. ArrayList的=新名單。
  3. commentList.invalidateViews();
  4. adapter = new ListViewAdapter(PrivateMessages.this,arraylist); commentList.setAdapter(adapter);