我正在嘗試創建一個GridView,它可以讓用戶創建一個點擊GridView上最後一項的新子項。我有一個重繪視圖的問題。事實上,在用戶通過alertdialog創建一個新的孩子後,我需要刷新gridview才能看到這個孩子。我一直在尋找了一段時間,一切的解決方案,我發現是:Android:在添加子項後重繪GridView
adapter.notifyDataSetChanged();
gridView.invalidateViews();
gridView.setAdapter(adapter);
這是我的代碼的一部分,但不會,至少表面上,做任何事情。下面的代碼只是android模板中的PlaceholderFragment類的onCreateView()方法,用於通過滑動選項卡進行活動。這裏的應用程序開發仍然是開始,但我已經堅持這個問題..
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_one, container, false);
final GridView gridView = (GridView) rootView.findViewById(R.id.gridview);
final GridAdapter adapter =new GridAdapter(getActivity());
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position==0){
LayoutInflater inflater = getActivity().getLayoutInflater();
View newidea= inflater.inflate(R.layout.newidea, null);
final TextView txtname = ((TextView) newidea.findViewById(R.id.name));
final TextView txtdesc = ((TextView) newidea.findViewById(R.id.desc));
final Spinner spinner=((Spinner)newidea.findViewById(R.id.spinner));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(R.drawable.type_add)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String name = txtname.getText().toString();
String desc = txtdesc.getText().toString();
DataReceiver dr = new DataReceiver(getActivity().getBaseContext(), name, desc,Integer.toString(spinner.getSelectedItemPosition()));
dr.writeFile();
dialog.dismiss();
adapter.notifyDataSetChanged();
gridView.invalidateViews();
gridView.setAdapter(adapter);
}
}).setNegativeButton("Forgot the idea?", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).setView(newidea)
.setTitle("New Idea");
AlertDialog dialog = builder.create();
dialog.show();
} else {
Toast.makeText(
getActivity(),
((TextView) v.findViewById(R.id.grid_item_label))
.getText(), Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
}
代碼發佈請求後更新。
這是GridAdapter。一切工作正常,唯一的是,我仍然必須關閉並重新打開應用程序,以查看gridview重繪和更新。
public class GridAdapter extends BaseAdapter {
private Context context;
boolean filesyet;
File f[];
public GridAdapter(Context context) {
this.context = context;
filesyet=filesYet();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.griditem, null);
TextView title = (TextView) gridView
.findViewById(R.id.grid_item_label);
LinearLayout background = (LinearLayout) gridView
.findViewById(R.id.grid_item);
if(position==0){
title.setText("");
Drawable d= context.getResources().getDrawable(R.drawable.type_add);
background.setBackground(d);
}else {
title.setText(f[position].getName());
Drawable d;
int selected=0;
try {
byte[] buffer = new byte[1];
new FileInputStream(f[position]).read(buffer,0,buffer.length);
selected=Integer.valueOf(new String(buffer));
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
switch(selected){
case 1:
d=context.getResources().getDrawable(R.drawable.type_app);
break;
case 2:
d=context.getResources().getDrawable(R.drawable.type_engi);
break;
case 3:
d=context.getResources().getDrawable(R.drawable.type_event);
break;
default:
d=context.getResources().getDrawable(R.drawable.eureka);
break;
}
background.setBackground(d);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
if (filesyet) {
return f.length;
} else {
return 1;
}
}
Boolean filesYet(){
try {
String path = context.getFilesDir().toString();
this.f = new File(path).listFiles();
System.out.println(f.toString());
return true;
} catch (NullPointerException e){
System.out.println("NULL");
return false;
}
}
感謝您的幫助
貴適配器知道它有一個新的項目? DataReceiver的.writefile()是做什麼的? –
是不是notifydatasetchanged告訴適配器它有一個新的項目? DataReceiver的writefile()將名稱和描述寫入應用程序專用目錄中的文件中。它稍後將用作數據庫來存儲gridview上所有項目的信息(點擊它們應該會打開一個包含所有項目信息的新屏幕) –
是的,'notifyDataSetChanged'告訴適配器它有一個新的項目。但問題是你是否將這個新項目添加到適配器的數據?如果你不添加它,那麼當你通知時,它不會找到新的項目。請發佈你的'GridAdapter'代碼.. –