ListView Help


  1. dgibbiii

    dgibbiii New Member

    I am new at creating UI Java apps and am trying to create an app that starts with a list of items (ListView) to choose from and then open the corresponding package. I have created the list along with one of the many packages and they both work independently. My problem is I cannot figure out for the life of me how to call the selected "module" from the list. Here is the list code:

    package com.constructioncalc.main;

    import com.example.squarefeet.*;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    public class ConstructionCalc extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, CALCULATORS));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    // When clicked, goto selected package
    DO SOMETHING HERE
    }
    });
    }

    static final String[] CALCULATORS = new String[] {
    "Square Feet"
    };
    }

    Advertisement
  2. JamTheMan

    JamTheMan Well-Known Member

    I am not sure I know what you mean when you say call the selected module, but i guess you want to either open a Dialog or a new Activity with the selected modules features?

    If so all you need to do is create these Activities and/or Dialogs (see developer.android.com for more on that), and open the right one depending on which item is selected in the list. That item is placed at a certain position in the list, which is given as one of the parameters of the OnItemClickListener.

    So if position 0 represents a module which allows a user to read about Tennis then you should have the "DO SOMETHING HERE" start the Tennis Activity if the position == 0...

    And so on...

Share This Page