I am trying to migrate from Regular listview
that deals with JSON response
to listview
with Fragments using JSON
with sherlock library
What i have done::
- I have imported the sherlock library to my project successfully
- I am converting one tab by tab
RatingDescriptionActivity.java [I updated based on answer]
import android.support.v4.app.Fragment;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.app.ProgressDialog;
public class RatingDescriptionActivity extends SherlockFragment {
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String NAME = "rank";
static String TYPE = "country";
static String DISTANCE = "distance";
static String RATING = "rating";
static String FLAG = "flag";
static String PRICE= "price";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_main, container, false);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
return rootView;
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
//mProgressDialog.setTitle("Fetching the information");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL("http://54.218.73.244:7005/DescriptionSortedRating/");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("restaurants");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put(RatingDescriptionActivity.NAME, jsonobject.getString("restaurantNAME"));
map.put(RatingDescriptionActivity.TYPE, jsonobject.getString("restaurantTYPE"));
map.put(RatingDescriptionActivity.FLAG, "http://54.218.73.244:7005/"+jsonobject.getString("restaurantIMAGE"));
map.put(RatingDescriptionActivity.DISTANCE, jsonobject.getString("restaurantDISTANCE"));
map.put(RatingDescriptionActivity.RATING, jsonobject.getString("restaurantRATING"));
map.put(RatingDescriptionActivity.PRICE, jsonobject.getString("restaurantPrice"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(RatingDescriptionActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
FragmentTabMainActivityForRestaurantDescription.java [Update based on answer]
import android.support.v4.app.Fragment;
import android.app.ActionBar;
import android.os.Bundle;
import android.app.Activity;
public class FragmentTabMainActivityForRestaurantDescription extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new RatingDescriptionActivity();
Fragment fragmentTab2 = new PriceDescriptionActivity();
Fragment fragmentTab3 = new DistanceDiscriptionActivity();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set Tab Icon and Titles
Tab1 = actionBar.newTab().setText("Tab1");
Tab2 = actionBar.newTab().setText("Tab2");
Tab3 = actionBar.newTab().setText("Tab3");
// Set Tab Listeners
Tab1.setTabListener(new TabListener(fragmentTab1));
Tab2.setTabListener(new TabListener(fragmentTab2));
Tab3.setTabListener(new TabListener(fragmentTab3));
// Add tabs to actionbar
actionBar.addTab(Tab1);
actionBar.addTab(Tab2);
actionBar.addTab(Tab3);
}
}
TabListener.java
import com.actionbarsherlock.app.SherlockFragment;
import android.support.v4.app.Fragment;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.app.ActionBar;
public class TabListener implements ActionBar.TabListener {
Fragment fragment;
public TabListener(Fragment fragmentTab1) {
// TODO Auto-generated constructor stub
this.fragment = fragmentTab1;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Errors i am facing ::
In FragmentTabMainActivityForRestaurantDescription.java
in the line
Fragment fragmentTab1 = new RatingDescriptionActivity();
I get:: Type mismatch: cannot convert from RatingDescriptionActivity to Fragment
ALSO
In RatingDescriptionActivity.java in the line
mProgressDialog = new ProgressDialog(RatingDescriptionActivity.this);
I get:: The constructor ProgressDialog(RatingDescriptionActivity) is undefined
And what other changes should i need to make ?
Any Ideas ?