我需要帮助我在Android工作的项目。我有一个AsyncTask,通过互联网使用自定义适配器收集外部数据。
我的视图目前有一个由适配器生成的视图,在它之外,包含一个按钮。我想按下那个按钮,从视图中获取当前信息,例如元素的名称。
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;
}
}
简而言之,按下按钮时如何获取信息。非常感谢大家,问候
答案 0 :(得分: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();
}
<强>更新强> 了解OPs要求后。
在重写的getCardView
处理程序中,在实例化ImageView
之后
ImageView image = (ImageView) convertView.findViewById(R.id.sp_image);
从模型对象设置其标记,如
image.setTag(model.getTitle());
然后,在您的点击处理程序中,获取ImageView的标记
@Override
public void onClick(View button) {
ImageView image = (ImageView) getView().findViewById(R.id.sp_image);
String item = (String)image.getTag();
}