lundi 9 février 2015

android fragment sqlite how to fetch all data

android fragment unable to display sqlite data in listview below is my code . i dont understand what i am doing wrong.


public class WindowsFragment extends ListFragment {



@SuppressWarnings("null")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */

ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = SqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
ContactListItems contactListItems = new ContactListItems();

contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));

} while (c1.moveToNext());
}
}
c1.close();

ContactListAdapter contactListAdapter = new ContactListAdapter(
getActivity(), contactList);


ListView simple_list_item_multiple_choice = null;
simple_list_item_multiple_choice.setAdapter(contactListAdapter);

return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onStart() {
super.onStart();

/** Setting the multiselect choice mode for the listview */
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}


and below is my list adapter`public class ContactListAdapter extends BaseAdapter {



Context context;
ArrayList<ContactListItems> contactList;

public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {

this.context = context;
contactList = list;
}

@Override
public int getCount() {

return contactList.size();
}

@Override
public Object getItem(int position) {

return contactList.get(position);
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ContactListItems contactListItems = contactList.get(position);

if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_list_row, null);

}

TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getName());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
tvPhone.setText(contactListItems.getPhone());

return convertView;
}


}


i am able to successfully run using activity but not working with fragment


Aucun commentaire:

Enregistrer un commentaire