Android 列表视图项点击
Android listview item click
我试图进入一个列表视图,项目在按下时有不同的动作,这个动作取决于一个变量"state",但问题是所有项目都在同一个动作中,这里是代码:
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 工作室工作,感谢您的帮助
与其使用两个不同的 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 设置不同的操作。您也可以 setOnClickListener
in getView()
in Adapter
并基于状态。
请参考以下内容:
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
}
}
});
实际上,这不是您问题的答案。但是看了你的代码后,你似乎没有以正确的方式实现 ListView
。
使用 ListView
的简单方法包括以下抽象步骤:
准备好您的集合 -> 从集合中准备适配器 -> 将此适配器设置为 ListView
在您的情况下,假设您正在调用 Web 服务以获取数据并且您的 Web 服务正在响应 JSON 数组。
现在,制作一个 POJO(普通旧 Java 对象)class,例如:
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")
)
);
}
现在,您已准备好列表,只需将其传递给您的自定义适配器即可。如果您不知道准备自定义适配器,请检查此 link。
准备好制作自定义适配器后,将列表传递给此适配器,例如:
MyCustomAdapter 适配器 = new MyCustomAdapter(context, 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();
}
}
});
希望你能得到你想要达到的目标..
这应该能帮到你。
我试图进入一个列表视图,项目在按下时有不同的动作,这个动作取决于一个变量"state",但问题是所有项目都在同一个动作中,这里是代码:
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 工作室工作,感谢您的帮助
与其使用两个不同的 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 设置不同的操作。您也可以 setOnClickListener
in getView()
in Adapter
并基于状态。
请参考以下内容:
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
}
}
});
实际上,这不是您问题的答案。但是看了你的代码后,你似乎没有以正确的方式实现 ListView
。
使用 ListView
的简单方法包括以下抽象步骤:
准备好您的集合 -> 从集合中准备适配器 -> 将此适配器设置为 ListView
在您的情况下,假设您正在调用 Web 服务以获取数据并且您的 Web 服务正在响应 JSON 数组。
现在,制作一个 POJO(普通旧 Java 对象)class,例如:
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")
)
);
}
现在,您已准备好列表,只需将其传递给您的自定义适配器即可。如果您不知道准备自定义适配器,请检查此 link。
准备好制作自定义适配器后,将列表传递给此适配器,例如:
MyCustomAdapter 适配器 = new MyCustomAdapter(context, 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();
}
}
});
希望你能得到你想要达到的目标..
这应该能帮到你。