我正在尝试创建一个gridview,使用户可以创建一个新的子项,点击gridview上的最后一项。我在重绘视图时遇到问题。事实上,在用户通过alertdialog创建一个新孩子后,我需要刷新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;
}
}
感谢您的帮助
答案 0 :(得分:1)
只有在构造函数中创建适配器时才加载文件列表。由于您没有创建新的构造函数,因此不会再次调用filesYet()方法,并且不会更新文件列表。
要进行此更新,您可以覆盖适配器中的notifydatasetchanged函数
@Override
public void notifyDataSetChanged (){
filesyet = filesYet();
super.notifyDataSetChanged();
}