Phalanx
Phalanx

Reputation: 1217

Should I extend ListActivity ? (already working well without it so far)

I am working on an app whose purpose is to display a listview with some content from a database. So I have an activity called ResultListViewActivity (extending something else than ListActivity!), in which I have a displayListView() method.

So far everything is working : I can see my listview, the content from my database is displayed perfectly, I can arrange it the way I want in my XML file, and all of this without extending the ListActivity class.
My next step is to set a onClickItemListener to open a new activity with different content depending on the button clicked.

Everytime I read tutorials, it's always about using tons of stuff, creating other classes with adapters, holder pattern, content provider, etc. I don't understand half of this, in my app I just have one class for my ListView and it works fine without using all the stuff I read in tutorials. So here is my question: will I have to find a way to extend ListActivity eventually (to use the onClickItemListener method for example)?

Here is the code of my activity displaying the listview:

public class ResultListViewActivity extends Base_Activity {

private SimpleCursorAdapter cursorAdapter;
private DatabaseAdapter dbHelper;
ListView listView;
TextView poititle; 
private static String TAG = ResultListViewActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result_list_view);

    poititle = (TextView) findViewById(R.id.poititle);
    dbHelper = new DatabaseAdapter(this);
    dbHelper.open();

    displayListView();
}

 private void displayListView() {

    Bundle bundle = getIntent().getExtras();
    String title = bundle.getString("title", "Choose here :");
    String inInterval = bundle.getString("inInterval"); 
    poititle.setText(title);

    Cursor c = dbHelper.findPoiInTable(inInterval);

            String[] columns = new String[] { DatabaseAdapter.COL_NAME, DatabaseAdapter.COL_STREET, DatabaseAdapter.COL_WEBSITE };
    int[] to = new int[] {R.id.name, R.id.street, R.id.website};
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
            columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.poilistview);
    // Assign adapter to ListView
    listView.setAdapter(cursorAdapter); 
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}   }

Should I add an inside class and make my code this way to allow further operations on the list view :

  public class ResultListViewActivity extends Base_Activity {

//declaration of variables

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result_list_view);

    poititle = (TextView) findViewById(R.id.poititle);
    dbHelper = new DatabaseAdapter(this);
    dbHelper.open();

    displayListView();
}

 public class MyListView extends ListActivity {

 @Override 
 protected void onCreate(Bundle savedInstanceState)
 super.onCreate(....)

 listview.setOnClickListener ....
 .....

 private void displayListView() {

    // same method as in the code above

    }

    }    

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getSupportMenuInflater().inflate(R.menu.main, menu);
    return true;
}   }  

As you can see, I would add an "inside" class MyListView extending ListActivity just so all which is related to listview would be in this class instead of the one extending Base_Activity. I don't know if it's a correct way to do : I don't remember seeing this in a tutorial.

Upvotes: 1

Views: 226

Answers (1)

AwadKab
AwadKab

Reputation: 3064

You can add onClickItemListener like this (you don't need extend ListActivity)

// Click event for single list row
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // Do somthing here
            }
        });

Hope this helped you.

Upvotes: 1

Related Questions