gpt4 book ai didi

java - 如何在Android中获取带有按钮的项目信息

转载 作者:行者123 更新时间:2023-12-01 12:45:15 26 4
gpt4 key购买 nike

我需要有关我正在工作的 Android 项目的帮助。我有一个 AsyncTask,它使用自定义适配器通过互联网收集外部数据。

我的 View 当前有一个由适配器生成的 View ,并且在其外部包含一个按钮。我想按下该按钮,从 View 中获取当前信息,例如元素的名称。

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

私有(private)类 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;
}
}

简而言之,按下按钮时如何获取项目信息。非常感谢大家,问候

最佳答案

您应该使用tag Android 中 View 的属性。

因此,您可以动态设置标签,例如

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 的标签,例如

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

关于java - 如何在Android中获取带有按钮的项目信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24768178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com