我正在使用图片和一些文字制作自定义列表视图。我已经使用了这个教程http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/.Now我想从listview中选择一个行项目,并希望显示在其他活动上。我使用onClickListener为此并使用Intent.But传递数据,其他任何活动都没有显示。
这是我的CustomListAdapter类
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.List;
import aquib.lathesis.volleylistview.R;
import aquib.lathesis.volleylistview.SingleItemView;
import aquib.lathesis.volleylistview.app.AppController;
import aquib.lathesis.volleylistview.model.Movie;
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(final 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 item_image = (NetworkImageView) convertView
.findViewById(R.id.item_image);
TextView item_name = (TextView) convertView.findViewById(R.id.item_name);
TextView item_category = (TextView) convertView.findViewById(R.id.item_category);
TextView item_serve = (TextView) convertView.findViewById(R.id.item_serve);
TextView item_price = (TextView) convertView.findViewById(R.id.item_price);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
item_image.setImageUrl(m.getItem_image(), imageLoader);
// item name
item_name.setText(m.getItem_name());
// item category
item_category.setText(m.getItem_category());
// item serve
item_serve.setText(m.getItem_serve());
// item price
item_price.setText(String.valueOf(m.getItem_price()));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Movie p = movieItems.get(position);
Intent i = new Intent(activity.getApplicationContext(), SingleItemView.class);
i.putExtra("itemimage", p.getItem_image());
i.putExtra("itemname", p.getItem_name());
i.putExtra("itemcategory", p.getItem_category());
i.putExtra("itemserve", p.getItem_serve());
i.putExtra("itemprice",String.valueOf(p.getItem_price()));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.getApplicationContext().startActivity(i);
}
});
return convertView;
}
}
这是我的SingleItemView活动
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import aquib.lathesis.volleylistview.app.AppController;
import aquib.lathesis.volleylistview.model.Movie;
public class SingleItemView extends Activity{
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private static String TAG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the view from singlelistview.xml
setContentView(R.layout.singlelistview);
try{
Intent i = getIntent();
//Get the result of item image
i.getStringExtra("itemimage");
//Get the result of item name
i.getStringExtra("itemname");
//Get the result of item category
i.getStringExtra("itemcategory");
//Get the result of item serve
i.getStringExtra("itemserve");
//Get the result of item price
i.getStringExtra("itemprice");
//Locate the image view
NetworkImageView itemImage = (NetworkImageView) findViewById(R.id.itemImage);
//Locate the text view
TextView itemName = (TextView) findViewById(R.id.itemName);
TextView itemCategory = (TextView) findViewById(R.id.itemCategory);
TextView itemServe = (TextView) findViewById(R.id.itemServe);
TextView itemPrice = (TextView) findViewById(R.id.itemPrice);
Movie m = new Movie();
itemImage.setImageUrl(m.getItem_image(), imageLoader);
itemName.setText(m.getItem_name());
itemCategory.setText(m.getItem_category());
itemServe.setText(m.getItem_serve());
itemPrice.setText(String.valueOf(m.getItem_price()));
} catch (Exception e){
Log.e(TAG, e.getMessage());
}
}
}
这是我的数据文件
包aquib.lathesis.volleylistview.model;
public class Movie {
private String item_image, item_name, item_category, item_serve;
private double item_price;
public Movie(){
}
public Movie(String item_image, String item_name, String item_category, String item_serve, double item_price) {
this.item_image = item_image;
this.item_name = item_name;
this.item_category = item_category;
this.item_serve = item_serve;
this.item_price = item_price;
}
public String getItem_image() {
return item_image;
}
public void setItem_image(String item_image) {
this.item_image = item_image;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public String getItem_category() {
return item_category;
}
public void setItem_category(String item_category) {
this.item_category = item_category;
}
public String getItem_serve() {
return item_serve;
}
public void setItem_serve(String item_serve) {
this.item_serve = item_serve;
}
public double getItem_price() {
return item_price;
}
public void setItem_price(double item_price) {
this.item_price = item_price;
}
}
这是我的MainActivity文件
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import aquib.lathesis.volleylistview.adapter.CustomListAdapter;
import aquib.lathesis.volleylistview.app.AppController;
import aquib.lathesis.volleylistview.model.Movie;
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://labs.kamkazi.com/aquib/buenomobileapp/jsonparseitems.txt";
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_main);
listView = (ListView) findViewById(R.id.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.setItem_image(obj.getString("item_image"));
movie.setItem_name(obj.getString("item_name"));
movie.setItem_category(obj.getString("item_category"));
movie.setItem_serve(obj.getString("item_serve"));
movie.setItem_price(((Number) obj.get("item_price"))
.doubleValue());
// 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;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这些是布局文件
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" >
</ListView>
</RelativeLayout>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp" >
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/item_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp" />
<!-- Name -->
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/item_image"
android:layout_toRightOf="@+id/item_image"
android:textSize="@dimen/title"
android:textStyle="bold" />
<!-- Category -->
<TextView
android:id="@+id/item_category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/item_image"
android:textSize="@dimen/rating" />
<!-- Serve -->
<TextView
android:id="@+id/item_serve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item_category"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/item_image"
android:textColor="@color/genre"
android:textSize="@dimen/genre" />
<!-- Price-->
<TextView
android:id="@+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/year"
android:textSize="@dimen/year" />
</RelativeLayout>
singlelistview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"/>
<!-- Name -->
<TextView
android:id="@+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/itemImage"
android:layout_toRightOf="@+id/itemImage"
android:textSize="@dimen/title"
android:textStyle="bold" />
<!-- Category -->
<TextView
android:id="@+id/itemCategory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemName"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/itemImage"
android:textSize="@dimen/rating" />
<!-- Serve -->
<TextView
android:id="@+id/itemServe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemCategory"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/itemImage"
android:textColor="@color/genre"
android:textSize="@dimen/genre" />
<!-- Price-->
<TextView
android:id="@+id/itemPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/year"
android:textSize="@dimen/year" />
</RelativeLayout>
当我运行我的应用程序时,第一个活动显示整个列表视图很好。但是当我点击一行并打开其他活动时,除了价格视图之外什么都没有显示。价格文本视图也没有正确显示。请帮助我..