I'm trying to get into a listview items have different action when pressed, this action depends on a variable "state", but the problem is that all the items are in the same action, here the code:
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// Storing JSON item in a Variable
String codigo = c.getString(TAG_CODIGO);
String asignatura = c.getString(TAG_NOMBRE);
int estado = c.getInt("estado");
final int maxhoras = c.getInt("maxhoras");
final String idprogramacion = c.getString("programacionid");
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_CODIGO, codigo);
map.put(TAG_NOMBRE, asignatura);
jsonlist.add(map);
list=(ListView)findViewById(R.id.lvclases);
ListAdapter adapter = new SimpleAdapter(Bienvenida.this, jsonlist,
R.layout.listview,
new String[] { TAG_CODIGO,TAG_NOMBRE, }, new int[] {
R.id.codigo, R.id.nombre,
});
list.setAdapter(adapter);
if(estado == 1) {
Log.e("estado", ""+estado);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
/*Toast toast1 = Toast.makeText(getApplicationContext(), "Correcto: el usuario existe", Toast.LENGTH_SHORT);
toast1.show();*/
//Toast.makeText(Bienvenida.this, "You Clicked at " + jsonlist.get(+position).get("asignatura"), Toast.LENGTH_SHORT).show();
}
});
}else{
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Bienvenida.this, "la clase aún no ha comenzado " + jsonlist.get(+position).get("asignatura"), Toast.LENGTH_SHORT).show();
}
});
}
}
I am working on android studio, I appreciate the help
Acually, this is not an answer to your question. But after watching your code it seems that you are not implementing
ListView
in a correct way.A simple way to work with
ListView
goes through following abstract steps:Make your Collection Ready -> Prepare the Adapter from the collection -> Set this adapter to ListView
In your case, assuming you are calling web services for getting data and your web services is responding with JSON array.
Now, make a POJO(plain old Java object) class like:
Now, prepare list like:
Now, you are ready with list, just pass it to your custom adapter. If you are not aware of preparing Custom Adapter, check this link.
Once you are ready with making custom adapter, pass the list to this adapter like:
MyCustomAdapter adapter = new MyCustomAdapter(context, mArrayList);
Note: Above line strictly depends constructor of your custom adapter, say,
MyCustomAdapter
After that, you are ready with your adapter, set it to your ListView, like:
list.setAdapter(adapter);
Now, here you set the
OnItemClickListener
Hope you get what you are trying to achieve..
This should help you out.