【问题标题】:Android listview item clickAndroid listview 项目点击
【发布时间】:2015-06-14 02:15:19
【问题描述】:

我试图进入一个列表视图项目在按下时有不同的动作,这个动作取决于一个变量“状态”,但问题是所有的项目都在同一个动作中,这里的代码:

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,感谢您的帮助

【问题讨论】:

    标签: android android-listview onitemclicklistener


    【解决方案1】:

    实际上,这不是您问题的答案。但看了你的代码后,你似乎没有以正确的方式实现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")
            )
        );
    }
    

    现在,您已准备好列表,只需将其传递给您的自定义适配器。如果您不知道准备自定义适配器,请查看此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();
             }
    
          }
    });
    

    希望你能得到你想要达到的目标..

    这应该可以帮助你。

    【讨论】:

    • 不客气 :) 如果对您有帮助,您可以接受答案。以便访问者了解正确答案。
    【解决方案2】:

    不要使用两个不同的 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
             }
           }
       });
    

    【讨论】:

    • 我已经试过了还是不行,继续做同样的动作
    • 点击后看到了什么?
    • 当我点击任何项目时,总是执行与 else 中相同的操作。
    • 这意味着您的estado 值不等于一。你在哪里设置estado 的值?也包括这部分代码。
    【解决方案3】:

    问题是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
         }
       }
    

    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      相关资源
      最近更新 更多