0

我有一个列表视图。我需要点击列表视图中的每个元素。但onItemClickListener不工作。我尝试将每个 textViewfocusable="false"放入并放入 android:descendantFocusability="blocksDescendants",但没有任何效果,我不知道我的错误在哪里。这是我的代码

listview中每个元素的xml

  **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>

活动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();
                }   
            }       
    );

任何人都可以帮助我吗?谢谢你的时间。

4

3 回答 3

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();


    }
});
于 2013-01-21T11:24:05.237 回答
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"

希望这可以帮助 :)

于 2013-01-21T11:11:52.503 回答
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>
于 2013-01-21T13:20:33.347 回答