2

tuneup https://play.google.com/store/apps/details?id=tuneup.target.myradioapp&hl=en出现在 google play 但ProgressDialog不添加我只添加toast图像添加现在请帮助我如何运行dialog在使用数据加载列表视图之前,我已经看到如何在具有列表视图的活动中显示进度条(圆圈)

public class Tab_Like extends Activity {

        ListView lv;
        TextView t1, t2;
        ImageView iv;
    public static String name, rating, like, radio_url, id, listner, image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.category_name_radio);

        setProgressBarIndeterminateVisibility(true);
        setProgressBarIndeterminateVisibility(false);
        super.onCreate(savedInstanceState);
        /*
         * LayoutInflater li = getLayoutInflater(); View layout =
         * li.inflate(R.layout.customtoast, (ViewGroup)
         * findViewById(R.id.custom_toast_layout)); Toast toast = new
         * Toast(getApplicationContext()); toast.setDuration(Toast.LENGTH_LONG);
         * toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
         * toast.setView(layout); toast.show();
         */

        TextView tv = (TextView) findViewById(R.id.textView1);
        lv = (ListView) findViewById(R.id.list_view);
        t1 = (TextView) findViewById(R.id.tv_country);
        t2 = (TextView) findViewById(R.id.tv_country_details);
        iv = (ImageView) findViewById(R.id.iv_flag);

        // URL to the JSON data
        String strUrl = "http://vaibhavtech.com/work/android/json_radio.php?cat=top";

        // Creating a new non-ui thread task to download json data
        DownloadTask downloadTask = new DownloadTask();

        // Starting the download process
        downloadTask.execute(strUrl);

        // Getting a reference to ListView of activity_main

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                int songIndex = arg2;
                Intent intent = new Intent(Tab_Like.this, Play_Radio.class);
                // Toast.makeText(getBaseContext(),"Adapter"+""+arg0+" "+"View"+""+arg1+" "+
                // "Int"+""+arg2+" "+"Long"+""+arg3+" ",
                // Toast.LENGTH_LONG).show();
                intent.putExtra("songIndex", songIndex);
                setResult(100, intent);

                radio_url = ((TextView) arg1
                        .findViewById(R.id.tv_other_data_radio_url)).getText()
                        .toString();
                rating = ((TextView) arg1
                        .findViewById(R.id.tv_other_data_rating)).getText()
                        .toString();
                id = ((TextView) arg1.findViewById(R.id.tv_other_data_id))
                        .getText().toString();
                name = ((TextView) arg1.findViewById(R.id.tv_other_data_name))
                        .getText().toString();
                Tab_Listner.id = id;
                Tab_Listner.radio_url = radio_url;
                Tab_Listner.name = name;
                Tab_Listner.rating = rating;

                startActivity(intent);

            }

        });

    }

    /** A method to download json data from url */
    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;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String> {
        String data = null;
        LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaH);

        @Override
        protected void onPreExecute() {
            // SHOW THE SPINNER WHILE LOADING FEEDS
            linlaHeaderProgress.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @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) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);

            // HIDE THE SPINNER AFTER LOADING FEEDS
            linlaHeaderProgress.setVisibility(View.GONE);
            super.onPostExecute(result);

        }

    }

    /** AsyncTask to parse json data and load ListView */
    private class ListViewLoaderTask extends
            AsyncTask<String, Void, SimpleAdapter> {

        JSONObject jObject;

        // Doing the parsing of xml data in a non-ui thread
        @Override
        protected SimpleAdapter 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());
            }

            // Instantiating json parser class
            StarterParser countryJsonParser = new StarterParser();

            // A list object to store the parsed countries list
            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());
            }

            // Keys used in Hashmap

            String[] from = { "image", "id", "like", "rating", "listener",
                    "radio_url", "name" };

            // Ids of views in listview_layout
            int[] to = { R.id.iv_other_data_image, R.id.tv_other_data_id,
                    R.id.tv_other_data_like, R.id.tv_other_data_rating,
                    R.id.tv_other_data_listner, R.id.tv_other_data_radio_url,
                    R.id.tv_other_data_name };

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    countries, R.layout.lv_radio_detail, from, to);

            return adapter;
        }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            lv.setAdapter(adapter);

            /*
             * for(int i=0;i<adapter.getCount();i++){ HashMap<String, Object> hm
             * = (HashMap<String, Object>) adapter.getItem(i); String imgUrl =
             * (String) hm.get("flag_path"); ImageLoaderTask imageLoaderTask =
             * new ImageLoaderTask();
             * 
             * HashMap<String, Object> hmDownload = new HashMap<String,
             * Object>(); hm.put("flag_path",imgUrl); hm.put("position", i);
             * 
             * // Starting ImageLoaderTask to download and populate image in the
             * listview imageLoaderTask.execute(hm); }
             */
        }
    }

    /** AsyncTask to download and load an image in ListView */
    /*
     * private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>,
     * Void, HashMap<String, Object>>{
     * 
     * @Override protected HashMap<String, Object>
     * doInBackground(HashMap<String, Object>... hm) {
     * 
     * InputStream iStream=null; String imgUrl = (String)
     * hm[0].get("flag_path"); int position = (Integer) hm[0].get("position");
     * 
     * URL url; try { url = new URL(imgUrl);
     * 
     * // 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();
     * 
     * // Getting Caching directory // File cacheDirectory =
     * getBaseContext().getCacheDir(); File cacheDirectory =
     * getBaseContext().getExternalCacheDir(); // Temporary file to store the
     * downloaded image File tmpFile = new File(cacheDirectory.getPath() +
     * "/wpta_"+position+".png");
     * 
     * // The FileOutputStream to the temporary file FileOutputStream fOutStream
     * = new FileOutputStream(tmpFile);
     * 
     * // Creating a bitmap from the downloaded inputstream Bitmap b =
     * BitmapFactory.decodeStream(iStream);
     * 
     * // Writing the bitmap to the temporary file as png file
     * b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
     * 
     * // Flush the FileOutputStream fOutStream.flush();
     * 
     * //Close the FileOutputStream fOutStream.close();
     * 
     * // Create a hashmap object to store image path and its position in the
     * listview HashMap<String, Object> hmBitmap = new HashMap<String,
     * Object>();
     * 
     * // Storing the path to the temporary image file
     * hmBitmap.put("image",tmpFile.getPath());
     * 
     * // Storing the position of the image in the listview
     * hmBitmap.put("position",position);
     * 
     * // Returning the HashMap object containing the image path and position
     * return hmBitmap;
     * 
     * 
     * }catch (Exception e) { e.printStackTrace(); } return null; }
     * 
     * @Override protected void onPostExecute(HashMap<String, Object> result) {
     * // Getting the path to the downloaded image
     * 
     * String path = (String) result.get("image");
     * 
     * // Getting the position of the downloaded image int position = (Integer)
     * result.get("position");
     * 
     * // Getting adapter of the listview SimpleAdapter adapter = (SimpleAdapter
     * ) lv.getAdapter();
     * 
     * // Getting the hashmap object at the specified position of the listview
     * HashMap<String, Object> hm = (HashMap<String, Object>)
     * adapter.getItem(position);
     * 
     * // Overwriting the existing path in the adapter hm.put("image",path);
     * 
     * // Noticing listview about the dataset changes
     * adapter.notifyDataSetChanged(); } }
     */
}
4

1 回答 1

0

从该代码中尝试替换以下代码,

dialog=new ProgressDialog(Tab_Like.this, android.R.style.Theme_Translucent_NoTitleBar);
            dialog.setMessage("loading...");
            dialog.getWindow().setGravity(Gravity.CENTER);
            dialog.show();

用下面的代码替换上面的代码,

dialog = new Dialog(Tab_Like.this,
android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.layout_progress_loader);
dialog.show(); 

换成 private ProgressDialog dialog;_private Dialog dialog;

创建一个名为 的 xml 布局,然后layout_progress_loader.xml将以下 xml 代码添加到该文件中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/prgbar_loader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>
于 2013-10-25T11:33:36.453 回答