Tuon Can
Tuon Can

Reputation: 71

How to get TextView item in GridView

I have a list of numbers that the user can select. Once a number is selected, I want to save the selected state of the number and be able to restore the state of these numbers when the app restarts.

Currently I have a hard time getting the items in the GridView to restore their state when the onCreateView is called. I tried call the gridView.getChildCount() and I notice it returns 0. I've read many threads with similar issue and tried their answer but have no success. I have been on this for a couple days already.

Fragment1.java:

public class Fragment1 extends Fragment implements OnItemClickListener {

public static final String ARG_SECTION_NUMBER = "section_number";

private static final int START_NUM = 1;
private static final int END_NUM = 59;
public static boolean[] num_select = new boolean[END_NUM - START_NUM + 1];

private static GridView gridView;
private static NumberAdapter adapter;

public Fragment1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_1, container, false);

    gridView = (GridView) rootView.findViewById(R.id.gridview_number);

    adapter = new NumberAdapter(getActivity(), START_NUM, END_NUM);
    gridView.setAdapter(adapter);
    //gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);

    gridView.setOnItemClickListener((OnItemClickListener) this);

    // ************* I NEED TO BE ABLE TO RESTORE THE NUMBER STATE HERE *******
//restoreNumbers();

    return rootView;
}   

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

num_select[position] = !num_select[position];

    if (num_select[position])
    {
        v.getBackground().setColorFilter(Color.rgb(210, 230, 230), 
    }
    else
    {
        v.getBackground().clearColorFilter();           
    }       
}

NumberAdapter.java:

public class NumberAdapter extends BaseAdapter {
private Context context;
private int min_num;
private int max_num;    

public NumberAdapter(Context c, int num1, int num2) {
    this.context = c;        
    this.min_num = num1;
    this.max_num = num2;

    total_num = max_num - min_num + 1;       
}    

public View getView(int position, View convertView, ViewGroup parent) {

    TextView textView;

    if(convertView == null)
    {
        textView = new TextView(context);
        textView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 85));
        textView.setTextSize(18.0f);
        textView.setGravity(0x11);
    }
    else
    {
        textView = (TextView) convertView;
    }    

    textView.setText(Integer.toString(position));
    textView.setBackgroundResource(R.drawable.number_deselect);  

    return textView;
}

public int getCount() {
    return total_num;
}

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

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

}

Upvotes: 0

Views: 2405

Answers (1)

Tuon Can
Tuon Can

Reputation: 71

I have found my answer thanks to this thread. Basically you will need to create an addOnGlobalLayoutListener and call getChildAt after the GridView is done drawing. If you call it too early, it will return a view of null.

ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        TextView textView = (TextView) gridView.getChildAt(i);
        textView.getBackground().setColorFilter(Color.rgb(210, 230, 230), PorterDuff.Mode.MULTIPLY);
    }
});

Upvotes: 1

Related Questions