具体OnItemClick Android的意图 [英] Specific OnItemClick Intent Android

查看:249
本文介绍了具体OnItemClick Android的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下面的情况发生(一个简单的想法):我在主要活动单击Add按钮,输入一些信息(进入EditText上盒),并返回到主屏幕。在这里,我要显示一个列表,只有两个冠军(不是所有的信息)。当我点击列表项目,我想表明该项目的相应的保存的信息。因此,每个项目的信息将是不同的。

I want the following to happen (a simple idea): I click an Add button in my main activity, enter some info(into EditText boxes), and go back to the main screen. Here, I want to display a list, with just two titles(not all the info). When I click that list Item, I want to show the corresponding saved info for that item. Therefore, each item's info is going to be different.

当我这样做,我用startActivityForResult()从主屏幕,然后回来。这完全适用于一个项目。当我添加另一个项目,问题就出现了。无论我点击哪个项目,所显示的信息是相同的。总之,我需要找到一种方法来保存信息唯一的每个项目。

When I do this, I use startActivityForResult() from the main screen then back. This works perfectly for one item. When I add another item, the problem arises. No matter which item I click, the info displayed is the same. In short, I need to find a way to save that info unique to each item.

截至目前,我有以下code片断:

As of now, I have the following code snippet :

 //After I add all the information in the second intent

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 1) {
        //these display in the list (Each list item has two textfields,row1 and row 2)

        row1 = data.getStringExtra("com.painLogger.row1");
        row2 = data.getStringExtra("com.painLogger.row2");

           //below is the other info entered

        painLevelString = data.getStringExtra("com.painLogger.painLevel");
        painLocation = data.getStringExtra("painLocation");
        timeOfPainString = data.getStringExtra("com.painLogger.painTime");
        textTreatmentString = data
                .getStringExtra("com.painLogger.treatment");
        addItem();
    }
}

    //When I click the item-- this is the info that is not unique...
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    Intent intent = new Intent(this, Item1.class);
    intent.putExtra("com.painLogger.painLevel", painLevelString);
    intent.putExtra("com.painLogger.painTime" , timeOfPainString);
    intent.putExtra("com.painLogger.treatment", textTreatmentString);
    intent.putExtra("painLocation", painLocation);
    startActivity(intent);
}

// 编辑:添加项目$ C $下加入

private void addItem() {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("row_1", row1);
    map.put("row_2", row2);
    painItems.add(map);
    adapter.notifyDataSetChanged();

}

// *的编辑:一些定义*

SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();

我能理解为什么这是我occuring--单击一个项目每一次,目的是把过去的额外费用,这意味着最后一个对象的信息补充。怎样使每个OnItemClick独特之处?我怀疑我将不得不使用位置变量。

I can understand why this is occuring-- every time I click an item, the intent is putting the last extra, meaning the last object's info added. How do I make each OnItemClick unique? I suspect I will have to use the position variable.

推荐答案

两项建议:

将数据传递到你的方法,而不是使用类字段:

Pass the data to your method instead of using class fields:

addItem(String row1, String row2...){...}

另外,你想抓住你的数据出来的图:

Also, you want to grab your data out of the map:

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    Intent intent = new Intent(this, Item1.class);
    intent.putExtra("com.painLogger.painLevel", painItems.get(position).get("row_1"));
    intent.putExtra("com.painLogger.painTime" , painItems.get(position).get("row_2"));
    intent.putExtra("com.painLogger.treatment", painItems.get(position).get("row_3"));
    intent.putExtra("painLocation", painItems.get(position).get("row_4"));
    startActivity(intent);
}

您当然会需要把row_3和row_4进入地图,你创建它,我相信你还没有做的事情。

You will of course need to put "row_3" and "row_4" into the map as you create it, which I believe you are not yet doing.

DMON建议在你创建一个类,再presents数据不同的答案一个评论,他是正确的,它是更好的设计,有助于节省这些问题。要做到这一点,你需要看看Parcelable接口能够通过意图传递你的类。你的设计工作,但它会很难理解为未来的家伙(包括您在6个月)和更难以扩展。

DMon suggested in a comment in a different answer that you create a class that represents your data, and he is right, it is much better design and helps save on these problems. To do so you will need to look at the Parcelable interface to be able to pass your class through Intents. Your design will work, but it will be very difficult to understand for the next guy (including you in 6 months) and much more difficult to extend.

这篇关于具体OnItemClick Android的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆