Android listview如何知道我点击了哪个项目并在其他活动中显示

时间:2017-04-06 00:51:40

标签: android listview android-json

我正在制作Android APP,我有一个来自数据库的listview,它是json解析。我希望listview中的Onclick项目打开其他活动,点击任何项目时打开新活动没有问题。但问题是如何知道我点击了哪个项目并向新活动显示项目详细信息。

public class items extends BaseActivity {

private ProgressDialog pDialog;
private ArrayList<RowItem> rowItems = new ArrayList<RowItem>();
private ListView mylistview;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_items);

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean error = jsonResponse.getBoolean("error");
                System.out.println(jsonResponse.toString());
                if (error){
                    String errorMessage = jsonResponse.getString("message");
                    AlertDialog.Builder builder = new AlertDialog.Builder(items.this);
                    builder.setMessage(errorMessage)
                            .setNegativeButton("Retry", null)
                            .create()
                            .show();
                }else {
                    JSONArray itemSet = jsonResponse.getJSONArray("itemSet");
                    for (int i = 0; i < itemSet.length(); i++){
                        JSONObject item = itemSet.getJSONObject(i);
                        String title = item.getString("title");
                        String seller = item.getString("nickName");
                        String price = item.getString("price");
                        String itemID = item.getString("itemID");
                        String image_view = item.getString("picPath");
                        String Path;
                        JSONArray pic = item.getJSONArray("picPath");
                        if (pic.length()==0)
                        {
                            Path="uploads/default.jpg";
                        }else {
                            Path = pic.getString(0);
                        }
                        RowItem row=new RowItem(title,seller,price,image_view,Path,itemID);
                        rowItems.add(row);

                    }
                    mylistview = (ListView) findViewById(R.id.list);
                    CustomListAdapter adapter = new CustomListAdapter(items.this, rowItems);
                    System.out.println(rowItems.isEmpty());
                    mylistview.setAdapter(adapter);

                    mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(items.this, ItemDetail.class);
                            String title = ((TextView) view.findViewById(R.id.tv_title)).getText().toString();
                            String seller = ((TextView) view.findViewById(R.id.tv_seller)).getText().toString();
                            String price = ((TextView) view.findViewById(R.id.tv_price)).getText().toString();

                            intent.putExtra("title", title);
                            intent.putExtra("nickName", seller);
                            intent.putExtra("price", price);
                            startActivity(intent);

                        }
                    });
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
    };
    Intent intent = getIntent();
    ArrayList<String> list =intent.getStringArrayListExtra("userInfo");
    System.out.println(list.toString());
    itemRequest itemRequest = new itemRequest(list.get(0),list.get(1),responseListener);
    RequestQueue queue = Volley.newRequestQueue(items.this);
    queue.add(itemRequest);

    pDialog.dismiss();
}
}

我使用适配器

public class CustomListAdapter extends BaseAdapter {
private Context context;
private List<RowItem> rowItems;
private LayoutInflater inflater = null;

public static final String IMAGE_BASEURL = "http://XXXXXXXXXXXXXXXXXXXXXXXX/";

private LruCache<Integer, Bitmap> imageCache;
private RequestQueue queue;

public CustomListAdapter(Context context, List<RowItem> rowItems){
    this.context=context;
    this.rowItems=rowItems;
    inflater = LayoutInflater.from(context);

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    imageCache = new LruCache<>(cacheSize);

    queue = Volley.newRequestQueue(context);
}
@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

private class ViewHolder{
    ImageView imageView;
    TextView tv_title;
    TextView tv_seller;
    TextView tv_price;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    final RowItem row_pos = rowItems.get(position);
    final ViewHolder holder;
    System.out.print("=================================================================");
    if (convertView==null){
        convertView = mInflater.inflate(R.layout.custom_layout, null);
        holder = new ViewHolder();
        holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
        holder.tv_seller = (TextView) convertView.findViewById(R.id.tv_seller);
        holder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);

        convertView.setTag(holder);
    }else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.tv_title.setText(row_pos.getTv_title().toString());
    holder.tv_seller.setText(row_pos.getTv_seller().toString());
    holder.tv_price.setText(row_pos.getTv_price().toString());

    Bitmap bitmap = imageCache.get(Integer.parseInt(row_pos.getItemID()));
    holder.imageView = (ImageView) convertView.findViewById(R.id.image_view);

    if (bitmap != null){
        holder.imageView.setImageBitmap(bitmap);
        System.out.print("bitmap");
    }else {
        String imageURL = IMAGE_BASEURL + row_pos.getPath();
        ImageRequest request = new ImageRequest(imageURL, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                holder.imageView.setImageBitmap(response);
                imageCache.put(Integer.parseInt(row_pos.getItemID()), response);
                System.out.print(row_pos.getItemID()+"this is image");
            }
        }, 90, 90,
                Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("error", error.toString());
                    }
                });
        queue.add(request);
    }
    return convertView;
}
}

如何将我从数据库中获取的数据传递给新活动?另外我无法显示图像形式的PHP。我用过凌空图书馆。 This is the lisetview

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望在两个活动之间传输数据。你可以这样做:

//send data
mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //get the item and transform it to your JsonBean
        RowItem item= (RowItem) parent.getItemAtPosition(position);                         
        Intent intent = new Intent(items.this, ItemDetail.class);
        //put it in Intent,the item bean must implements Serializable
        intent .putExtra("RowItem",item)
        startActivity(intent);
    }

//receive data (in Activity of ItemDetail)
RowItem rowItem =(RowItem) getIntent().getSerializableExtra("RowItem");
// do something with rowItem,if your want know the position of item ,your alse can transport the positon(put the position in Intent object).