4

我正在開發中,我創建了一個自定義列表視圖的應用定製列表視圖的文本:Android的列表視圖 - 獲取

列表視圖XML代碼:

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_margin="15dip" 
    android:divider="#623b42" 
    android:dividerHeight="0.5dip" 
    android:cacheColorHint="#00000000" 
    android:overScrollMode="never" > 
</ListView> 

和適配器XML是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/textview1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/textview_rupee_mlsaa" 
     android:gravity="center_vertical" 
     android:padding="15dip" 
     android:textColor="@color/color_black" 
     android:textSize="15sp" 
     tools:ignore="SelectableText" /> 

    <TextView 
     android:id="@+id/textview2" 
     android:layout_width="60sp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:gravity="center_vertical" 
     android:padding="15dip" 
     android:textColor="@color/color_black" 
     android:textSize="15sp" 
     tools:ignore="SelectableText" /> 

</RelativeLayout> 

適配器的java文件是:

public class ListAdapter extends ArrayAdapter<String> { 
/** Global declaration of variables. As there scope lies in whole class. */ 
private Context context; 
private String[] dataset1; 
private int[] dataset2; 

    /** Constructor Class */ 
    public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) { 
     super(context,R.layout.list_adapter_layout,ListArray); 
     this.context = context; 
      this.dataset1= ListArray1; 
      this.dataset2= ListArray2; 
    } 

    /** Implement getView method for customizing row of list view. */ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
     // Creating a view of row. 
     View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false); 

     TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);  
     TextView tv2 = (TextView)rowView.findViewById(R.id.textview2); 

     tv1.setText(data1[position]);  
     tv2.setText(""+data2[position]); 

     return rowView; 
    } 
} 

然後我設置項目點擊監聽器列表查看方式:

listView = (ListView) findViewById(R.id.listview); 
listView.setOnItemClickListener(this);  

ListAdapter adapter = new ListAdapter(this,list1,list2); 
listView.setAdapter(adapter); 

然後我就項目點擊使用onItemClickListener爲:

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 
name = (String) arg0.getItemAtPosition(position);  
} 

我在這裏獲取自定義列表視圖中的第一個文本瀏覽的價值。但我也想要第二個文本視圖的價值。如果我添加更多,然後如何獲得該值。

請建議我這樣做。

+0

如果一些問題的答案幫助,請接受他們,所以這個問題不會出現未答覆。 – 2013-03-20 11:25:01

+0

看到多種方式的getText的ListView http://stackoverflow.com/a/28479593/3496570 – Nepster 2015-02-12 14:11:45

回答

17

試着像這裏面的烏爾OnItemClick

TextView text = (TextView) arg1.findViewById(R.id.urtextID); 
          ^^^^^^ 
String tEXT = text.getText().toString(); 
+0

這始終顯示的第一個項目文本,而不是一個在你把那個正確的點擊 – Raa 2014-01-12 06:32:57

+0

ARG1即查看變量是很重要的位置在你的實施中......? – 2014-01-12 07:55:40

1

只是一個簡化版本,上面的回答的

public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 
    // code in the commented section works in case of normal single textView 

    // TextView temp=(TextView) view; 
    // Toast.makeText(this, 
    // temp.getText()+" is pressed "+position,Toast.LENGTH_SHORT 
    //).show(); 

    TextView temp = (TextView) view.findViewById(R.id.textView1); 

    String str = temp.getText().toString(); 
    Toast.makeText(this, 
      str + " is pressed " + position, 
      Toast.LENGTH_SHORT).show(); 
}