简体   繁体   中英

ListActivity Android

Does somebody know why ListActivity won't work and when I change it to Activity base class, with slight changes it works, but onClick() method still doesn't. I'm trying only to put some strings into the list... Here's the code:

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();
}

}

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

I edited now to extend Activity, now "only" onClick() dont work... Thanks in advance!

For a ListActivity, you should override onListItemClick() instead:

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


}

Since you are extending ListActivity you need to have an Listview with android:id="@android:id/list".

if you want to use @+id/lst_id then dont extend ListActivity, just extended Activity.

And whenever you are using Listview you need to implement OnItemClickListener not OnClickListener .

and override the default OnItemClick method

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

    }

you should implement OnClickListener

public class TestDatabaseActivity extends ListActivity implements
    OnClickListener{

try following code a simple list activity

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

Use this code for list activity and modify row xml according to your needs. hope this helps..

This is what's troubling you:

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

Build a global object for your adapter instead. ( Declare it before onCreate() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM