我有一个电影列表,当我长时间按电影时,我想要显示电影的概要。我尝试运行这个简单的代码,但它不会返回一个值。 PS:我在https://app.subarnanto.com/api/inventory制作的json实际上并不是一个电影列表,我刚刚学会了在我的学校作业中为nodejs和mysql创建一个api。 谢谢。
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;
}
});
}
}
答案 0 :(得分:0)
使用以下代码
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;
}
});