I don't know if it is the best way or if there is another way, but when building the listview, I need it to be clickable and load another Activity (each item a different Activity).
Main class:
public class Inicio extends Activity {
ListView lista;
TextView textTitNivel, textNivel, textSabiduria, textExp;
ProgressBar progressBarSabiduria, progressExp;
View divider;
static final String Operaciones[][] =
{
{ "Sumas I", "80","5" },
{ "Restas I","110","2" },
{ "Números I","50","7" }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inicio);
lista = (ListView)findViewById(R.id.listview);
CustomAdapter adaptador = new CustomAdapter(this, Operaciones);
lista.setAdapter(adaptador);
Toast.makeText(Inicio.this, "Test2", Toast.LENGTH_SHORT).show();
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Inicio.this, "Test3", Toast.LENGTH_SHORT).show();
switch(position) {
case 1:
Toast.makeText(Inicio.this, "Case1", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Inicio.this, SumasActivity.class);
//i.putExtra("key", value); //Optional parameters
startActivity(i);
break;
case 2:
Intent i2 = new Intent(Inicio.this, SumasActivity.class);
//i.putExtra("key", value); //Optional parameters
startActivity(i2);
break;
case 3:
Intent i3 = new Intent(Inicio.this, SumasActivity.class);
//i.putExtra("key", value); //Optional parameters
startActivity(i3);
break;
}
}
});
}
}
Inside the CustomAdapter:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.listview, null);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
TextView textView4 = (TextView) view.findViewById(R.id.textView4);
TextView textSabiduria = (TextView) view.findViewById(R.id.textSabiduria);
TextView textOp = (TextView) view.findViewById(R.id.textOp);
TextView textExp = (TextView) view.findViewById(R.id.textExp);
textOp.setText(Operaciones[i][0]);
textExp.setText(Operaciones[i][1]);
textSabiduria.setText(Operaciones[i][2]);
return view;
}
Where do I put the following onclicklistener? I'm lost..
For the item "Sums I":
Intent i = new Intent(this, Sumas.class);
startActivity(i);
For the item "Subtractions I":
Intent i = new Intent(this, Restas.class);
startActivity(i);
Well, the truth is simple. The event
OnItemClick
is implemented from where you are implementing the ListView.I recommend that you read the following articles to better understand how ListViews and interfaces work.
Another thing, to fill the ListView I recommend that you use a list of objects instead of a two-dimensional array, I assure you that it is easier to understand and maintain; in the Beautiful Programming tutorial they show how.
Update
If you use a button or an ImageButton in the items of your ListView, you must add the property:
android:descendantFocusability="blocksDescendants"
. This is so that the button or ImageButton doesn't steal focus from the item, when they steal focus from the item, the item can't be clicked.For more information see:
ListView OnItemClickListener not responding?
Focus problems with list rows and ImageButtons
I recommend you use RecyclerView , it is a more flexible widget than ListView to create lists, and with it you will not have these kinds of problems.