0
我有一個用戶可以選擇的數字列表。選擇一個號碼後,我想要保存該號碼的選定狀態,並且能夠在應用程序重新啓動時恢復這些號碼的狀態。如何在GridView中獲取TextView項目
當我調用onCreateView
時,我很難在GridView中恢復它們的狀態。我試過撥打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;
}
}