How do I get the selected item from a Gridview with ImageAdapter? (Android)

a 夏天 提交于 2020-01-10 05:01:13

问题


I had used a ListView to show some data (String) retrieved from the database...quite simple, but one of my cooworkers decided to put an image in front of the text in the screen. We were not able to do it with Listview, but we found some example with GridView that looked exactly as we need then we spent some time using GridView and did put the image in the front of the text.

Now, unfortunately we don't know how to get the selected item (based on the Text that comes from the item selected)

I'm using OnItemClickListener, and within onItemClick the method gridView.getItemSelectedAtPosition(), but everything i get is "null".

I don't now if ImageAdapter has something to do with it, cuz if I change it to an ArrayAdapter I can get the right item properly but there is no image in the row in this way.

gridview.setOnItemClickListener(new OnItemClickListener() {  
   public void onItemClick(AdapterView<?> parent, View v, int position, long id{     String selectedItem = (String) (gridView.getItemAtPosicion(position)); //always null     } });

what can I do?

EDITED - ListView source

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private final String[] mobileValuesD;

public ImageAdapter(Context context, String[] mobileValues, String[] mobileValuesD) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileValuesD = mobileValuesD;
}

  public View getView(int position, View convertView, ViewGroup parent) { 
  LayoutInflater inflater = (LayoutInflater) context.getSystemService           (Context.LAYOUT_INFLATER_SERVICE);
  View gridView;

  if (convertView == null) {

  gridView = new View(context);

   // get layout from mobile.xml
    gridView = inflater.inflate(R.layout.pesquisa_2, null);

  // set value into textview
    TextView textView = (TextView) gridView.findViewById(R.id.grid_item_label);
     textView.setText(mobileValues[position]+"\n"+mobileValuesD[position]);

 // set image based on selected text
  ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);

  String mobile = mobileValues[position];

 //SELECT DAS IMAGENS

  if (mobile.equals("pdt1")) {
     imageView.setImageResource(R.drawable.img1);
  } else if (mobile.equals("prd2")) {
      imageView.setImageResource(R.drawable.feijao);
   } else {
       imageView.setImageResource(R.drawable.acucar);
    }


   } else {
    gridView = (View) convertView;
   }

    return gridView;
 }

   public int getCount() {
        return mobileValues.length;
 }

  public Object getItem(int position) {
      return null;
 }

   public long getItemId(int position) {
      return 0;
 }

 }

回答1:


gridView.getItemAtPosition(position) calls the adapter's getItem(int position) under the scene - so implement getItem in your ImageAdapter class to return sth that will allow you to identify what is selected (maybe mobileValues[position] is enough or just what you write into textview: mobileValues[position] + "\n" + mobileValuesD[position]).

On the other hand side in onItemClick(AdapterView<?> parent, View v, int position, long id) you have allready have the position of selected item maybe it is enough information? what you are about to do in that listener?

I have put some changes to your adapter:

public class ImageAdapter extends BaseAdapter {

    private final String[] mobileValues;
    private final String[] mobileValuesD;

    public ImageAdapter(String[] mobileValues, String[] mobileValuesD) {
        this.mobileValues = mobileValues;
        this.mobileValuesD = mobileValuesD;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = ViewGroup.inflate(
                    parent.getContext(), R.layout.pesquisa_2, null);
        }

        ((TextView) view.findViewById(R.id.grid_item_label))
                .setText(getItem(position));

        ((ImageView) view.findViewById(R.id.grid_item_image))
                .setImageResource(getImageResForPosition(position));

        return view;
    }

    private int getImageResForPosition(int position) {
        String mobile = mobileValues[position];
        if (mobile.equals("pdt1")) {
            return R.drawable.img1;
        } else if (mobile.equals("prd2")) {
            return R.drawable.feijao;
        } else {
            return R.drawable.acucar;
        }
    }

    @Override
    public int getCount() {
        return mobileValues.length;
    }

    @Override
    public Object getItem(int position) {
        return mobileValues[position] + "\n" + mobileValuesD[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}


来源:https://stackoverflow.com/questions/10769989/how-do-i-get-the-selected-item-from-a-gridview-with-imageadapter-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!