Question

I'm trying to create a gridview that gives the possibility to the user to create a new child clicking on the last item on the gridview. I have a problem redrawing the view. In fact after the user creates a new child through an alertdialog I need to refresh the gridview to make this child visible. I've been looking for a solution for a while and everything I found is:

adapter.notifyDataSetChanged();
gridView.invalidateViews();
gridView.setAdapter(adapter);

Which is part of my code but doesn't,at least apparently, do anything. The code below is just the onCreateView() from the PlaceholderFragment class in the android template for an activty with swiping tabs. The app development here is still at the beginning but I'm already stuck with this issue..

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

}

UPDATE after code posting requests.

Here is the GridAdapter. Everything works fine, the only thing is that I still have to close and reopen the app to see the gridview redrawn and updated.

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

Thanks for helping

Was it helpful?

Solution

You only load a list of files when the adapter is created in the constructor. As you are not creating a new constructor the filesYet() method is not called again and the file list is not updated.

To make this update you could override the notifydatasetchanged function in the adapter

@Override
public void notifyDataSetChanged (){
    filesyet = filesYet();
    super.notifyDataSetChanged();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top