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?

Allen Gingrich
  • 5,608
  • 10
  • 45
  • 57
  • I'm not sure but aren't you missing to call Intent.setAction()? – RoflcoptrException Aug 10 '10 at 16:21
  • What does the CurrentMarketValue code look like? – Key Aug 10 '10 at 16:33
  • I'm not sure about the setAction(). I never use it unless I'm calling a data provider... As for CurrentMarketValue - it's all valid, albeit nearly non-existent. The program doesn't even attempt to initiate it for it to fail. – Allen Gingrich Aug 10 '10 at 16:49
  • setAction is useless when you specify the class you wan't to launch, Android does not have to choose between applications to know which is the best. – fedj Aug 10 '10 at 19:14
  • I was thinking there might be a call to setContentView() missing in CurrentMarketValue's onCreate which could be the reason why nothing happens, but I'll take your word for it that it is valid :) – Key Aug 11 '10 at 07:46

1 Answers1

1

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
  • 37,905
  • 5
  • 60
  • 62