-1

我正在尝试使用 onclicklistener 创建一个列表视图,当用户单击列表视图上的一个项目时,它将启动一个新活动。如何将文件从列表视图传输到另一个活动?下面是我更新 json 数据和更新列表视图的代码

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

        // looping through all posts according to the json object returned
        for (int i = 0; i < mComments.length(); i++) {
             JSONObject c = mComments.getJSONObject(i);

             // gets the content of each tag
             String title = c.getString(TAG_TITLE);
             String content = c.getString(TAG_MESSAGE);
             String username = c.getString(TAG_LNAME);
             String studnum = c.getString(TAG_USERNAME);
             // creating new HashMap
             /**SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(BsitLoungeActivity.this);
             String fname = sp.getString("firstname","anon");
             String lname = sp.getString("lasstname","anon");**/

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

             map.put(TAG_TITLE, title);
             map.put(TAG_MESSAGE, content);
             map.put(TAG_LNAME, username);
             map.put(TAG_USERNAME, "ID:"+studnum);
             // adding HashList to ArrayList
             mCommentList.add(map);

             // annndddd, our JSON data is up to date same with our array
             // listw
        }

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


private void updateList() {

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


    ListView lv = getListView();    
    lv.setOnItemClickListener(new OnItemClickListener() {

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

            // This method is triggered if an item is click within our
            // list. For our example we won't be using this, but
            // it is useful to know in real life applications.

        }
    });
}  

我可以使用插入到 hashmap 中的数据吗?如何?请帮助我任何人

4

4 回答 4

0

尝试在您的列表视图中执行此操作

    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        if(position ==1){
            startActivity(new Intent(this, NewClass.class));
}
else if(position ==2){
    }
});
于 2013-09-04T13:29:34.220 回答
0

执行以下操作,

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

     Intent intent = new Intent(MainActivity.this,SecondActivity.class);
   intent.putExtra("extra_text", string); // string should contain the value which you wan to send to the next class.
   startActivity(intent);

    }

第二活动

 String text = getIntent().getStringExtra("extra_text");
于 2013-09-04T13:31:44.567 回答
0

如果您需要传递字符串值

 lv.setOnItemClickListener(new OnItemClickListener() {

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

         String s = mCommentList.get(position).get(TAG_TITLE).toString();
         Intent i = new Intent(MainActivity.this,SecondActivity.class);
         i.putExtra("key",s);
         startActivity(i);       

    }
});

如果需要传递 hashmap

lv.setOnItemClickListener(new OnItemClickListener() {

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

        Intent i = new Intent(MainActivity.this,SecondActivity.class);
        i.putExtra("arraylist",mCommentList);
        }
    });

在 SecondActivity 中将其检索为

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
于 2013-09-04T13:46:39.037 回答
0

用这个:

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

    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
    intent.putExtra("key", mCommentList.get(position).get(TAG_USERNAME));// same for others 
    startActivity(intent);


}
});

并在 onCreate() 方法的 Second Activity 中检索该数据:

Intent n = getIntent();
String i = n.getStringExtra("key");
于 2013-09-04T13:38:44.937 回答