Home > DeveloperSection > Forums > How to calling a fragmnet inside its ListView Adapter?
Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    October-14-2014 12:48 AM

 Android Android  Mobile Development 
Ratings:


 2 Reply(s)
 626  View(s)
Rate this:
I have a Fragment which has a ListView inside it , in each row of my ListView i have a delete Button which works perfectly, what i need is to refresh my ListView to show the updated data. my question is how can i refresh my ListView inside its Adapter. this is my Adapter:

public class BrowserBannedListViewAdapter extends BaseAdapter {
Context mContext;
private static List<BrowserBannedSites> listUrlForAdapter;
AppSQLiteHelper db;




public BrowserBannedListViewAdapter(Context c, List<BrowserBannedSites> urls) {
    mContext = c;
    listUrlForAdapter = urls;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listUrlForAdapter.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

static class ViewHolder {
    TextView text;
    ImageView icon;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    LayoutInflater li = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = li.inflate(R.layout.row_fragment_browser_url, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView
                .findViewById(R.id.tv_fragment_browser_url_name);
        viewHolder.icon = (ImageView) convertView
                .findViewById(R.id.iv_fragment_browser_delete);

        viewHolder.icon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                        db=new AppSQLiteHelper(mContext);

                int tempID =listUrlForAdapter.get(position).get_Id();
                db.deleteBannedSites(tempID);
  //******************** here is where i want to refresh my listview

            }
        });
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
      viewHolder.text.setText(listUrlForAdapter.get(position).get_browserBannedSitesAddress());

    return convertView;
}

}


Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Supported
Posted on    October-14-2014 12:49 AM

Only deleting from database will not working. you need to delete from List too.

deleteButton.setOnClickListener(new OnClickListener(){
   public void onClick(View v){

      .........
      listUrlForAdapter.remove(position); // position is the position of the custom view in the list 
      notifyDataSetChanged();
    }
}

Pawan Shukla

Total Post:34

Points:246
Supported
Posted on    October-14-2014 12:50 AM

change first the List of your data and You can directly call notifyDataSetChanged();

@Override
        public void onClick(View v) {
                    db=new AppSQLiteHelper(mContext);

            int tempID =listUrlForAdapter.get(position).get_Id();
            db.deleteBannedSites(tempID);
     //******************** here is where i want to refresh my listview
            notifyDataSetChanged();
        }
    });

Don't want to miss updates? Please click the below button!

Follow MindStick