如何在GridView中获取TextView项

时间:2013-10-13 00:31:31

标签: android gridview state

我有一个用户可以选择的数字列表。选择一个号码后,我想保存所选的号码状态,并能够在应用重新启动时恢复这些号码的状态。

目前,我很难在GridView中获取项目,以便在调用onCreateView时恢复其状态。我尝试调用gridView.getChildCount()并且我注意到它返回0.我已经阅读了许多类似问题的线程并尝试了他们的答案但没有成功。我已经在这几天了。

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

}

1 个答案:

答案 0 :(得分:1)

感谢thread,我找到了答案。 基本上,您需要创建一个addOnGlobalLayoutListener并在GridView完成绘图后调用getChildAt。如果你过早地调用它,它将返回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);
    }
});