从GridView项目获取值 [英] Get the values from GridView items

查看:77
本文介绍了从GridView项目获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,我有一个让我们说5x5的TextViews网格,并且我想检查整行或列是否具有相同的元素。我正在使用一个Adapter类来扩充我的GridView,只需一个textview元素。这是我尝试过的代码,但我似乎无法使它工作:

  final int size = gridView.getCount() ; // 25 
int temp = 0;

for(int i = 0; i< size; i ++){
ViewGroup gridChild =(ViewGroup)gridView.getChildAt(i);
childSize = gridChild.getChildCount();

for(int j = 0; j< childSize; j ++){

if(gridChild.getChildAt(j)instanceof TextView&&
( (TextView)gridChild.getChildAt(j))。getText()。toString()。equals(x)){
temp ++;
}

事情是当我试图调试时,调试器为childSize变量显示空值并且无法从getChildAt正确获取值。基本上,我试图做的是进入if语句。这也是我第一次使用ViewGroup calss以及我所调用的方法。任何帮助将不胜感激。

编辑:我正在寻找一种方法来在适配器类的getView方法之外执行此操作,而不是在onClick方法中执行此操作。 (代码示例答案将高度赞赏)。此外,getChildAt方法调用返回null,所以我显示的代码将不起作用,因为我将一个空值分配给gridChild。



这是我用于TextViews的onClick:

`

  public void numberFill(View view){
if(((TextView)view).getText()。toString()。isEmpty()){
((TextView)view).setText(String.valueOf(numbCounter + 1));
numbCounter ++; $(b)b $ b}

if(!((TextView)view).getText()。toString()。isEmpty()&& numbCounter> = 16){
((TextView)view).setText(x);


这是我的适配器类:

  public class GridAdapter extends BaseAdapter {

private final Context mContext;
private String []数字;


public GridAdapter(Context context,String [] numbers){
this.mContext = context;
this.numbers =数字;
}


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


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


@Override
public Object getItem(int position){
return numbers [position];
//返回null;

$ b @Override
public View getView(int position,View convertView,ViewGroup parent){
LayoutInflater inflater =(LayoutInflater)
mContext.getSystemService (Context.LAYOUT_INFLATER_SERVICE);

查看gridView;


if(convertView == null){

gridView = new View(mContext);


gridView = inflater.inflate(R.layout.textview_layout,null);


TextView textView =(TextView)gridView.findViewById(R.id.cell);
textView.setText(numbers [position]);

} else {
gridView =(View)convertView;
}

返回gridView;
}

}

numberFill reworked:

  public void numberFill(View view){
int index =(Integer)view.getTag();
if(numbers [index] .toString()。isEmpty()){
numbers [index] = String.valueOf(numbCounter + 1);
numbCounter ++;


else if(!numbers [index] .toString()。isEmpty()&& numbCounter> = 25){
numbers [index] = X;
}

gridAdapter.notifyDataSetChanged();
}

`

使用 AdapterView 时 - 比如你的 GridView - 你通常不会' t希望直接访问和操作其适配器之外的子< View s。相反,支持 Adapter 的数据集应该更新,然后 GridView 刷新。



在你的情况下,你可能在你的 Activity 中有类似的设置:

  private GridAdapter gridAdapter; 
private String []数字;

@Override
protected void onCreate(Bundle savedInstanceState){
...
numbers = new String [25];
gridAdapter = new GridAdapter(this,numbers);

$ / code>

这里,数字 array是你想要直接修改的,而不是 GridView 的子 TextView s上的文本。然后,该数组可以很容易地迭代以进行行和列值检查。

由于数组将在 Activity ,我们需要一种方法来传递中点击的 TextView 位置 Adapter Activity 的click方法,因为我们需要它来访问正确的数组元素。为此,我们可以通过 setTag()和<$来利用所有 View 的可用标签属性c $ c> getTag()方法。例如,在 GridAdapter getView()方法中:

  ... 
TextView textView =(TextView)gridView.findViewById(R.id.cell);
textView.setText(numbers [position]);
textView.setTag(position);
...

在点击方法中,位置可以通过 getTag(),并用作索引来从数字中获取点击的 TextView 的文本数组。然后,您可以使用该文本进行必要的处理或计算,将修改后的值设置回数组元素,并在适配器上触发刷新。

  public void numberFill(View view){
int index =(Integer)view.getTag();

//用数字处理[index]

数字[index] =新值;

gridAdapter.notifyDataSetChanged();

notifyDataSetChanged() call会导致 GridView 更新它的子元素,并且你的新值将被设置在适当的 TextView 中。现在数字数组也具有当前值,并且可以在 Activity 中随时进行必要的检查。

I am working on a project where I have a let's say 5x5 grid of TextViews and I want to check if an entire row or column has equal elements. I am using an Adapter class to inflate my gridview with simply one textview element. Here is the code that I have tried but I cannot seem to make it work:

 final int size = gridView.getCount(); //25
 int temp = 0;

    for (int i = 0; i < size; i++) {
        ViewGroup gridChild = (ViewGroup) gridView.getChildAt(i);
        childSize = gridChild.getChildCount();

        for (int j = 0; j < childSize; j++) {

            if (gridChild.getChildAt(j) instanceof TextView &&
                    ((TextView) gridChild.getChildAt(j)).getText().toString().equals("x")) {
                temp++;
            }

The thing is when i tried to debug, debugger showed null values for childSize variable and could not properly get the value from getChildAt. Basically, what I am trying to do is get inside the if statement. Also this is the first time I am working with ViewGroup calss, and the methods that I call. Any help would be appreciated.

Edit:I am looking for a way to do this outside the getView method in the adapter class and not in a onClick method as well. (Code sample answers would be highly appreciated). Also, the getChildAt method call returns null so the code I have shown would not work because I am assigning a null value to the gridChild.

This is the onClick that I use for the TextViews:

`

 public void numberFill(View view) {
        if (((TextView) view).getText().toString().isEmpty()) {
            ((TextView) view).setText(String.valueOf(numbCounter + 1));
            numbCounter++;
        }

        else if (!((TextView) view).getText().toString().isEmpty() && numbCounter >= 16) {
            ((TextView) view).setText("x");
        }
    }

This is my adapter class:

public class GridAdapter extends BaseAdapter {

    private final Context mContext;
    private  String[] numbers;


    public GridAdapter(Context context, String[] numbers) {
        this.mContext = context;
        this.numbers = numbers;
    }


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


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


    @Override
    public Object getItem(int position) {
        return numbers[position];
        //return null;
    }

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

        View gridView;


        if (convertView == null) {

            gridView = new View(mContext);


            gridView = inflater.inflate(R.layout.textview_layout, null);


            TextView textView = (TextView) gridView.findViewById(R.id.cell);
            textView.setText(numbers[position]);

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

}

numberFill reworked:

    public void numberFill(View view) {
    int index = (Integer) view.getTag();
    if (numbers[index].toString().isEmpty()) {
        numbers[index] = String.valueOf(numbCounter + 1);
        numbCounter++;
    }

    else if (!numbers[index].toString().isEmpty() && numbCounter >= 25) {
        numbers[index] = "x";
    }

    gridAdapter.notifyDataSetChanged();
}

`

解决方案

When using an AdapterView – such as your GridView – you generally don't want to directly access and manipulate its child Views outside of its Adapter. Instead, the dataset backing the Adapter should be updated, and the GridView then refreshed.

In your case, you presumably have a setup similar to this in your Activity:

private GridAdapter gridAdapter;
private String[] numbers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    numbers = new String[25];
    gridAdapter = new GridAdapter(this, numbers);
}

Here, the numbers array is what you want to directly modify, rather than the text on the GridView's child TextViews. That array is then easily iterated over to do your row and column value checks.

Since the array will be modified in the Activity, we need a way to pass the clicked TextView's position in the Adapter to the Activity's click method, as we'll need it to access the correct array element. For this, we can utilize the tag property available on all View's, via the setTag() and getTag() methods. For example, in GridAdapter's getView() method:

...
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
textView.setTag(position);
...

In the click method, the position can be easily retrieved with getTag(), and used as the index to get the clicked TextView's text from the numbers array. You can then do the necessary processing or calculation with that text, set the modified value back to the array element, and trigger a refresh on the Adapter.

public void numberFill(View view) {
    int index = (Integer) view.getTag();

    // Do your processing with numbers[index]

    numbers[index] = "new value";

    gridAdapter.notifyDataSetChanged();
}

The notifyDataSetChanged() call will cause the GridView to update its children, and your new value will be set in the appropriate TextView. The numbers array now also has the current values, and is readily available in the Activity to perform the necessary checks there.

这篇关于从GridView项目获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆