2012-03-03 70 views
0

正如標題所說的那樣..我有一個列表視圖,其中包含3個文本視圖,請參閱下文。從自定義ListView中獲取文本

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="20dp" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="#000" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:textSize="12sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/text3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:textStyle="italic" 
     android:typeface="sans" /> 

</LinearLayout> 

這就是我所說的。

public void displayResultList(ArrayList<HashMap<String, String>> results2) { 
    // TODO Auto-generated method stub 

    lv = (ListView) findViewById(android.R.id.list); 
    SimpleAdapter adapter = new SimpleAdapter(this, results2, 
      R.layout.custom_row_view, 
      new String[] { "Ear", "Breed", "Sex" }, new int[] { R.id.text1, 
        R.id.text2, R.id.text3 }); 

    lv.setAdapter(adapter); 

} 

,這是我嘗試檢索文字,但這並不工作..

protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 

     String item = (String) ((TextView) v).getText(); 

     Toast toast3 = Toast.makeText(this, "items = " + item, 
       Toast.LENGTH_SHORT); 
     toast3.show(); 

     // Intent mainIntent3 = new Intent(this,CowDetailsActivity.class); 
     // mainIntent3.putExtra("cow", item.substring(10, 16)); 
     // startActivity(mainIntent3); 

    } 

誰能告訴我怎樣才能得到文本的第一行?

在此先感謝

回答

3

可有人告訴我,我怎樣才能得到文本的第一行?

((HashMap<String, String>)l.getItemAtPosition(position)會給你你的HashMap。我不知道「第一行文本」是什麼。

或者,如果您仍然有results2舉行某處,results2.get(position)也將返回給您您的HashMap

+1

完美答案謝謝 – 2012-03-04 10:53:55

相關問題