0

I am having a base adapter like below :

public class GridViewAdapter extends BaseAdapter {

Context cont;
........

@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {

    View v;
    TextView tv;
    GridItemObject itemObject = (Constants.gridItemsList.get(arg0));
    if (convertView == null) // if it’s not recycled, initialize some  attributes
    { 
        LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.grid_item, null);

        tv = (TextView) v.findViewById(R.id.grid_item_textView1);

                    if(itemObject.isvisited)
                    {
                     tv.setText(itemObject.letter);
                    }
        }

    } 
    else 
    {
        v = (View) convertView;
    }
    return v;

}

Constants.gridItemsList is a static list of type List < GridItemObject > which has few items(text set from 1 to 10) initially set with isvisited property as false.

The GridItemObject is a class which has definition as below :

    public class GridItemObject 
    {
      public String letter;
      public boolean isvisited;
    }

I want this gridview to be refreshed from another gridview which is as follows :

public class CustomGridViewAdapter extends BaseAdapter {

    Context cont;
    GridViewAdapter adapter;
    // setting the values in constructor....

    @Override
    public int getCount() {
    return 10;
    }

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
    View v;
    TextView tv;
    LayoutInflater li = (LayoutInflater) cont
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.numbers_grid_item, null);

        tv = (TextView) v.findViewById(R.id.numbers_grid_item_textView1); 
    tv.setText(arg0+"");

    tv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
      GridItemObject itemObject = (Constants.gridItemsList.get(arg0));
      itemObject.isvisited = true;
      adapter.notifyDataSetChanged();
    }
  });

I am using this gridview in my activity.

The issue is, I am not able to refresh GridViewAdapter adapter from CustomGridViewAdapter. When i click an item in CustomGridViewAdapter's gridview and check what's happening all the numbers are jumbled. Could someone suggest me on how to refresh a gridview from another (while using custom adapters in seperate files)

G_S
  • 7,068
  • 2
  • 21
  • 51

1 Answers1

0

A few things to check.

  1. After you called adapter.notifyDataSetChanged() is your GridViewAdapter's getView invoked ?

  2. In your GridViewAdapter i find a few issues and this could be a reason why it is not populated.

you set the text only if convertView is null.

Try this

`@Override public View getView(int arg0, View convertView, ViewGroup arg2) {

View v = convertView;
TextView tv;
GridItemObject itemObject = (Constants.gridItemsList.get(arg0));
Holder h;
if (v== null) // if it’s not recycled, initialize some  attributes
{ 
    h = new Holder();
    LayoutInflater li = (LayoutInflater) cont
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = li.inflate(R.layout.grid_item, null);

    h.tv = (TextView) v.findViewById(R.id.grid_item_textView1);
    v.setTag(h);

    }

} 
else 
{ 
   h = (Holder)v.getTag();
}
   if(itemObject.isvisited)
       {
         h.tv.setText(itemObject.letter);
       } 

return v;

}

and now you should have the Holder class

private static class Holder{ TextView tv; };

prijupaul
  • 2,076
  • 2
  • 15
  • 17
  • so if v == null, is it that it's not necessary to check itemobject.isvisited condition? – G_S Oct 08 '13 at 03:59
  • I tried the way you suggested. I am able to refresh the adapter but it is behaving weird. The number that i clicked is being replaced in the reverse order too. Some thing like this. If i pressed 1, the first griditem and the last grid item are replaced with 1. I tried even debugging. Everything worked fine while debugging. But after stopping debugging again the same behavior.Something went wrong somewhere Do you have any other suggestion please. – G_S Oct 09 '13 at 01:58
  • Could you please check this one .. http://stackoverflow.com/questions/19281020/baseadapters-getview-showing-irrelevant-information – G_S Oct 09 '13 at 19:37