2013-01-21 94 views
0

我有一個列表視圖。我需要點擊列表視圖中的每個元素。但onItemClickListener不起作用。我試着把每個textView與focusable="false"並把android:descendantFocusability="blocksDescendants"但沒有任何作品,我不知道我的錯誤在哪裏。 這裏是我的代碼裏面的ListView每個元素OnItemClickListener不工作

**xml_item_list** 
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="5dip" 
    android:paddingBottom="8dip" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:background="@android:drawable/list_selector_background" 
    android:clickable="true" 
    android:longClickable="true" 
    android:descendantFocusability="blocksDescendants"> 

<TextView 
    android:id="@+id/lDate" 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="3dp" 
    android:background="@drawable/et_style" 
    android:gravity="center" 
    android:textColor="#c2c2c2" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:focusable="false" /> 

<TextView 
    android:id="@+id/lDescription" 
    android:layout_width="50dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:background="@drawable/et_style" 
    android:textColor="#c2c2c2" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:focusable="false"/> 

<TextView 
    android:id="@+id/lMissValue" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="3dp" 
    android:gravity="center_horizontal|center" 
    android:layout_gravity="center" 
    android:textColor="#DDCC2EFA" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:focusable="false"/> 
    </TableRow> 

    **main_xml_list** 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background_tile" 
    android:orientation="vertical" > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginBottom="50dp"> 

    <ImageView 
     android:id="@+id/bankTransaction" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false"/> 

    <TextView 
     android:id="@+id/contextLabel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/et_style" 
     android:textSize="28dp" 
     android:layout_gravity="bottom" /> 
</LinearLayout> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:descendantFocusability="blocksDescendants"> 

    </ListView> 

</LinearLayout> 

代碼

XML中的onCreate活動()函數 的onCreate()

listView = getListView(); 
    listView.setClickable(true); 
    listView.setOnItemClickListener(
     new OnItemClickListener() 
      { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View view,int position, long id) {      
        Log.w(TransactionView.class.getName(), "Item click listener"); 
        Toast.makeText(TransactionView.this, "Item clicado", Toast.LENGTH_LONG).show(); 
       } 
      }  
    ); 

任何人都可以幫我嗎? 謝謝你的時間。

回答

0

我的解決辦法.....

public class TransactionView extends ListActivity implements **AdapterView.OnItemClickListener**{} 

**onCreate()**{ 
     adapter = new SimpleCursorAdapter(
      this, 
      R.layout.transaction_item_view, 
      c, 
      new String[]{MySQLiteHelper.C_TRANSACTION_DUE, MySQLiteHelper.C_TRANSACTION_DESCRIPTION, MySQLiteHelper.C_TRANSACTION_VALUE, MySQLiteHelper.C_TRANSACTION_COVERVALUE, MySQLiteHelper.C_TRANSACTION_STATE, MySQLiteHelper.C_TRANSACTION_SOURCE}, 
      new int[]{ R.id.lDate, R.id.lDescription, R.id.lMissValue},SimpleCursorAdapter.IGNORE_ITEM_VIEW_TYPE); 

    } 

    setListAdapter(adapter);  
    listView = getListView(); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
      { 
       @Override 
       public void onItemClick(AdapterView<?> arg0, View view,int position, long id) { 

        Log.w(TransactionView.class.getName(), "Item click listener"); 
        Toast.makeText(TransactionView.this, "Item clicado", Toast.LENGTH_LONG).show(); 
       } 
      } 
     ); 

     **item_list_row** 
     <TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="5dip" 
    android:paddingBottom="8dip" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:background="@android:drawable/list_selector_background" 
> 

<TextView 
    android:id="@+id/lDate" 
    android:layout_width="150dp" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="3dp" 
    android:background="@drawable/et_style" 
    android:gravity="center" 
    android:textColor="#c2c2c2" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    /> 

<TextView 
    android:id="@+id/lDescription" 
    android:layout_width="50dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:background="@drawable/et_style" 
    android:textColor="#c2c2c2" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    /> 

<TextView 
    android:id="@+id/lMissValue" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="3dp" 
    android:gravity="center_horizontal|center" 
    android:layout_gravity="center" 
    android:textColor="#DDCC2EFA" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    /> 

**main_xml_list** 
<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/background_tile" 
    android:orientation="vertical" > 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    </ListView> 

</LinearLayout> 
0

首先,檢查您是否在代碼中添加了這一行。

public class MyActivity extends Activity implements OnItemClickListener 

然後在你的的onCreate()活動添加此代碼...

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

    @Override 
    public boolean onItemClick(AdapterView<?> parent, View v, int pos,long id) 
    { 
     System.out.println("~~~~~~Item Clicked"); 
     return false; 
    } 

改變這種::

android:id="@+id/list" 

希望這有助於:)

+0

你好AndroidLearner。謝謝你的回覆。但不工作....我不明白原因。 –

+0

你在做什麼getListView()? – AndroidLearner

+0

也發佈你的main.xml :) – AndroidLearner

1

嘗試像這樣

lvlList = (ListView)findViewById(R.id.lvlList); 
lvlList.setOnItemClickListener(new OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> a, View v,int position, long id) 
    { 
     Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show(); 


    } 
}); 
相關問題