views:

232

answers:

6

Hi

I'd like to show another List View 'B' after clicking a item of List View 'A'. I use onListItemClick event in Android 1.6 project.

public void onListItemClick( ListView parent, View v, int position, long id) {
Toast.makeText(this, "You have selected " + lv_arr[position], Toast.LENGTH_SHORT).show(); }

how to code it?

A: 

If you want to stay on the same Activity and layout you could use a ViewSwitcher, which is designed for flipping between two views.

http://developer.android.com/intl/fr/reference/android/widget/ViewSwitcher.html

However I would strongly suggest that the click triggers a new local Activity via an Intent. This will have a new Layout containing your second ListView. This is because users will expect that having clicked and had the display significantly change, that pressing the back button will take them back to the original location in the app. As a general rule, any user action that changes the conceptual location in the application should be accompanied by an Activity change.

Jim Blackler
A: 

How about try ExpandableListView. When you click on the groupview it expands to show childviews. It has a nice BaseExpandableListAdapter.

Raja
A: 

For example I call a new activity using Intents, with packed values added this way...

@Override
    public void onListItemClick( ListView parent, View v, int position, long id)  {
        Intent lancon = new Intent(this, viewContact.class);
        //lancon.putExtra("id", id);
        //or
        c.moveToPosition(position); 
        id = c.getInt(0); 
        c.close(); 
        lancon.putExtra("id", id);
        this.startActivity(lancon);
        finish();
    }

Then in the other class onCreate method I call:

this._id = this.getIntent().getLongExtra("id", 0);
Pentium10
Sorry for my inconvenient replies. I didn't add this list activity at AndroidMainfest.xml.
A: 

Thanks all.

I get the error - Intent cannot be resolved to a type.

public class MainView extends ListActivity

{ private String[] lv_arr={"LA","LB","LC"};

static final int LA_Pos = 0;
static final int LB_Pos = 1;


@Override  
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.mview);

    setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, lv_arr));
}    

@Override
public void onListItemClick(
        ListView parent, View v,
        int position, long id) 
        {   
        switch (position) {
                case LA_Pos: 
                     startActivity(new Intent( this, WhiteListView.class));                 


                case LB_Pos: 
                     Toast.makeText(this, 
                        "You have selected " + lv_arr[position], 
                            Toast.LENGTH_SHORT).show();


            }                   

        }

}

A: 

I add this - import android.content.Intent;

Pentium10,

c.moveToPosition(position); id = c.getInt(0); c.close();

Here, 'c' means? And

this._id = this.getIntent().getLongExtra("id", 0);

_id? Without adding this statement, I get 'Source not found'. Please explain me more. Thank you.

+1  A: 

I could see the List View by adding

        <activity android:name=".WhiteListView"/>

in AndroidMainfest.xml. Thank you.