0

我正在尝试从 处理Regular listview迁移JSON responselistview处理Fragments using JSONsherlock library

我做了什么::

  • 我已成功将 sherlock 库导入到我的项目中
  • 我正在逐个标签转换

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

    }
}

我面临的错误::

FragmentTabMainActivityForRestaurantDescription.java排队

Fragment fragmentTab1 = new RatingDescriptionActivity();

我得到::Type mismatch: cannot convert from RatingDescriptionActivity to Fragment

RatingDescriptionActivity.java行中

mProgressDialog = new ProgressDialog(RatingDescriptionActivity.this);

我得到::The constructor ProgressDialog(RatingDescriptionActivity) is undefined


我还需要做哪些其他更改?

有任何想法吗 ?

4

1 回答 1

3

替换这个

mProgressDialog = new ProgressDialog(RatingDescriptionActivity.this);

经过

mProgressDialog = new ProgressDialog(getActivity());

替换这个

import android.app.Fragment;  

经过

import android.support.v4.app.Fragment; 
// import from support library
于 2013-10-25T04:34:26.907 回答