通过Android上的外部按钮获取适配器信息

时间:2014-07-19 16:03:46

标签: java android button android-asynctask adapter

我希望通过外部按钮从视图的当前项目接收信息,因为它显示图像,按钮位于适配器之外。任何人都可以帮助我吗?

图片: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;
    }

非常感谢所有人,问候!

1 个答案:

答案 0 :(得分: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