[英]Android listview item click
我試圖進入一個listview項目時按下時具有不同的操作,此操作取決於變量“狀態”,但問題是所有項目都處於同一操作中,這里的代碼:
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();
}
});
}
}
我正在android studio上工作,感謝您的幫助
從根本上講,這不是您問題的答案。 但是看完代碼后,似乎您沒有以正確的方式實現ListView
。
使用ListView
一種簡單方法包括以下抽象步驟:
准備好您的收藏集->從收藏集中准備適配器->將此適配器設置為ListView
在您的情況下,假設您正在調用Web服務以獲取數據,並且您的Web服務使用JSON數組進行響應。
現在,制作一個POJO(普通的舊Java對象)類,例如:
public class MyListItem{
private String codigo;
private String asignatura;
private int estado;
private int maxhoras;
private String idprogramacion;
public MyListItem(String codigo, String asignatura, int estado, int maxhoras, String idprogramacion){
this.codigo = codigo;
this.asignatura = asignatura;
this.estado = estado;
this.maxhoras = maxhoras;
this.idprogramacion = idprogramacion;
}
// you can write getter setter methods for this
public int get_estado(){
return estado;
}
public String get_idprogramacion(){
return idprogramacion;
}
public int get_maxhoras(){
return maxhoras;
}
public String get_asignatura(){
return asignatura;
}
}
現在,准備清單:
ArrayList<MyListItem> mArrayList = new ArrayList<>();
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
mArrayList.Add(
new MyListItem(
c.getString(TAG_CODIGO),
c.getString(TAG_NOMBRE),
c.getInt("estado"),
c.getInt("maxhoras"),
c.getString("programacionid")
)
);
}
現在,您已經准備好使用列表,只需將其傳遞給您的自定義適配器即可。 如果您不知道要准備“定制適配器”,請檢查此鏈接 。
准備好制作自定義適配器后,將列表傳遞給該適配器,例如:
MyCustomAdapter適配器=新的MyCustomAdapter(上下文,mArrayList);
注意:上一行嚴格取決於您的自定義適配器的構造函數,例如MyCustomAdapter
之后,就可以使用適配器了,將其設置為ListView,如下所示:
list.setAdapter(adapter);
現在,在這里設置OnItemClickListener
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyListItem item = (MyListItem)parent.getItem(position);
if(item.get_estado() == 1)
{
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", item.get_idprogramacion());
i.putExtra("maxhoras", item.get_maxhoras());
startActivity(i);
}
else{
Toast.makeText(Bienvenida.this, "la clase aún no ha comenzado " + item.get_asignatura(), Toast.LENGTH_SHORT).show();
}
}
});
希望你能得到想要的成就。
這應該可以幫助您。
而不是使用兩個不同的onClickListeners(),請嘗試僅使用一個,並將if-else塊移至onItemClick()方法內
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(estado == 1) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
} else {
// Do something else
}
}
});
問題是estado
變量,它是全局變量,因此,當您使用listview.setOnItemClickListener(...)
,這意味着listview
所有項目將與您的上述代碼具有相同的作用。
解決方案是,您應該創建自己的custom adapter
,然后根據不同的狀態在adapter
實現OnItemClickListener
來為item設置不同的操作。 您也可以在Adapter
getView()
中設置setOnClickListener
並基於狀態。
請參考以下內容:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int estado = -1;
// you should do something like this
/* ********** */
YourItem item = ((YourItemsData)data).get(position);
estado = item.getState();
/* ********** */
if(estado == 1) {
Intent i = new Intent(Bienvenida.this, registroAsistencia.class);
i.putExtra("programacion", idprogramacion);
i.putExtra("maxhoras", maxhoras);
startActivity(i);
} else {
// Do something else
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.