无法将正确的ID输入Fragment

时间:2014-10-10 07:12:20

标签: android json onitemclicklistener onitemclick

我需要通过行点击ex来传输从JSON实体解析的id的值。(listview中的第一项具有861的id(从Json获得)第二个823,我需要将正确的id传递给FrafmentB

这就是我获取ID的方式

List<NameValuePair> param = new ArrayList<NameValuePair>();
 JSONObject json = jsonParser.makeHttpRequest(URL_MESSAGES, "GET", param, token);
 JSONObject data = json.getJSONObject("data");
 JSONArray messages = data.getJSONArray("messages");
for(int i=0;i<messages.length();i++){
JSONObject c = messages.getJSONObject(i);
read_status=c.getString(TAG_READ_STATUS);
if(read_status.equals("unread")){
msgID=c.getInt("id");
Log.d("BUNDLE ID", String.valueOf(msgID));
title = c.getString(TAG_TITLE);
Log.d("TITLE",title);
time=c.getString(TAG_TIME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TITLE, title);
map.put(TAG_TIME, time);

mailList.add(map);

这样我将id转移到片段

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm=getFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
bundle.putInt("message", msgID );
MessageView ViewMessages=new MessageView();
ViewMessages.setArguments(bundle);
transaction.replace(R.id.fragmentInnerMail,ViewMessages);
transaction.commit();
}

当用户点击行中的项目时,只有最后一个id(通过asyncTask解析)进入fragmentB,而不是基于listview中的行的idd,我无法弄清楚,请帮助

适配器

  @Override
        protected void onPostExecute(String s) {
            ListView list = (ListView) getActivity().findViewById(R.id.list);
            ListAdapter adapter = new SimpleAdapter(
                    getActivity(), mailList,
                    R.layout.mail_row, new String[] { TAG_TITLE,
                    TAG_TIME}, new int[] {
                    R.id.mTitle,R.id.mDate});

            list.setAdapter(adapter);


            pDialog.dismiss();

        }

1 个答案:

答案 0 :(得分:0)

您需要将ID添加到HashMap

map.put(TAG_ID, msgID);

onItemClick你可以通过这种方式获得你按下的项目:

HashMap<String, String> itemAtPostion =  parent.getItemAtPosition(position);

然后

   bundle.putInt("message", itemAtPostion.get(TAG_ID) );