Reputation: 129
I am trying to show a listview
when in I click an item on another listview
. How do I do this?
This is my code:
public class ListaPrincipal extends Activity implements OnItemClickListener{
private ListView lvPrincipal;
private List<String> principal;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_prinicpal);
llenarLista();
mostrarListar();
}
private void llenarLista(){
principal = new ArrayList<String>();
principal.add("Gorras");
principal.add("Nintendo DS");
principal.add("Pantuflas");
principal.add("Peluches");
principal.add("Xbox 360");
}
private void mostrarListar(){
lvPrincipal = (ListView)findViewById(R.id.listaprincipal);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, principal);
lvPrincipal.setAdapter(adapter);
lvPrincipal.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clickeado", Toast.LENGTH_LONG).show();
}
}
Upvotes: 1
Views: 24
Reputation: 13540
There are several ways to do this, One approach is (not the best approach) to utilize the same list view and change the adaptor inside on item click event. issue is you need to handle navigation manually, back button etc
Other approach is to start new activity with another listview inside on item click event, this method is preferred because don't have to write bunch of code to handle navigation, back stack etc.
Or else you can make use of fragments, two fragments with list views.
Upvotes: 1