0

I have implemented an ArrayAdapter for customizing a GridView. The layout of each item is defined in xml code that produce something like this:

layout GridView item

In my Activity I have implemented the following code:

GridView gridview = (GridView) findViewById(R.id.gridview);
        adapter = new myArrayAdapter(this, articlesList);
        gridview.setAdapter(new myArrayAdapter(this, articlesList));     
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // here I want to change the text of the `TextView` in the right bottom corner to "CLICKED"
            }
        });

As explained in the commented section of the code, I simple want to update the TextView in the right bottom corner, writing in it the text "CLICKED".

mmBs
  • 8,421
  • 6
  • 38
  • 46
GVillani82
  • 17,196
  • 30
  • 105
  • 172

3 Answers3

1

The View parameter to AdapterView.OnItemClickListener onItemClick() is the view that was clicked. In your case it will be the item root RelativeLayout. Call findViewById() on it or use the ViewHolder pattern to obtain a reference to the TextView you want to update and just update it.

Also note that when the views are recycled i.e. your adapter's getView() is called with a non-null convertView, you need to reset the TextView to its default state.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Thank you @laalto Yes, I'm using recycling views, but if I reset the TextView to default value, I lost the text "CLICKED" in my TextView – GVillani82 Sep 11 '13 at 13:17
0

You may not need to set a TextView to "Clicked", if all you want to do is give the user the feedback that they have checked an item. An alternative might be to do the following. Note, this method will also help you set a textview to "clicked" if you still need to.

To check an item I would extend your RelativeLayout as follows:

public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
    private boolean mChecked;

    private static final int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };

    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    public void toggle() {
        setChecked(!mChecked);
    }

    public boolean isChecked() {
        return mChecked;
    }

    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    }
}

Then you can call parent.setItemChecked(position,true); in onItemClick which will actually set the item as checked within the AdapterView.

Doing this will then allow you to set a background selector on the extended RelativeLayout and the items will perform selected, pressed, checked etc actions. However, you don't need to use a background selector.

There is more, you can alway see whether the view is checked with gridview.getCheckedItemPosition() which you may find useful if you want to actually set the textview to "Clicked".

gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        parent.setItemChecked(position,true);
        yourAdapter.ViewHolder vh = (yourAdapter.ViewHolder) v.getTag();
        vh.textView.setText("Clicked");
    }
});

In your adapter you can hold a reference to you extended relative layout in your static ViewHolder class and if checked (e.g. holder.extendedrelativeLayout.isChecked()) then make sure you set the textView to "Clicked".

HGPB
  • 4,346
  • 8
  • 50
  • 86
0

try gridView.invalidateViews(); it automatically calls getView() method of the adapter and updates the view.

inkedTechie
  • 684
  • 5
  • 13