リストの 2 番目の項目にアクセスするにはどうすればよいですか? 私が意味するのは、文字列がこれを出力するということです:
07-17 21:15:38.723: D/MY APP(15806): {feeling=joyful}
でも「嬉しい」とだけ読みたい
これが私のコードです:
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);
}
};
感情リストに等しい文字列の感情が私に問題を与えています。配列のさまざまな部分にアクセスできるメソッドはありますか? get メソッドは両方の要素のみを返すようです。