简体   繁体   中英

How to implement OnItemLongClickListener on ListView from array / json?

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;
                   }
                });
           }
    }

图片1-setOnItemLongClickListener

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;
               }
            });

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