简体   繁体   中英

How to make Clickable ListView

I have a problem with the ListView ... I would like the application to go in a second activity at the click of every element contained within it. In the second activity there will be the update and delete functions. The elements present in the ListView are contained in a mysql database and recovered with json ... Can you help me understand how to program the click of the ListView elements?

public class LoggedActivity extends AppCompatActivity {

    private RequestQueue mQueue;
    private ListView list_view;
    Button buttonParse;
    Button aggingi_prodotto;

    private ArrayList<String> lista;

    private ArrayAdapter<String> adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logged);

        list_view = findViewById(R.id.list_view);

        lista = new ArrayList<>();

        buttonParse = findViewById(R.id.button_parse);
        aggingi_prodotto = findViewById(R.id.aggiungi_prodotto);

        //--------------------------
        Bundle extras = getIntent().getExtras();
        final String id_utente = extras.getString("id");
        //--------------------------

        mQueue = Volley.newRequestQueue(this);

        aggingi_prodotto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),AggiungiActivity.class);
                intent.putExtra("id",id_utente);
                startActivity(intent);

            }
        });

        buttonParse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse(id_utente);
            }
        });

    }

    private void jsonParse(final String id_utente) {

        String url = "http://192.168.1.5/progettoPHP/WebServices/webSrv.php?type=recuperaProdotti&idutente=" + id_utente;

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        try {

                            JSONArray jsonArray = response.getJSONArray("Dati");

                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject dato = jsonArray.getJSONObject(i);

                                String id = dato.getString("id");
                                final String id_utente = dato.getString("id_utente");
                                String nome_prodotto = dato.getString("nome_prodotto");

                                //lista.add(id_utente + " - " + id +  " - " + nome_prodotto + "\n\n");
                                lista.add(nome_prodotto);


                            }

                            adapter = new ArrayAdapter<String>(LoggedActivity.this, android.R.layout.simple_list_item_1, lista);
                            list_view.setAdapter(adapter);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        mQueue.add(request);

    }

}

Try like this

list_view.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
     // do whatever you want to click of the listview item
   }

});

If you want List Click you can simply use

list_view.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
     // do whatever you want to click of the listview item
   }

});

Instead of Listview , you can use ResyclerView

If you want to click on the specific item in the list and get the position of item you can make Listner in Adapter

In Adapter

Initialized

private OnItemClick onItemClick;

ClickListner

ItemView.setOnClickListener(v -> {
                if (onItemClick != null) {
                    onItemClick.onItem(i);
                }
            });

Functions

public interface OnItemClick {
        void onItem(int position);
    }

public void setOnItem(OnItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

In MainActivity

 adapter.setOnItem(new Adapter.OnItemClick() {
                    @Override
                    public void onItem(int i) {

                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM