tags:

views:

65

answers:

1

I'm trying to have another activity launch when a list item gets clicked. Below is my code:

public class AvoidForeclosure extends CustomListTitle {
/** Called when the activity is first created. */
private DbAdapter db;
private SimpleCursorAdapter clients;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ListView list = getListView();

    setContentView(R.layout.main);

    this.title.setText("Avoid Foreclosure");

    db = new DbAdapter(this);
    db.open();

    fillData();

    list.setOnItemClickListener(new OnItemClickListener() {

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

                int viewId = view.getId();

                TextView theView = (TextView) findViewById(viewId);

                String name = theView.getText().toString();

                Cursor clientData = db.getClientByName(name);

                Intent intent = new Intent();
                intent.setClass(view.getContext(), CurrentMarketValue.class);
                intent.putExtra("clientId", clientData.getInt(0));
                startActivity(intent);

        }

    });

}

private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor c = db.fetchAllClients();
    startManagingCursor(c);

    String[] from = new String[] { DbAdapter.KEY_NAME };

    int[] to = new int[] { R.id.text1 };

    // Now create an array adapter and set it to display using our row
    clients = new SimpleCursorAdapter(this, R.layout.clientsrow, c, from, to);
    setListAdapter(clients);


}

}

Yet when I click it, nothing happens at all. Any ideas?

+1  A: 

Try switching this line:

ListView list = getListView();

to be after this one:

setContentView(R.layout.main);

Otherwise you'll be getting a handle to the ListActivity's default layout's ListView, rather than R.layout.main's listview (which is the one you really want).

Yoni Samlan
Thank you for answering, but that didn't solve the problem. It's as if the listener isn't even hearing a click at all.
Allen Gingrich
Try overriding ListActivity's onListItemClicked() method rather than manually setting a new onClickListener, too.
Yoni Samlan