Odin
Odin

Reputation: 682

Refresh visible ListView with Custom Adapter

So basically the idea is that I have 2 ListViews

Initially: The 2 listviews are both visible, the first one populated properly and the second one is empty but already shown

After clicking a row of first ListView: Data is fetched from Database and put properly in second one

Questions & Problems:

The data are being fetched properly, but the second ListView remains empty and the getView() isn't called at all.

1- Is there a way to load the second ListView without refreshing the page?

2- getView() isn't called because the row isn't visible right? How can I bypass this problem?

Tried using notifyDataSetChanged() then validating the second ListView

Much appreciated for any assistance

Code

CustomAdapter.java

public class CustomAdapter extends  ArrayAdapter<ListItem>
{  
LayoutInflater inflater;
List<ListItem> items;

public CustomAdapter(Context context, int textViewResourceId, List<ListItem> items2) {
    super(context, textViewResourceId);
    this.items = items2;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override  
public int getCount() {  
    return items.size();  
}  


@Override
public ListItem getItem(int position) {
        return ((CustomAdapter) items).getItem(position);
}

@Override  
public long getItemId(int position) {  
    return position;  
}
 
@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    
    final ListItem holder;
    final ListItem item = items.get(position);
    
    
    View vi=convertView;
    
    if(vi==null){
        vi = inflater.inflate(R.layout.popup_toping, null);
        holder = new ListItem();
        holder.text = (TextView) vi.findViewById(R.id.text);
        
        holder.Check = (CheckBox) vi.findViewById(R.id.check);
        
        vi.setTag(holder);
    }else{
        holder = (ListItem) vi.getTag();
   
    }
    
    
    holder.text.setText(item.t);
    
    holder.Check.setChecked(item.IsChecked);
    
    return vi;
  }

}

Implementation in main activity

List<ListItem> items = new ArrayList<ListItem>();;
items.add(new ListItem(){{
                            t= c.getString("first_text");
                            IsChecked = false;
                        }});
                        
                
CustomAdapter listadapter = new CustomAdapter(context, R.layout.popup, items);                      
myList.setAdapter(listadapter);
listadapter.notifyDataSetChanged();
myList.invalidate();

Upvotes: 3

Views: 1754

Answers (2)

Odin
Odin

Reputation: 682

After long debugging and frustration, I've finally solved my problem..

The issue: I was setting the adapter in doInBackground in AsyncTask

The solution: I set the adapter in onPostExecute and it have solved my problem.. apparently it was interfering with the custom adapter somehow..

I'd really appreciate it if anyone can explain why though..

Thanks a lot for those who tried to help me :)

Upvotes: 1

Jordan Hochstetler
Jordan Hochstetler

Reputation: 1416

I would advise you to look into implementing Fragments for a few reasons...

  1. Fragments have their own lifecycle
  2. Fragments can communicate in the same activity very easily
  3. Best practice for supporting good UI for tablets

enter image description here

So if you have a ListFragment A and Fragment B you can create a listener on "ListFragment B" so that when an item is selected on ListFragment A then Fragment B is populated with your specifications. I think this will help you out a lot. Look at this example in section 10 of this article: http://www.vogella.com/articles/AndroidFragments/article.html

Upvotes: 2

Related Questions