2014-01-31 45 views
0

當我將分析後的數據插入到列表視圖中時,添加的項目出現在底部,我想將它們放在頂部,就像在facebook上一樣,新的貼子添加在頂部。這裏是我的代碼:
我從服務器檢索我的評論。請幫我出在列表視圖頂部添加新項目

這是Readpost.java

private static final String TAG_SUCCESS = "success"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_POSTS = "posts"; 
private static final String TAG_POST_ID = "post_id"; 
private static final String TAG_USERNAME = "username"; 
private static final String TAG_MESSAGE = "message"; 

private JSONArray mComments = null; 

private ArrayList<HashMap<String, String>> mCommentList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.newsfeed); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    // loading the comments via AsyncTask 
    new LoadComments().execute(); 
} 

public void addComment(View v) { 
    Intent i = new Intent(Readpost.this, Addpost.class); 
    startActivity(i); 
} 

/** 
* Retrieves recent post data from the server. 
*/ 
public void updateJSONdata() { 
    mCommentList = new ArrayList<HashMap<String, String>>(); 
    JSONParser jParser = new JSONParser(); 

    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL); 

    try { 


     mComments = json.getJSONArray(TAG_POSTS); 
     for (int i = 0; i < mComments.length(); i++) { 
      JSONObject c = mComments.getJSONObject(i);    
      String title = c.getString(TAG_TITLE); 
      String content = c.getString(TAG_MESSAGE); 
      String username = c.getString(TAG_USERNAME); 


      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(TAG_TITLE, title); 
      map.put(TAG_MESSAGE, content); 
      map.put(TAG_USERNAME, username); 


      mCommentList.add(map); 

     } 

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

private void updateList() { 

    ListAdapter adapter = new SimpleAdapter(this, mCommentList, 
      R.layout.singlepost, new String[] { TAG_TITLE, TAG_MESSAGE, 
        TAG_USERNAME }, new int[] { R.id.title, R.id.message, 
        R.id.username }); 


    setListAdapter(adapter); 

    ListView lv = getListView(); 

    lv.scrollTo(0, 0); 


    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

     } 
    }); 
} 

這是Addpost.java

private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "message"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addpost); 

    title = (EditText)findViewById(R.id.title); 
    message = (EditText)findViewById(R.id.message); 

    mSubmit = (Button)findViewById(R.id.submit); 
    mSubmit.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
      new PostComment().execute(); 
} 


class PostComment extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(Addpost.this); 
     pDialog.setMessage("Posting..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     // Check for success tag 
     int success; 

     String post_title = title.getText().toString(); 
     String post_message = message.getText().toString(); 

     try { 
      final SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
      String value =(mSharedPreference.getString("username", "anon")); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("username", value));  
      params.add(new BasicNameValuePair("title", post_title)); 
      params.add(new BasicNameValuePair("message", post_message)); 

      Log.d("request!", "starting"); 


      JSONObject json = jsonParser.makeHttpRequest(
        POST_COMMENT_URL, "POST", params); 


      Log.d("Post Comment attempt", json.toString()); 


      success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       Log.d("Comment Added!", json.toString());  
       finish(); 
       return json.getString(TAG_MESSAGE); 
      }else{ 
       Log.d("Comment Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 

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

     return null; 

    } 

    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once product deleted 
     pDialog.dismiss(); 
     if (file_url != null){ 
      Toast.makeText(Addpost.this, file_url, Toast.LENGTH_LONG).show(); 
     } 

    } 

} 

}

+0

你可以存儲'datetime'與評論,然後嘗試獲取數據排序依據日期倒序.... –

+0

只是在mCommentList – Simo

+0

頂部u能幫助我添加它用代碼? sori我只是一個初學者 – IamAndroida

回答

3

假設你保留你的ListAdapter如下:

ListAdapter mAdapter; 

Yo你可以添加一個像這樣的新項目。

HashMap<String, String> map = new HashMap<String, String>(); 
map.put(TAG_TITLE, "New Title"); 
map.put(TAG_MESSAGE, "new content"); 
map.put(TAG_USERNAME, "someone"); 

mCommentList.add(0, map); 
mAdapter.notifyDatasetChanged(); 
+0

我將我的代碼放在哪裏?即時對不起,只是一個begginer – IamAndroida

0

使用此:

mCommentList.add(0, obj); 
listAdapter.notifyDataSetChanged(); 
listView.scrollTo(0, 0); 
相關問題