2017-04-06 39 views
0

我正在製作android應用程序,並且我有一個來自json解析的數據庫的listview。我想在列表視圖中的Onclick項目打開其他活動,我沒有問題,當任何項目點擊時打開新的活動。但問題是我不知道我點擊了哪個項目,並將項目細節顯示給新的活動。Android的listview如何知道我點擊並顯示在其他活動中的哪個項目

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

+0

的[拔下列表視圖機器人的點擊項目]可能的複製(http://stackoverflow.com/questions/32295051/remove-an-clicked-item-from-listview- android) –

+0

雖然不一樣,http://stackoverflow.com/a/32295106/2684提供了你正在尋找的東西。被點擊的項目有一個位置。該位置來自適配器,從適配器中獲取該物品。這已經完成,問,回答和談論,在任何搜索引擎和在這裏堆棧溢出巨大的時間。 –

回答

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). 
相關問題