neel
neel

Reputation: 5293

Select Item in a list view in Android

I am new in android development. I have a table in sqllite and that contains id, name, number.

I am try to do the following things :

  1. list the names in the table.
  2. click on a particular name, go to another XML page and show the name and number.

I can list the names(first one). but when clicking on the name in the list, logcat shows the following error.

09-25 06:42:24.071: E/AndroidRuntime(32364): java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor

Details.java

public class Details extends ListActivity {
private DetailOperations detailDBoperation;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);
    detailDBoperation=new DetailOperations(this);
    detailDBoperation.open();
    final ListView list=(ListView) findViewById(android.R.id.list);
    List<Detail> values=detailDBoperation.getAllDetails();
    List<String> names=new ArrayList<String>();
    for(int i=0;i<values.size();i++)
    {
      names.add(values.get(i).getName()); 
    }
    ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,names);
    list.setAdapter(adapter);

    //click on the list item
    list.setClickable(true);
    list.setTextFilterEnabled(true);
    list.setOnItemClickListener(new OnItemClickListener() {

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

         Cursor cursor=(Cursor) list.getItemAtPosition(position);
         TextView text1=(TextView) findViewById(R.id.nametext);
         text1.setText(cursor.getString(1).toString());
         TextView text2=(TextView)findViewById(R.id.numbertext);
         text2.setText(cursor.getString(2).toString());
         openDetails();

       /*   AlertDialog.Builder adb=new AlertDialog.Builder(Details.this); 
            adb.setTitle("ListView OnClick");
            adb.setMessage("Selected item="+ list.getItemAtPosition(position));
            adb.setPositiveButton("Ok",null);
            adb.show();
                    */

        }
    });

}
public void openDetails(){
    Intent intent=new Intent(this,ShowDetails.class);
    startActivity(intent);
}

@Override
protected void onResume(){
    detailDBoperation.open();
    super.onResume();
}
@Override
protected void onPause(){
    detailDBoperation.close();
    super.onPause();
}

    }

showDetails.java

    public class ShowDetails extends Activity{
public void onCreate(Bundle savedInstaceState){
    super.onCreate(savedInstaceState);
    setContentView(R.layout.showdetails);

}
   }

details.xml

 <ListView
    android:cacheColorHint="#00000000"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@android:id/list"
    android:layout_alignParentLeft="true"
    android:textSize="15dp" >
</ListView>

showdetails.xml

    <TextView
    android:id="@+id/nametext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/numbertext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

Upvotes: 0

Views: 4195

Answers (3)

Gil Moshayof
Gil Moshayof

Reputation: 16771

If you're trying to send the values to the new activity, you should do this:

Change your details method like this:

public void openDetails(String name, String number)
{
    Intent intent=new Intent(this,ShowDetails.class);
    intent.putExtra("name", name);
    intent.putExtra("number", number);
    startActivity(intent);
}

In the onCreate of the new activity, do this:

String name = getIntent().getExtras().getString("name");
String number = getIntent().getExtras().getString("number");

TextView nameText = (TextView)findViewById(R.id.nametext);
TextView numberText = (TextView)findViewById(R.id.numbertext);  

nameText.setText(name);
numberText.setText(number); // I'm assuming you've already created instances of these textviews.

Also, you'll need to pass the name & number parameters to the openDetails method from the onItemClick method, like this:

list.setOnItemClickListener(new OnItemClickListener() {

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

     Cursor cursor=(Cursor) list.getItemAtPosition(position);
     TextView text1=(TextView) findViewById(R.id.nametext);
     String name = cursor.getString(1).toString();
     //text1.setText(name);
     TextView text2=(TextView)findViewById(R.id.numbertext);
     String number = cursor.getString(2).toString();
     //text2.setText(number);
     openDetails(name, number);

   /*   AlertDialog.Builder adb=new AlertDialog.Builder(Details.this); 
        adb.setTitle("ListView OnClick");
        adb.setMessage("Selected item="+ list.getItemAtPosition(position));
        adb.setPositiveButton("Ok",null);
        adb.show();
                */

    }
});

Hope this helps :)

Upvotes: 1

Umer Farooq
Umer Farooq

Reputation: 7486

You are passing String array to Arrayadapter and expect it to return a cursor to the database table?

In OnItemClick() this line is causing this exception:

Cursor cursor=(Cursor) list.getItemAtPosition(position);

What you can do is, declare List<Detail> values outside onCreate() and follow this:

     TextView text1=(TextView) findViewById(R.id.nametext);
      text1.setText(values.get(position).getName());
     TextView text2=(TextView)findViewById(R.id.numbertext);
     text2.setText(values.get(position).getNumber());

Upvotes: 0

Kuffs
Kuffs

Reputation: 35661

In your OnItemClick method you have the line:

   Cursor cursor=(Cursor) list.getItemAtPosition(position);

The result of list.getItemAtPosition(position) is a string but you are trying to convert it to a cursor.

You therefore receive the error:

 java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor

Upvotes: 0

Related Questions