setOnItemClickListener()不在ListView上工作(总是选择第一行)

时间:2016-05-08 12:48:34

标签: android listview

我通过为每一行(Contact)扩展LinearLayout创建了一个自定义ListView,我需要选择项目,但方法" setOnItemClickListener()"不工作我刚刚放了一个onItemSelectedListener,现在是方法" setOnItemClickListener"尽管我选择了其他行

,但总是选择第一项

MainActivity:

public class PhoneBookAdapter extends BaseAdapter{

private Context mContext;
private List<PhoneBook> mListPhoneBook;

public PhoneBookAdapter (Context context, List<PhoneBook> list) {
    mContext = context;
    mListPhoneBook = list;
}
@Override
public int getCount() {
    return mListPhoneBook.size();
}
@Override
public Object getItem(int position) {
    return mListPhoneBook.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PhoneBook entry = mListPhoneBook.get(position);

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(R.layout.phonebook_row,null);
    }

    ImageView ivAvatar = (ImageView)convertView.findViewById(R.id.imgAvatar);
    ivAvatar.setImageBitmap(entry.getmAvatar());

    TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
    tvName.setText(entry.getmName());

    TextView tvPhone = (TextView)convertView.findViewById(R.id.tvPhone);
    tvPhone.setText(entry.getmPhone());

    TextView tvEmail = (TextView)convertView.findViewById(R.id.tvEmail);
    tvEmail.setText(entry.getmEmail());

    TextView tvID = (TextView)convertView.findViewById(R.id.tvID);
    tvID.setText(entry.getmID());

    return convertView;
}

 }

PhoneBookAdapter:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">

<ImageView
    android:id="@+id/imgAvatar"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:scaleType="fitCenter"
    android:src="@drawable/image"
    android:clickable="true"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:clickable="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:textStyle="bold"
        android:clickable="true"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvPhone"
        android:textStyle="bold"
        android:clickable="true"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvEmail"
        android:textStyle="bold"
        android:clickable="true"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvID"
        android:textStyle="bold"
        android:clickable="true"/>

</LinearLayout>

</LinearLayout>

`

PhoneBook_row:

[C, h1] = contourf(X, Y, log(Z)); 
clabel(C,h1);

3 个答案:

答案 0 :(得分:0)

PhoneBook_row.xml替换为我在这里放置的android:clickable="true",我已经尝试了这个并且它对我有用。我已将您的所有android:clickable="false"更改为ListView。这样做的原因是,您的所有子视图都使用了click,结果是您的父视图,即<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false"> <ImageView android:id="@+id/imgAvatar" android:layout_width="70dp" android:layout_height="70dp" android:scaleType="fitCenter" android:src="@drawable/image" android:clickable="false"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:clickable="false"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvName" android:textStyle="bold" android:clickable="false"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvPhone" android:textStyle="bold" android:clickable="false"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvEmail" android:textStyle="bold" android:clickable="false"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvID" android:textStyle="bold" android:clickable="false"/> </LinearLayout> </LinearLayout> 没有获得click事件。希望这会有所帮助。

<!doctype html>
<html lang="en">
     <head>
      <meta charset="utf-8">
      <title>jQuery UI Datepicker - Display month &amp; year menus</title>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

      <link rel="stylesheet" href="css.css">

      <script>
      $(function() {
        $( "#datepicker" ).datepicker({
          changeMonth: true,
          changeYear: true
        });
      });

      $(document).on('click', '#bta', function () {

        if ($('#datepicker').val() != '' && $('#evento').val() != '' ) {
           $('#caixa').prepend('<p>' + $('#datepicker').val() + '&nbsp;' + $('#evento').val() + '&nbsp;' + '<input type="button" id="apagar" class="apagar" value="apagar"/>' + '</p>');
            $('input').val('');
        }

    });

     $(document).on('click', '#apagar', function () {
       $( "p" ).remove();
       });

      </script>
    </head>

    <body>
        <div id="box">
            Lista de Tarefas:
            <br><br>
            Date: <input type="text" id="datepicker"> &nbsp; &nbsp;

            Event: <input type="text" id="evento">    <button id="bta" class="bta">+</button>

            <div id="caixa">  </div>
            </body>
        </div>

</html>

答案 1 :(得分:0)

看起来你错过了覆盖。 在函数前添加 @Override ,如下所示。

   lvPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            }
        });

在布局文件中添加android:clickable = true也不是必需的。

答案 2 :(得分:0)

你已经改变了你的问题,无论如何我相信你现在可以让所选择的项目听众工作了。要获取所选项目,您可以使用以下位置

     lvPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                PhoneBook phoneBook = listPhoneBook.get(position);                 
                Toast.makeText(MainActivity.this, phoneBook.getName() , Toast.LENGTH_SHORT).show(); 
//where getName is a function to get the name in the phonebook class
            }
        });