Why doesn’t this work? Can Anyone tell me whats wrong with this onitemclicklistener? It doesn’t give me any errors, but does nothing when clicked.
public class Test extends ListActivity
{
private CommentsDataSource datasource;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
datasource = new CommentsDataSource( this );
datasource.open();
List<Comment> values = datasource.getAllComments();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>( this, R.layout.simple_list_item_1, values );
setListAdapter( adapter );
// Will be called via the onClick attribute
// of the buttons in main.xml
ListView list = (ListView) findViewById(android.R.id.list);
list.setClickable(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> list, View view, int position, long id)
{
@SuppressWarnings( "unchecked" )
ArrayAdapter<Comment> adapter = ( ArrayAdapter<Comment> ) getListAdapter();
Comment comment = null;
switch ( view.getId() )
{
case R.layout.simple_list_item_1:
String s = ((TextView)view.findViewById(android.R.id.text1)).getText().toString();
String keyword = s.substring(s.lastIndexOf(' '));
String uri= "smsto:" + keyword;
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uri));
startActivity(intent);
}
}
});
}
public void onClick( View view )
{
@SuppressWarnings( "unchecked" )
ArrayAdapter<Comment> adapter = ( ArrayAdapter<Comment> ) getListAdapter();
Comment comment = null;
switch ( view.getId() )
{
case R.id.delete:
if (getListAdapter().getCount() > 0)
{
comment = ( Comment ) getListAdapter().getItem( 0 );
datasource.deleteComment( comment );
adapter.remove( comment );
}
break;
case R.id.add:
if (getListAdapter().getCount() > 0)
{
int far = getListAdapter().getCount();
comment = ( Comment ) getListAdapter().getItem( 0 );
datasource.deleteComment( comment );
adapter.remove( comment );
}
break;
/*
case R.layout.simple_list_item_1:
{
List<String> sms = new ArrayList<String>();
Uri uriSMSURI = Uri.parse("content://sms/inbox");
Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, null);
String address = cur.getString(cur.getColumnIndex("address"));
String body = cur.getString(cur.getColumnIndexOrThrow("body"));
sms.add("Number: " + address + " .Message: " + body);
}
break;
*/
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume()
{
datasource.open();
super.onResume();
}
@Override
protected void onPause()
{
datasource.close();
super.onPause();
}
}
Here is the main.xml file where the lstview is kept:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="406dp"
android:layout_y="76dp"
android:background="#FFFFFF"
android:clickable="true"
android:divider="#000000"
/>
<Button
android:id="@+id/delete"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_x="21dp"
android:layout_y="16dp"
android:onClick="onClick"
android:text="Delete First" />
<Button
android:id="@+id/add"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_x="174dp"
android:layout_y="16dp"
android:onClick="onClick"
android:text="Delete All" />
</AbsoluteLayout>
And the xml file where the edit text is kept:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_margin="10dp"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textColor="#000000"
android:clickable="true"
android:background="#FFFFFF"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Your edit text is clickable and fills the entire row. Clicks are handled by the edit text, so the list item click listener never fires. There isn’t any way to determine whether the user intended to click into the edit text, or click on the row.
Add a button or something for the user to click on in each row that isn’t the edit text.