如何访问列表中的第二个参数?
Posted
技术标签:
【中文标题】如何访问列表中的第二个参数?【英文标题】:How can I access the second argument in the list? 【发布时间】:2013-07-25 17:22:35 【问题描述】:我想知道如何访问列表中的第二项?我的意思是字符串输出这个:
07-17 21:15:38.723: D/MY APP(15806): feeling=joyful
但我只希望它读“快乐”
这是我的代码:
公共类 FeelingsMain 扩展 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);
;
等于感觉列表的字符串感觉给了我问题。是否有一些方法可以让我访问数组的不同部分,get 方法似乎只返回两个元素。
【问题讨论】:
...您有一个地图列表,并且您正在将地图转储到给定的索引处。转储地图值。还有,嗯。 不要使用 toString ... 将其转换为列表项类 .. 通过创建ArrayList
of HashMaps
不确定您在做什么。
我也没有,我只做了几个星期,在黑暗中磕磕绊绊,休息一下。
【参考方案1】:
看这个链接:Android - Get value from 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
【讨论】:
【参考方案2】:@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);
;
【讨论】:
以上是关于如何访问列表中的第二个参数?的主要内容,如果未能解决你的问题,请参考以下文章