English 中文(简体)
如何从图像设计器的网格视图中获取选中的项目? (Android)
原标题:How do I get the selected item from a Gridview with ImageAdapter? (Android)

我用“ListView”来显示从数据库中检索到的一些数据(String)...很简单,但我的一位同事决定将一个图像放在屏幕的文本前面。我们无法用“ListView”做到这一点,但我们找到了一些GridView的例子,这个例子与我们需要的一模一样,然后我们用GridView(GridView)用了一些时间,把图像放在文本前面。

现在,不幸的是,我们不知道如何获得选中的项目(基于选定项目产生的文本)

我使用OnTroundClickListener, 并在项目ClickFlick这个方法网格里View. Get TroupsSeabectAtPostion (), 但我得到的只是“Null”。

如果图像设计师和它有什么关系, 我现在不这样做, 因为如果我把它换成一个阵列设计师......我可以正确得到正确的项目......但是在行里没有这样的图像。

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

我能怎么办?

编辑编辑 - 列表查看源

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]+"
"+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;
 }

 }
最佳回答

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] + " " + mobileValuesD[position]).

onTroundClick( AdapterView<?? & gt; 父、 view v、 int 位置、 long id) 的另一方面, 您已经具备了所选项目的位置, 也许足够信息吗? 您将在听众中做什么?

我对你的适应器做了一些修改:

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] + "
" + mobileValuesD[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签