2016-12-03 13 views
1

我已經定製我自己ListView,其中包括:Android的轉換從的LinearLayout到CheckedTextView和TextView的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:padding="5dp"> 

<CheckedTextView 
    android:id="@+id/txt_lan" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:textSize="25sp" /></LinearLayout> 

,我希望能夠展示的是裏面有什麼文字我CheckedTextView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Toast.makeText(getApplicationContext(), (((TextView)CheckedTextView)view).getText().ToString(), Toast.LENGTH_SHORT).show(); 

因此,我認爲,查看包含對我的LinearLayout的引用。如何訪問CheckedTextView中的文字?因爲我的方式似乎並沒有工作

回答

0
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 

    CheckedTextView checkedTextView = (CheckedTextView)arg1.findViewById(R.id.checkedTextView1); 
     // do anything to checkedTextView now 
     checkedTextView.toggle(); 

        } 

怎麼看帕拉姆arg1被使用,這應該工作!

0

view參數保存適配器創建的視圖,它也應該包含您的自定義佈局(see reference)。

所以你可以通過這種方式得到你的(Checked)TextView的文本:

((TextView)view.findViewById(R.id.txt_lan)).getText().toString() 
相關問題