如何在片段类中使用加载器,其中数据来自json?

时间:2013-11-12 05:41:37

标签: android

     public class MyFragment extends Fragment {
int mCurrentPage;
Context c;
GridView mListView;
String id, cat;
String strUrl;
TextView tvtitle;
TextView tv_id, tv_rating, tv_url;
public static String img_url, img_rating, img_id, img_name;
private ImageLoader imageLoader;
private DisplayImageOptions options;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle data = getArguments();
    mCurrentPage = data.getInt("current_page", 0);
    imageLoader=ImageLoader.getInstance();

    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.empty_photo)
    .showImageForEmptyUri(R.drawable.empty_photo)
    .showImageOnFail(R.drawable.empty_photo)
    .cacheInMemory(true)
    .cacheOnDisc(true)
    .displayer(new RoundedBitmapDisplayer(20))
    .build();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = null;
    DownloadTask downloadTask = new DownloadTask();
    v = inflater.inflate(R.layout.starters, container, false);
    tv_id = (TextView) v.findViewById(R.id.tv_starter_hide_id);
    tv_rating = (TextView) v.findViewById(R.id.tv_starter_hide_ratinf);
    tv_url = (TextView) v.findViewById(R.id.tv_starter_hide_url);
    cat = Category.Main_Cat;
    Log.i("Logcat Cat1", cat);
    switch (mCurrentPage) {
    case 1:
        Log.v("MyFragment Heap", "Max Mem in MB:"
                + (Runtime.getRuntime().maxMemory() / 1024 / 1024));
        // strUrl =
        // "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
        strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
        downloadTask.execute(strUrl);
        mListView = (GridView) v.findViewById(R.id.lv_countries);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getActivity().getBaseContext(),
                        Starter_info.class);
                img_id = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_id)).getText()
                        .toString();
                img_rating = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_ratinf))
                        .getText().toString();
                img_url = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_url)).getText()
                        .toString();
                img_name = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_imagename))
                        .getText().toString();
                startActivity(i);
            }
        });
        break;
    case 2:
        // strUrl =
        // "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
        strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
        downloadTask.execute(strUrl);
        Log.v("Splash Heap", "Max Mem in MB:"
                + (Runtime.getRuntime().maxMemory() / 1024 / 1024));
        mListView = (GridView) v.findViewById(R.id.lv_countries);
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getActivity().getBaseContext(),
                        Starter_info.class);
                img_id = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_id)).getText()
                        .toString();
                img_rating = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_ratinf))
                        .getText().toString();
                img_url = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_url)).getText()
                        .toString();
                img_name = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_imagename))
                        .getText().toString();
                startActivity(i);
            }
        });
        break;
    default:
        Log.i("Cat IS", Category.Main_Cat);
        // strUrl =
        // "http://vaibhavtech.com/work/android/get_json.php?cat="+cat+"&subcat=1";
        strUrl = " http://vaibhavtech.com/work/android/movie_list.php?category=BollyWood%20&sub_category=top";
        downloadTask.execute(strUrl);
        Log.v("Splash Heap", "Max Mem in MB:"
                + (Runtime.getRuntime().maxMemory() / 1024 / 1024));
        mListView = (GridView) v.findViewById(R.id.lv_countries);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Intent i = new Intent(getActivity().getBaseContext(),
                        Starter_info.class);
                img_id = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_id)).getText()
                        .toString();
                img_rating = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_ratinf))
                        .getText().toString();
                img_url = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_url)).getText()
                        .toString();
                img_name = ((TextView) arg1
                        .findViewById(R.id.tv_starter_hide_imagename))
                        .getText().toString();
                startActivity(i);

            }
        });
        break;
    }
    return v;
}

private String downloadUrl(String strUrl) throws IOException {
    String data = "";
    InputStream iStream = null;
    try {
        URL url = new URL(strUrl);
        // Creating an http connection to communicate with url
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        // Connecting to url
        urlConnection.connect();
        // Reading data from url
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(
                iStream));
        StringBuffer sb = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        data = sb.toString();
        br.close();

    } catch (Exception e) {
        Log.d("Exception while downloading url", e.toString());
    } finally {
        iStream.close();
    }

    return data;
}

private class DownloadTask extends AsyncTask<String, Integer, String> {
    String data = null;

    @Override
    protected String doInBackground(String... url) {
        try {
            data = downloadUrl(url[0]);

        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
        listViewLoaderTask.execute(result);
    }

}

private class ListViewLoaderTask extends
        AsyncTask<String, Void, List<HashMap<String, Object>>> {
    JSONObject jObject;
    @Override
    protected List<HashMap<String, Object>> doInBackground(String... strJson) {
        try {
            jObject = new JSONObject(strJson[0]);
            StarterParser countryJsonParser = new StarterParser();
            countryJsonParser.parse(jObject);
        } catch (Exception e) {
            Log.d("JSON Exception1", e.toString());
        }
        StarterParser countryJsonParser = new StarterParser();
        List<HashMap<String, Object>> countries=null;
        try {
            // Getting the parsed data as a List construct
            countries = countryJsonParser.parse(jObject);
        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }
        return countries;
    }

    @Override
    protected void onPostExecute(List<HashMap<String, Object>> countries) {
        mListView.setAdapter(new WallPaperAdapter(countries));
    }
}
public class WallPaperAdapter extends BaseAdapter {

    private LayoutInflater mInflater = null;
    private List<HashMap<String, Object>> countries = null;
    public WallPaperAdapter(List<HashMap<String, Object>> countries) {
        mInflater = LayoutInflater.from(getActivity());
        this.countries=countries;
    }

    public int getCount() {
        return countries.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.lv_layout,
                    null);
            holder = new ViewHolder();

            holder.txtId = (TextView) convertView
                    .findViewById(R.id.tv_starter_hide_id);
            holder.txtDuration = (TextView) convertView
                    .findViewById(R.id.tv_starter_hide_ratinf);
            holder.txtTitle= (TextView) convertView
                    .findViewById(R.id.tv_starter_hide_imagename);
            holder.txtYear = (TextView) convertView
                    .findViewById(R.id.tv_starter_hide_url);
            holder.imgPoster= (ImageView) convertView
                    .findViewById(R.id.iv_flag);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            holder.txtId.setText(countries.get(position)
                            .get("id").toString());
            holder.txtDuration.setText(countries.get(position)
                    .get("duration").toString());
            holder.txtTitle.setText(countries.get(position)
                    .get("title").toString());
            holder.txtYear.setText(countries.get(position)
                    .get("year").toString());
            Log.v("Image Loader URL",countries.get(position)
                    .get("flag_path").toString());
            imageLoader.displayImage(countries.get(position)
                            .get("flag_path").toString(),holder.imgPoster,options);

        } catch (Exception e) {
        }
        return convertView;
    }

    class ViewHolder {

        public TextView txtId;
        public TextView txtDuration;
        public TextView txtTitle;
        public TextView txtYear;
        public ImageView imgPoster;

    }
}

  }

我正在使用gridview,其中我想要一个显示滑动过程的加载程序,直到我的gridview.my图像中的图像加载来自json ...,我为此做了什么...,请求我的任何建议此,三江源。

1 个答案:

答案 0 :(得分:0)

在AsyncTask中添加此代码:

    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        ProgressDialog pDialog = new ProgressDialog(your_activity.this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

对于后期执行:

protected void onPostExecute(Void unused) {
            // closing progress dialog
            pDialog.dismiss();
        }

这可能对你有帮助....