首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListActivity安卓

ListActivity安卓
EN

Stack Overflow用户
提问于 2013-09-30 19:17:58
回答 5查看 3.3K关注 0票数 0

有人知道为什么ListActivity不能工作吗?当我把它改成Activity基类时,只要稍加改动就能工作,但onClick()方法仍然不能工作。我只是想把一些字符串放到列表中……代码如下:

代码语言:javascript
复制
public class TestDataBaseActivity extends Activity implements OnClickListener {
private CommentsDataSource datasource;
ListView m_list;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();
    m_list = (ListView) findViewById(R.id.listView1);
    // Use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, values);
    m_list.setAdapter(adapter);

    Button add = (Button)findViewById(R.id.button_add);
    Button delete = (Button)findViewById(R.id.button_delete);

    add.setOnClickListener(this);
    delete.setOnClickListener(this);

}

// Will be called via the onClick attribute
// of the buttons in main.xml
@Override
public void onClick(View view) {
    @SuppressWarnings("unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter();
    Comment comment = null;
    switch (view.getId()) {
        case R.id.button_add:
            String[] comments = new String[] {
                    "Cool", "Very nice", "Hate it"
            };
            int nextInt = new Random().nextInt(3);
            // Save the new comment to the database
            comment = datasource.createComment(comments[nextInt]);
            adapter.add(comment);
            break;
        case R.id.button_delete:
            if (m_list.getAdapter().getCount() > 0) {
                comment = (Comment) m_list.getAdapter().getItem(0);
                datasource.deleteComment(comment);
                adapter.remove(comment);
            }
            break;
    }
    adapter.notifyDataSetChanged();
}

@Override
protected void onResume() {
    datasource.open();
    super.onResume();
}

@Override
protected void onPause() {
    datasource.close();
    super.onPause();
}

}

下面是XML:

代码语言:javascript
复制
<RelativeLayout 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"
tools:context=".TestDataBaseActivity" >

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

<Button
    android:id="@+id/button_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/button_add"
    android:text="@string/delete_first" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/button_add"
    android:padding="15dp" >

</ListView>

</RelativeLayout>

我现在编辑以扩展活动,现在“唯一”onClick()不起作用……提前感谢!

EN

回答 5

Stack Overflow用户

发布于 2013-09-30 19:30:02

对于ListActivity,您应该改写onListItemClick()

代码语言:javascript
复制
@Override
protected void onListItemClick(ListView lv, View v, int position, long id){


}
票数 1
EN

Stack Overflow用户

发布于 2013-10-01 12:19:37

由于您要扩展ListActivity,因此需要具有带有android:id="@android:id/list".Listview

如果你想使用@+id/lst_id,那么不要扩展ListActivity,只需扩展Activity即可。

而且,当你使用Listview时,你需要实现OnItemClickListener,而不是OnClickListener

并重写默认的OnItemClick方法

代码语言:javascript
复制
@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }
票数 1
EN

Stack Overflow用户

发布于 2013-09-30 19:20:20

您应该实现OnClickListener

代码语言:javascript
复制
public class TestDatabaseActivity extends ListActivity implements
    OnClickListener{
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19092862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档