在listview上添加新项目

时间:2014-01-31 08:56:23

标签: android android-listview listadapter

当我将解析后的数据插入列表视图时,添加的项目显示在底部我希望将它们放在顶部,就像在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();
        }

    }

}

}

2 个答案:

答案 0 :(得分:3)

假设您保留ListAdapter,如下所示:

ListAdapter mAdapter;

您可以添加这样的新项目。

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();

答案 1 :(得分:0)

使用此:

mCommentList.add(0, obj);
listAdapter.notifyDataSetChanged();
listView.scrollTo(0, 0);