我无法为onItemClick
实施ListView
。我需要在点击ListView
中的项目时打开一项新活动,并将标题/流派/评级/发布年份数据传递给此新活动。我还想知道如何在ListView
中传递所点击的项目的缩略图,并将其显示在新活动中。
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
String genreStr = "";
for (String str : m.getGenre()) {
genreStr += str + ", ";
}
genreStr = genreStr.length() > 0 ? genreStr.substring(0,
genreStr.length() - 2) : genreStr;
genre.setText(genreStr);
// release year
year.setText(String.valueOf(m.getYear()));
return convertView;
}
}
Main2Activity.java
public class Main2Activity extends AppCompatActivity {
// Log tag
private static final String TAG = Main2Activity.class.getSimpleName();
// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = (ListView) findViewById(service_list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
//getActionBar().setBackgroundDrawable(
//new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
答案 0 :(得分:1)
您可以将模型调用的各个项目作为Extras添加到intent中,或者您可以使模型类实现Serializable接口,只需手动将整个对象添加到intent:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Movie movie = (Movie) adapterView.getItemAtPosition(i);
Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra("mName", movie.getTitle());
intent.putExtra("mRating", movie.getRating());
intent.putExtra("mGenre", movie.getGenre());
intent.putExtra("mYear". movie.getYear());
//Get the Bitmap object from the NetworkImageView
Bitmap bm = ((BitmapDrawable)((NetworkImageView)view.findViewById(R.id.image)).getDrawable()).getBitmap();
//Can be added directly as an Extra, as Bitmap implements Parcelable
intent.putExtra("mImage", bm);
//OR make Moview implement Serializable and do this :
//intent.putExtra("clickedObject", movie);
startActivity(intent);
}
});
在接收活动中,您可以使用getExtras()来获取值:
Bitmap bm = getIntent().getExtras().getParcelable("mImage");
imageView.setImageBitmap(bm);
答案 1 :(得分:1)
您可以发送整个对象movieItem和项目的位置然后在下一个活动中,您可以像这样提取它们。
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
{
intent=new Intent(getBaseContext(),NextActivity.class);
intent.putParcelableArrayListExtra("key", movielist);
intent.putExtra("IntKey",p3);
}
});
答案 2 :(得分:0)
在我回答这个问题的人的帮助下改变了代码后,一切正常。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Movie movie = (Movie) adapterView.getItemAtPosition(i);
Intent intent = new Intent(view.getContext(), Main3Activity.class);
intent.putExtra("mName", movie.getTitle());
intent.putExtra("mRating", movie.getRating());
intent.putExtra("mGenre", movie.getGenre());
intent.putExtra("mYear", movie.getYear());
intent.putExtra("mServiceImage",movie.getThumbnailUrl());
//OR make Moview implement Serializable and do this :
//intent.putExtra("clickedObject", movie);
startActivity(intent);
}
});
在最后的活动中,我使用Android-Universal-Image-Loader从我从第一个活动传递的网址加载图片。
String mName = getIntent().getExtras().getString("mServiceImage");
imageLoader.displayImage(mName, imageView);