2012-05-09 12 views
2

私のリストビューでクリックされた要素から文字列データを取得しています。Android分離リスト文字列

要素には、現在の名前と名前の2つの行があります。 私のlistItemOnClick()で、私はクリックされたアイテムを取得して、それからtoString()を実行しています。私が得ているのは次のようなものです:

{current=SOMETHING, name=SOMETHING} 

私はこれらをどうやって分けるのですか?ここに私のオンクリックコードがあります:

protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    Object o = this.getListAdapter().getItem(position); 
    String current = o.toString(); 

    ((TextView) findViewById(R.id.check)).setText(current); 
} 

私は、例えば現在のものだけを表示したいと思います。ありがとう!

EDIT

マイリスト変数:

static final ArrayList<HashMap<String,String>> listItems = 
     new ArrayList<HashMap<String,String>>();; 
SimpleAdapter adapter; 

は、リストの作成:

 for(int i=0; i<num_enter; i++){ 
    final int gi = i; 
    adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view,new String[]{"name", "current"}, new int[] {R.id.text1, R.id.text2}); 
    setListAdapter(adapter); 
    HashMap<String,String> temp = new HashMap<String,String>(); 
    temp.put("name", name[i]); 
    temp.put("current", "Value: " + Integer.toString(current[i])); 
    listItems.add(temp); 
    adapter.notifyDataSetChanged(); 
    } 
+0

実際にどのような種類のオブジェクトがリストにありますか?最善のことは、 'o'が何であれ、' Object'よりもより具体的なクラスを得ることです。 –

+0

リストにはテキストしかありません。コードを追加しました – arielschon12

答えて

4

は、あなたがそのように行うことができます(醜いと将来のエラーを起こしやすいとき/場合書式の変更) - 文字列に正しい書式がない場合のエラーチェックを追加します。

String s = "{current=CURRENT, name=NAME}"; 
s = s.substring(1, s.length() - 1); //removes { and } 
String[] items = s.split(","); 
String current = items[0].split("=")[1]; //CURRENT 
String name = items[1].split("=")[1]; //NAME 

あなたの編集後、あなたにも(より良い)を書くことができるように、oは地図のようです:

Map<String, String> map = (Map<String, String>) this.getListAdapter().getItem(position); 
String current = map.get("current"); 
String name = map.get("name"); 
0

それは非常に難しいことではありません。

protected void onListItemClick(ListView l, View v, int position, long id) { 
// TODO Auto-generated method stub 
super.onListItemClick(l, v, position, id); 
Object o = this.getListAdapter().getItem(position); 
String current = o.toString(); 

// first remove the first and last brace 
current = current.substring(1,current.length()-1); 
// then split on a comma 
String[] elements = current.split(","); 
// now for every element split on = 
String[] subelements = elements[0].split("="); 
String key1 = subelements[0]; 
String value1 = subelements[1]; 

subelements = elements[1].split("="); 
String key2 = subelements[0]; 
String value2 = subelements[1]; 


((TextView) findViewById(R.id.check)).setText(current); 
} 
3

うわー、みんな長い道を歩いている。ビューから直接データを取得します。この場合のView vはレイアウト行です。そのため、findViewByIdを使って個々のtextviewsを見つけ出し、そこからテキストを取得することができます。あなたのコードを使用すると、次のようになります。

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    TextView nameTxt = (TextView) v.findViewById(R.id.Text1); 
    TextView currentTxt = (TextView) v.findViewById(R.id.Text2); 
    String name = nameTxt.getText().toString(); 
    String current = currentTxt.getText().toString(); 
} 

これは役に立ちます。

関連する問題