安卓:onListItemClick没有得到所谓的ListActivity [英] Android: onListItemClick not getting called in ListActivity

查看:202
本文介绍了安卓:onListItemClick没有得到所谓的ListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的第一个Android应用程序的问题。

我已经子类ListActivity,和我有没有运气获取覆盖onListItemClick()来响应点击事件。我读过的重点可以是一个问题,但在XML文件中不断变化的重点似乎不工作。这里是code的相关位。任何人都看到发生了什么,我鸡奸了?

 公共类Notepadv1扩展ListActivity {
    私人INT mNoteNumber = 1;
    私人NotesDbAdapter mDbHelper;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.notepad_list);
        mDbHelper =新NotesDbAdapter(本);
        mDbHelper.open();
        fillData();
    } 私人无效fillData(){
    //获取所有的音符从数据库中,并创建项目列表
    光标C = mDbHelper.fetchAllNotes();
    startManagingCursor(C);    的String [] =由新的String [] {} NotesDbAdapter.KEY_TITLE;
    INT []为= INT新[] {} R.id.text1;
    SimpleCursorAdapter指出=
        新SimpleCursorAdapter(这一点,R.layout.notes_row,C,从,到);
    setListAdapter(注);
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);    AlertDialog警报=新AlertDialog.Builder(本).create();
    字符串消息=行点击!;
    alert.setMessage(消息);
    alert.show();}

notepad_list.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>  < ListView控件
      机器人:ID =@机器人:ID /列表
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:dividerHeight =6DP/>  < TextView的机器人:ID =@机器人:ID /空
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=@字符串/ no_notes/>< / LinearLayout中>

和notes_row.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< TextView的机器人:ID =@ + ID / text1中
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =60dp
    机器人:可聚焦=FALSE/>


解决方案

我创建了一个ListActivity,用你的onListItemClick和notes_row.xml。在code为我工作。不过,我相信你所期望的的整行的回应点击,根据notes_row你需要点击的文本的本身(行中没有任何地方)。

尝试改变notes_row.xml宽度属性match_parent

 < TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / text1中
    机器人:layout_height =60dp
    机器人:layout_width =match_parent
    机器人:可聚焦=FALSE/>

现在整个行响应用户点击。

I'm having problems with my first Android app.

I have subclassed ListActivity, and I'm having no luck getting the overridden onListItemClick() to respond to click events. I've read focus can be a problem, but changing focus in the XML files does not seem to work. Here's the relevant bits of code. Anyone see what's I've buggered up?

public class Notepadv1 extends ListActivity {
    private int mNoteNumber = 1;
    private NotesDbAdapter mDbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notepad_list);
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
    }

 private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor c = mDbHelper.fetchAllNotes();
    startManagingCursor(c);

    String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
    int[] to = new int[] { R.id.text1 };


    SimpleCursorAdapter notes =
        new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
    setListAdapter(notes);
}

@Override
public void onListItemClick (ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    AlertDialog alert = new AlertDialog.Builder(this).create();
    String message = "row clicked!";
    alert.setMessage(message);
    alert.show();

}

notepad_list.xml

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

  <ListView
      android:id="@android:id/list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:dividerHeight="6dp"/>



  <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes" />

</LinearLayout>

And notes_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:focusable="false"/>

解决方案

I created a ListActivity, used your onListItemClick, and notes_row.xml. The code works for me. However, I believe you are expecting the entire row to respond to a click, according to notes_row you need to click on the text itself (not anywhere in the row).

Try changing the width attribute in notes_row.xml to "match_parent":

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_height="60dp"
    android:layout_width="match_parent"
    android:focusable="false" />

Now the entire row responds to user clicks.

这篇关于安卓:onListItemClick没有得到所谓的ListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆