2014-07-15 100 views
0

我需要我在Android工作的項目的幫助。我有一個AsyncTask,通過互聯網通過自定義適配器收集外部數據。如何在Android中使用按鈕獲取項目的信息

我的視圖目前有一個由適配器生成並在其外部包含按鈕的視圖。我想按下該按鈕,從視圖中提供當前信息,例如元素的名稱。

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ebebeb" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/header" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="55dp" 
      android:gravity="center_vertical" 
      android:paddingLeft="35dp" 
      android:text="Menú" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#FFF" /> 

     <TextView 
      android:id="@+id/openmenu" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:background="@drawable/ic_action_overflow" 
      android:clickable="true" 
      android:contentDescription="Desc" 
      android:visibility="visible" /> 

     <TextView 
      android:id="@+id/love" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:background="@drawable/ic_action_love" 
      android:clickable="true" 
      android:contentDescription="Desc" 
      android:visibility="visible" /> 

     <TextView 
      android:id="@+id/refresh" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/love" 
      android:layout_alignBottom="@+id/love" 
      android:layout_marginRight="5dp" 
      android:layout_toLeftOf="@+id/love" 
      android:background="@drawable/ic_action_refresh" 
      android:clickable="true" 
      android:contentDescription="Desc" 
      android:visibility="visible" /> 
    </RelativeLayout> 

    <com.unomasdelafamilia.sevilla.CardContainer 
     android:id="@+id/layoutview" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#ebebeb" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" > 

     <Button 
      android:id="@+id/infobtn" 
      android:layout_width="75dp" 
      android:layout_height="75dp" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/lovebtn" 
      android:background="@drawable/love" /> 

    </RelativeLayout> 

</LinearLayout> 

MainActivity.class

私有類JSONParse延伸的AsyncTask {

@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); 
       String id = c.getString(TAG_ID); 
       String image = c.getString(imagen); 
       adapter.add(new CardModel(id, "Sevilla", image)); 
      } 

      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 
    } 

} 

CardModel.class

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; 
    } 

    /*public Drawable getCardLikeImageDrawable() { 
     return cardLikeImageDrawable; 
    } 

    public void setCardLikeImageDrawable(Drawable cardLikeImageDrawable) { 
     this.cardLikeImageDrawable = cardLikeImageDrawable; 
    } 

    public Drawable getCardDislikeImageDrawable() { 
     return cardDislikeImageDrawable; 
    } 

    public void setCardDislikeImageDrawable(Drawable cardDislikeImageDrawable) { 
     this.cardDislikeImageDrawable = cardDislikeImageDrawable; 
    } 
    */ 
/* public void setOnCardDimissedListener(OnCardDimissedListener listener) { 
     this.mOnCardDimissedListener = listener; 
    } 

    public OnCardDimissedListener getOnCardDimissedListener() { 
     return this.mOnCardDimissedListener; 
    } 


    public void setOnClickListener(OnClickListener listener) { 
     this.mOnClickListener = listener; 
    } 

    public OnClickListener getOnClickListener() { 
     return this.mOnClickListener; 
    }*/ 
} 

SimpleCardStackAdapter.java

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; 
     ImageView image = (ImageView) convertView.findViewById(R.id.sp_image); 

     ImageLoader imgLoader = new ImageLoader(mContext); 

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

     convertView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //Toast.makeText(getContext(), "Pulsado", Toast.LENGTH_SHORT).show(); 
       ((ViewManager)v.getParent()).removeView(v); 
      } 
     }); 
     return convertView; 
    } 
} 

總之,我怎麼能得到項目信息時,按下按鈕。非常感謝大家,Greetings

+0

什麼樣的商品信息?一個ID,字符串...? – AmmarCSE

+0

Hi @AmmarCSE我需要字符串 – Adrian

+0

是的,但是是動態字符串還是靜態分配給每個唯一項目? – AmmarCSE

回答

1

您應該使用Android中的視圖的tag屬性。

所以,你可以動態設置像

String item = "item"; 
Button button = findViewById(R.id.Button); 
button.setTag(item); 

其中,每當在你的代碼適當的標籤。

然後,當點擊喜歡

@Override 
    public void onClick(View button) { 
     String myString = (String) button.getTag(); 
    } 

更新 瞭解OP的要求後,你可以檢索標籤。

在你重寫getCardView處理程序,實例化後的ImageView

ImageView image = (ImageView) convertView.findViewById(R.id.sp_image); 

設置它的標籤從模型中的物體,像

image.setTag(model.getTitle()); 

那麼,在您單擊處理程序,得到的標籤ImageView like

@Override 
public void onClick(View button) { 
    ImageView image = (ImageView) getView().findViewById(R.id.sp_image); 
    String item = (String)image.getTag(); 
} 
+0

它的工作原理!但爲什麼只顯示我一個項目? https://dl.dropboxusercontent.com/u/29764789/captura.png – Adrian

+0

@Adrian那麼,每個按鈕每個標籤只能設置一個項目。如果您只有一個按鈕,那麼在您的代碼中,您可以根據自己的情況更改標籤。 – AmmarCSE

+0

對不起,我不明白@AmmarCSE。你可以發表一個簡短的例子嗎? – Adrian

相關問題