2014-07-19 61 views
-1

我想通過外部按鈕從當前的視圖項目接收信息,因爲它顯示圖像,該按鈕位於適配器之外。誰能幫我?通過Android上的外部按鈕獲取適配器信息

圖片:https://www.dropbox.com/s/ikdcjp7zmhyc1hm/image.png MainActivity.class

SimpleCardStackAdapter adapter = new SimpleCardStackAdapter(MainActivity.this); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.mainlayout); 
     new JSONParse().execute(); 
    } 

    private class JSONParse extends AsyncTask<String, String, JSONObject> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mCardContainer = (CardContainer) findViewById(R.id.layoutview); 

      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Obteniendo datos ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected JSONObject doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 
      JSONObject json = jParser.getJSONfromURL(url); 
      return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 

      if(json != null){ 
       pDialog.dismiss(); 
       try { 
        // Getting JSON Array 
        user = json.getJSONArray(TAG_USER); 
        for (int i = 0; i < user.length(); i++) { 
         JSONObject c = user.getJSONObject(i); 
         id = c.getString(TAG_ID); 
         String image = c.getString(imagen); 
         adapter.add(new CardModel(id, "Sevilla", image)); 
        } 
        //T 
lovebtn = (Button) findViewById(R.id.lovebtn); 
        lovebtn.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 
          // TODO Auto-generated method stub       
          Toast.makeText(getApplicationContext(), ""+item, Toast.LENGTH_SHORT).show(); 
         } 
        });*/ 

        mCardContainer.setAdapter(adapter); 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      }else{ 
       pDialog.dismiss(); 
       Toast.makeText(getApplicationContext(), "Compruebe su conexión e inténtelo de nuevo más tarde", Toast.LENGTH_SHORT).show(); 
       //No hay datos 
      } 

     } 
    } 

SimpleCardStackAdapter.class

public final class SimpleCardStackAdapter extends CardStackAdapter { 

    public SimpleCardStackAdapter(Context mContext) { 
     super(mContext); 
    } 

    @Override 
    public View getCardView(int position, CardModel model, View convertView, ViewGroup parent) { 
     if(convertView == null) { 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.std_card_inner, parent, false); 
      assert convertView != null; 
     } 

     int loader = R.drawable.ic_launcher; 
     final ImageView image = (ImageView) convertView.findViewById(R.id.sp_image); 

     ImageLoader imgLoader = new ImageLoader(mContext); 

     ((TextView) convertView.findViewById(R.id.textView1)).setText(model.getTitle()); 
     ((TextView) convertView.findViewById(R.id.textView2)).setText(model.getDescription()); 
     imgLoader.DisplayImage(model.getImage(), loader, image); 

     return convertView; 
    } 
} 

CardModel

public class CardModel { 

    private String title; 
    private String description; 
    //private Drawable cardImageDrawable; 
    private String image; 
    /* private Drawable cardLikeImageDrawable; 
    private Drawable cardDislikeImageDrawable;*/ 

    private OnCardDimissedListener mOnCardDimissedListener = null; 

    private OnClickListener mOnClickListener = null; 

    public interface OnCardDimissedListener { 
     void onLike(); 
     void onDislike(); 
    } 

    public interface OnClickListener { 
     void OnClickListener(); 
    } 

    /*public CardModel(String string, Drawable drawable) { 
     this(null, null, null); 
    }*/ 

    public CardModel(String title, String description, String image) { 
     this.title = title; 
     this.description = description; 
     //this.cardImageDrawable = cardImage; 
     this.image = image; 
    } 

    public CardModel(String title, String description, Bitmap cardImage) { 
     this.title = title; 
     this.description = description; 
     //this.cardImageDrawable = new BitmapDrawable(null, cardImage); 
    } 

    public CardModel(HashMap<String, String> map) { 
     this.title = title; 
     this.description = description; 
     this.image = image; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public String getImage(){ 
     return image; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 


    public void setImage(String image) { 
     this.image = image; 
    } 

非常感謝大家,問候!

回答

0

您可以使用ListView的setOnItemClickListener獲取信息,並使用外部按鈕來呈現顯示詳細信息的界面。

讓我們只說:

ListView list = (ListView) findViewById(R.id.listview); 
list.setOnItemClickListener(new AdapterView.onItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
     Object listItem = list.getItemAtPosition(position); 
    } 
}); 

this post

+0

嗨參考,我目前沒有使用任何名單 – Adrian