OnItemClickListener没有触发

时间:2015-04-13 22:35:14

标签: android listview onitemclicklistener

注册按钮运行良好,但调试器不会运行到setOnItemClickListener onItemClick。如果您单击某个项目,则应启动用于编辑该记录的活动。

package at.bomsbg.MindX;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import at.bomsbg.MindX.sqlite.helper.MindXopenHelper;
import at.bomsbg.MindX.sqlite.helper.MindXtableAdapter;

public class Activity_cardmanager extends Activity {
MindXtableAdapter mindxtableadapt;
MindXopenHelper openHelper;

ListView nameList;
Button registerBtn;
Cursor cursor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cardmanager);
    nameList = (ListView) findViewById(R.id.title);
    registerBtn = (Button) findViewById(R.id.btn_register);
    mindxtableadapt = new MindXtableAdapter(this);

    String[] from = { MindXopenHelper.title, MindXopenHelper.detail,
            MindXopenHelper.active };
    int[] to = { R.id.tv_title, R.id.tv_detail, R.id.tv_active };

    cursor = mindxtableadapt.queryName();
    SimpleCursorAdapter cursorAdapter = 
                 new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);
    nameList.setAdapter(cursorAdapter);

    nameList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> tobjParent, View tobjView,
                int tintPosition, long tlngid) {
            Bundle passdata = new Bundle();
            Cursor listCursor = (Cursor) tobjParent
                    .getItemAtPosition(tintPosition);
            int nameId = listCursor.getInt(listCursor
                    .getColumnIndex(MindXopenHelper.KEY_ID));
            passdata.putInt(MindXopenHelper.KEY_ID, nameId);
            Intent passIntent = new Intent(Activity_cardmanager.this,
                    Activity_edit_mindxtables.class);
            Toast.makeText(getApplicationContext(),
                    Integer.toString(nameId), 500).show();

            passIntent.putExtras(passdata);
            startActivity(passIntent);
        }
    });

    registerBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent registerIntent = new Intent(Activity_cardmanager.this,
                    Activity_MindXtablesRecord.class);
            startActivity(registerIntent);
        }
    });
}

@Override
public void onResume() {
    super.onResume();
    cursor.requery();

}

}

activity_Cardmanager.html

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

<Button
    android:id="@+id/btn_register"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/register" />

</LinearLayout>

row.xml 我为文本字段添加了clickable项目。我认为这不是真正的必要条件。

<?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="wrap_content"
android:clickable="true"
android:orientation="horizontal" >

<TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:width="100dp" />

<TextView
    android:id="@+id/tv_detail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:width="100dp" />

<CheckBox
    android:id="@+id/tv_active"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您的列表视图项目中有可聚焦/可点击的项目 - 复选框。

选项1 - 将android:descendantFocusability="blocksDescendants"标记设置为ListView,您应该触发onItemClickListener

选项2 - 在您的适配器中,您可以在整个列表视图项对象上设置onClickListener,并从那里处理它。

答案 1 :(得分:0)

我的解决方案问题出在row.xml上

再次超过X:

row.xml

<LinearLayout
android:clickable="true" (removed)

<TextView
    android:id="@+id/tv_title"
    android:onClick="Title" (added)
    android:clickable="true" (added)
    android:focusable="true" (added)

在我的Activity_cardmanager上,我在nameList.setAdapter ...

之后插入了以下代码
nameList.setClickable(true);            // 2015-04-14
nameList.setFocusable(true);            // 2015-04-14
nameList.setItemsCanFocus(true);        // 2015-04-14
setViewItemListener();

。 。

    public void setViewItemListener() {
        nameList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> tobjParent, View tobjView, int tintPosition, long tlngid) {
                Bundle passdata = new Bundle();
//              Cursor listCursor = (Cursor) tobjParent.getItemAtPosition(tintPosition);
                Cursor listCursor = (Cursor) nameList.getItemAtPosition(tintPosition);  //2015-04-15
                int nameId = listCursor.getInt(listCursor
                        .getColumnIndex(MindXopenHelper.KEY_ID));
                passdata.putInt(MindXopenHelper.KEY_ID, nameId);
                Intent passIntent = new Intent(Activity_cardmanager.this,
                        Activity_edit_mindxtables.class);
                Toast.makeText(getApplicationContext(),
                 Integer.toString(nameId), Toast.LENGTH_LONG).show();

                passIntent.putExtras(passdata);
                startActivity(passIntent);
            }
        });     
    }

returnValue为true,但这是另一个故事。