Get the values from GridView items

空扰寡人 提交于 2019-12-02 08:22:13

问题


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

`


回答1:


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.



来源:https://stackoverflow.com/questions/48979008/get-the-values-from-gridview-items

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