How to implement OnItemLongClickListener on ListView from array / json?

63 views Asked by At

I have a movie list, and when I perform long press on a movie, I want to display a synopsis of the movie. I try to run this simple code, but it does't return a value. PS: The json I made at https://app.subarnanto.com/api/inventory is not actually a movie list, I just learned to create an api on a nodejs and mysql for my school assignment. Thank you.

private static final String TAG = "MainActivity";
    String url = "https://app.subarnanto.com/api/inventory";
//    String url = "https://www.subarnanto.com/api/andri.json";
    ListView list;
    private List<MovieDataSet> listMovie = new ArrayList<>();
    private MovieAdapter movieAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView)findViewById(R.id.list);

        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
        JsonArrayRequest jar = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                //Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();
                listMovie.clear();
                for (int i=0; i<response.length(); i++) {
                    try {
                        JSONObject job = response.getJSONObject(i);
                        MovieDataSet mds = new MovieDataSet();
                        mds.setImageUrl(job.getString("image"));
                        mds.setJudul(job.getString("name"));
                        mds.setYear(job.getInt("serial"));
                        mds.setRating(job.getDouble("tag"));
                        listMovie.add(mds);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                movieAdapter = new MovieAdapter(MainActivity.this, listMovie);
                movieAdapter.notifyDataSetChanged();
                list.setAdapter(movieAdapter);

                list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                   @Override
                   public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                       Log.d(TAG, "onItemLongClick:" + listMovie.get(i));

                       Toast.makeText(MainActivity.this, "Anda menekan: " + listMovie.get(i), Toast.LENGTH_LONG).show();
                       return false;
                   }
                });
           }
    }

Pic 1 - setOnItemLongClickListener

1

There are 1 answers

3
Sam Raju On BEST ANSWER

Use code like below

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
               @Override
               public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                   Log.d(TAG, "onItemLongClick:" + listMovie.get(i));

                   Toast.makeText(MainActivity.this, "Anda menekan: " + listMovie.get(i).getSynopsis/*this from your MovieDataSet model class*/ , Toast.LENGTH_LONG).show();
                   return false;
               }
            });