I am having issues converting my previously working FragmentActivity and Customlistadapter to a ListFragment to be used with my navigation drawer. Currently, the navigation drawer is working correctly.
Below is the modified code for GetContacts
public class GetContacts extends android.support.v4.app.ListFragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// json URL
private static final String url = "http://url.json.php";
private ProgressDialog pDialog;
private List<Contacts> contactList = new ArrayList<Contacts>();
private ListView listView;
private ContactListAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_contacts, container, false);
return rootview;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//See the second issue about wotking with ListFragment's list.
adapter = new ContactListAdapter(this, contactList);
setListAdapter(adapter);
// pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
// pDialog.setMessage("Loading...");
//pDialog.show();
// Creating volley request obj
JsonArrayRequest contactReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Contacts contacts = new Contacts();
contacts.setDivision(obj.getString("Division"));
contacts.setName(obj.getString("name"));
contacts.setNumber(obj.getString("number"));
// adding contacts to contacts array
contactList.add(contacts);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(contactReq);
//end volley code paste
}
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
Additionally, I have modified the code for ContactListAdapter. Currently I am getting the following compile error. "Error:(49, 56) error: cannot find symbol method getContext()". I checked this SO question and tried to understand what is causing this issue with no luck.
public class ContactListAdapter extends BaseAdapter {
private Activity activity;
private Fragment fragment;
private LayoutInflater inflater;
private List<Contacts> contactItems;
public ContactListAdapter(Fragment fragment, List<Contacts> contactItems) {
this.fragment = fragment;
this.contactItems = contactItems;
}
@Override
public int getCount() {
return contactItems.size();
}
@Override
public Object getItem(int location) {
return contactItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_rowcontacts, null);
}
TextView division = (TextView) convertView.findViewById(R.id.division);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView number = (TextView) convertView.findViewById(R.id.number);
// getting contact data for the row
Contacts m = contactItems.get(position);
// Division
division.setText(m.getDivision());
// Name
name.setText("Name: " + m.getName());
// number
number.setText("Number: " + m.getNumber());
return convertView;
}
}