-2

I'm wondering how I can access second item in my list? What I mean is that the string outputs this :

07-17 21:15:38.723: D/MY APP(15806): {feeling=joyful}

But I only want it to read "joyful"

Here is my code:

public class FeelingsMain extends Activity {

private static final String TAG = "MY APP";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initList();
    ListView lv = (ListView) findViewById(R.id.pageLayout);
    // design the listview with the adapter
    SimpleAdapter simpleAdpt = new SimpleAdapter(this, feelingsList,
            android.R.layout.simple_list_item_1,
            new String[] { "feeling" }, new int[] { android.R.id.text1 });

    lv.setAdapter(simpleAdpt);

    lv.setOnItemClickListener(viewNeeds);

}

List<Map<String, String>> feelingsList = new ArrayList<Map<String, String>>();

private void initList() {
    // populate the feelingsList
    feelingsList.add(createFeeling("feeling", "wonderful"));
    feelingsList.add(createFeeling("feeling", "content"));
    feelingsList.add(createFeeling("feeling", "joyful"));
    feelingsList.add(createFeeling("feeling", "tired"));
    feelingsList.add(createFeeling("feeling", "gay"));
    feelingsList.add(createFeeling("feeling", "sad"));
    feelingsList.add(createFeeling("feeling", "amazing"));

}

private HashMap<String, String> createFeeling(String key, String name) {
    HashMap<String, String> feeling = new HashMap<String, String>();
    feeling.put(key, name);

    return feeling;

}

OnItemClickListener viewNeeds = new OnItemClickListener() {


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        // create variable for database input
        String feeling = feelingsList.get(arg2).toString();
        Log.d(TAG, feeling);

        Intent gotoNeeds = new Intent(FeelingsMain.this, Needs.class);
        gotoNeeds.putExtra("aFeeling", feeling);
        startActivity(gotoNeeds);
    }

};

The string feeling which is equal to feelingsList is giving me the problem. Is there some method that allows me to access different parts of the array, the get method only seems to return both elements.

4

2 回答 2

1

看这个链接:Android - 从 HashMap 中获取价值

Map<String, String> map = new HashMap<String, String>();
map.put("Color1","Red");
map.put("Color2","Blue");
map.put("Color3","Green");
map.put("Color4","White");

 System.out.println(map);
 // {Color4=White, Color3=Green, Color1=Red, Color2=Blue}        

  System.out.println(map.get("Color2")); // Blue

 System.out.println(map.keySet());
  // [Color4, Color3, Color1, Color2]

for (Map.Entry<String,String> entry : map.entrySet()) {
   System.out.printf("%s -> %s%n", entry.getKey(), entry.getValue());
}
// Color4 -> White

// 颜色 3 -> 绿色 // 颜色 1 -> 红色 // 颜色 2 -> 蓝色

The credit is for @polygenelubricants
于 2013-07-25T17:14:06.933 回答
0

@Tobiel 和其他人是对的:使用 HashMap.getValue(key) 方法输出值。在您的情况下,它将是:

OnItemClickListener viewNeeds = new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    // create variable for database input
    String feeling = feelingsList.get(arg2).get("feeling").toString();  // <<- changes here
    Log.d(TAG, feeling);

    Intent gotoNeeds = new Intent(FeelingsMain.this, Needs.class);
    gotoNeeds.putExtra("aFeeling", feeling);
    startActivity(gotoNeeds);
}

};
于 2013-07-25T17:23:33.233 回答