Domanda

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"
/>
È stato utile?

Soluzione

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.

Altri suggerimenti

How do you know it doesn't do anything when clicked? Because the Intent doesn't fire? That's a different kettle of fish. Make sure your listener is getting called at all by either setting a breakpoint and debugging or putting the following in:

public void onItemClick(AdapterView<?> list, View view, int position, long id) 
{
    Log.v("onItemClick()", "inside listener, id = ["+view.getId()+"]");
    ...

Personally I think you're getting in there just fine. I suspect the view ID isn't what you think it is, and that log will show it.

switch ( view.getId() ) 
{
    case R.layout.simple_list_item_1:

The view ID isn't going to be R.layout.simple_list_item_1, it's likely going to be the ID of the root element of that layout if anything which, if you are using that layout, would be android:id/text1. I'm not quite following why you're testing for the view's ID, but testing for the right one will help.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top