ListActivity Android

时间:2013-09-30 11:17:58

标签: android listactivity

有人知道为什么ListActivity不起作用,当我将其更改为Activity基类时,稍作更改就可以了,但onClick()方法仍然没有。我只想把一些字符串放到列表中......这是代码:

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:

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

我现在编辑扩展Activity,现在“只”onClick()不工作... 提前谢谢!

5 个答案:

答案 0 :(得分:1)

对于ListActivity,您应该改为覆盖onListItemClick()

@Override
protected void onListItemClick(ListView lv, View v, int position, long id){


}

答案 1 :(得分:1)

由于您要延长ListActivity,因此您需要Listview android:id="@android:id/list".

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

无论何时使用Listview,您都需要实施OnItemClickListener而不是OnClickListener

并覆盖默认的OnItemClick方法

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

答案 2 :(得分:0)

你应该实现OnClickListener

public class TestDatabaseActivity extends ListActivity implements
    OnClickListener{

答案 3 :(得分:0)

尝试使用简单列表活动代码

public class MainActivity extends ListActivity implements OnItemClickListener {

ListView listView;
static final String[] SPORTS = {"Shuttle Badminton", "Tennis", "FootBall",
    "Basket Ball","Table Tennis", "Chess","Hockey"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    listView = getListView();

    //setting adapter
    listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,SPORTS));
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    TextView tv = (TextView)view.findViewById(R.id.text1);
    Toast.makeText(getApplicationContext(),"Position is: "+position,Toast.LENGTH_SHORT).show();
    Toast.makeText(getApplicationContext(),"Text is: "+tv.getText().toString(),Toast.LENGTH_SHORT).show();
}
}

here list_item is an xml having following code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/black"
android:padding="10dp" />

将此代码用于列表活动,并根据需要修改行xml。 希望这会有所帮助..

答案 4 :(得分:0)

这令你不安:

ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter();

为您的适配器构建一个全局对象。 (在onCreate()之前声明它)