0

Ok guys, so as an Android newbie Im working my way through various things. My issue here is Im trying to start a new intent activity in a 'onListItemClick'. I have the method set up for this but when i run the AVD upon clicking the item in the list, nothing happens, the intent doesn't start.

If I change the the super class to 'ListActivity' I get a crash message of 'your content must have a listview whose id attribute is 'android.r.id.list''

Can anyone tell me why the intent wont start from clicking the item in the listview? Here's my listview class:

package com.example.sqliteexample;



import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class SQLView extends ListActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    HotOrNot H = new HotOrNot(this, null, null);

    setContentView(R.layout.layout);
    ListView listContent = (ListView)findViewById(R.id.contentList);

    HotOrNot Content = new HotOrNot(this, null, null);
    Content.open();
    Cursor cursor = Content.getData();

    startManagingCursor(cursor);

    @SuppressWarnings("static-access")
    String [] from = new String [] {H.KEY_NAME, H.KEY_HOTNESS};
    int [] to = new int [] {R.id.txtName, R.id.txtAge};

    @SuppressWarnings("deprecation")
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.entries, cursor, from, to);

    listContent.setAdapter(cursorAdapter);
 }


public void onListItemClick(ListView l, View v, int posistion, long id)
{

    Intent i = new Intent("com.example.sqliteexample.SQLiteExample");

    startActivity(i);
}




}
user1352057
  • 3,162
  • 9
  • 51
  • 117

5 Answers5

2

if you're gonna use a ListActivity then you don't need this line:

ListView listContent = (ListView)findViewById(R.id.contentList);

BUT that specific ListView being referred to right now (provided it's in the corresponding xml layout) must have it's id changed to

<ListView
  android:id="@android:id/list"
.....
mango
  • 5,577
  • 4
  • 29
  • 41
  • mango, have you got any idea why when i change my id to this, i cant get my R file back? – user1352057 Jan 11 '13 at 18:00
  • try cleaning your project. honestly the reason could for any particular reason, most probably a mis-spelling. you can look in this post for any ideas that you haven't thought of yet. http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error – mango Jan 11 '13 at 18:51
0

I get a crash message of 'your content must have a listview whose id attribute is 'android.r.id.list''

This happenes when you extend ListActivity but there is no attribute list in xml (in your case layout.xml). Read this example on how to create ListActivity.

Extend activity or listactivity?

Answer for this question depends on your requirements.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

Inside your layout.xml file adjust your ListView element to have something similar to below: the most important is android:id line

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>
Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • Javacadabra, if i do that (which i have done in the past, then its not recognized in my sqlView class? – user1352057 Jan 11 '13 at 17:40
  • Mangos answer above should work. You don't need to create a new list. By extending to ListActivity your Activity will no by default which list you want to use, providing you assign it the id of "list" in the xml. – Javacadabra Jan 11 '13 at 17:45
0
listContent.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {

                Intent i = new Intent("com.example.sqliteexample.SQLiteExample");
                startActivity(i);
        }
    });
Daniel Uribe
  • 778
  • 4
  • 17
0

Activity - Use for normal purpose ListActivity - Use where your more concentration is on displaying the lists.

Advantages of ListActivity over Activity:

  1. If there is only textual display of list then there is even no need of defining layout file and hence no setContentView() to call.
  2. If we need to display something else(say TextView) other than list alone then in layout file, just use @android:id/xxx and layout resource parameter android.R.layout.simple_list_item_1 in Adapter will correspond to this list defined in xml.
  3. Instead of first defining list and then calling setAdapter() on it, just call setListAdapter() directly.
  4. There is no need to implement OnClickListener, setOnClickListener() on list and then use onItemClick(). Just use onListItemClick().
Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Mr Singh
  • 415
  • 1
  • 6
  • 14