如何在Android中使用按钮获取项目信息

阿德里安

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

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

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

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

AmmarCSE

您应该在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();
    }

更新了解操作要求之后。

在您覆盖的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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android Kotlin中使用onclicklistner获取按钮文本?

如何在Android中使用XMLPULLPARSER从XML获取项目?

如何在UICollectionViewCell中使用详细信息披露按钮?

如何在firebase for android中使用身份验证UId从用户节点获取用户信息?

如何在firebase中使用uid获取用户信息?

如何在SwiftUI中使用按钮从数组中删除项目?

如何在Android项目中使用java反射获取超类中的子类列表

如何在android java中使用volley进入信息对象

如何在Android中使用onclick更改按钮?

Kotlin:如何在android中使用按钮从片段打开活动

如何在UIBarButtonItem上使用信息按钮

如何在Android库项目中使用匕首

如何在Eclipse中使用android项目库?

如何在libgdx项目中使用android类?

如何在Android项目中使用@WorkerThread批注?

如何在Android中使用MutableLiveData更新仅更改的项目?

如何在Android Studio项目中使用最新的FFMPEG?

如何在android studio项目中使用Material Design

如何在android studio项目中使用Sketch设计

如何在android中获取Facebook信息

如何在Shell脚本中使用条件从文件中获取信息

如何在 Hive 查询中使用 row_number 来获取最新的用户登录信息?

如何在Java中使用Jenkins客户端获取Jenkins工作详细信息列表

如何在Python中使用inspect从被呼叫者获取呼叫者的信息?

如何在Node.js中使用repl获取模块功能的详细信息?

如何在 C# 中使用 UserPrincipal 获取 Active Directory 中地址的详细信息

如何在JSP页面中使用会话来获取信息?

如何在mysql中使用按日期顺序获取两个表的详细信息

如何在Linux操作系统中使用Java获取服务器时区信息