【问题标题】:Error AsyncTask Progress Bar not display错误 AsyncTask 进度条不显示
【发布时间】:2015-01-02 05:09:38
【问题描述】:

你好朋友们,我一直在杀人以实现呃终止这段代码尝试了一切,但没有任何结果..

我认为我在这部分有错误

ImageAdapter ImageAdapter  = new ImageAdapter();
lstView1.setAdapter (ImageAdapter);

应该很好但是没能达到诶!

lstView1.setAdapter (new ImageAdapter (this, MyArrList));

感谢所有帮助,我正在尝试寻找名称的应用程序,并在其他活动中传递参数并在 mysql 数据库中显示 listview alamacenados 文本和图像。使用 json

 public class MainActivity extends Activity {

    private ProgressDialog pDialog;

     public String searchkey;



      // Creating JSON Parser object
        JSONParser jParser = new JSONParser();


    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));



        Intent myIntent = getIntent(); 


        searchkey = myIntent.getStringExtra("texto");

      final  String url = "http://10.0.2.2/test/getJSON.php?keyword=" +  searchkey ;       

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 11) {
            StrictMode.ThreadPolicy policy = new                        StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);


}





        // listView1
        final ListView lstView1 = (ListView)findViewById(R.id.listView1); 






        class pbar extends AsyncTask<String, String, String> {


            public  String getJSONUrl (String url) {
                StringBuilder str = new StringBuilder();
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);





                try {
                    HttpResponse response = client.execute(httpGet);
                    StatusLine statusLine = response.getStatusLine();
                    int statusCode = statusLine.getStatusCode();




                    if (statusCode == 200) { // Download OK
                        HttpEntity entity = response.getEntity();
                        InputStream content = entity.getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(content));


                        String line;




                        while ((line = reader.readLine()) != null) {

                            str.append(line);
                        }
                    } else {
                        Log.e("Log", "Failed to download file..");
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str.toString();
            }










            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Cargando Productos.Por Favor Espere.");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            /**
             * getting Idioms from url
             * */
            protected String doInBackground(String... args) {









                try {


                    JSONArray data = new JSONArray(getJSONUrl(url));

                    final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
                    HashMap<String, String> map;

                    for(int i = 0; i < data.length(); i++){
                        JSONObject c = data.getJSONObject(i);
                        map = new HashMap<String, String>();
                        map.put("ImageID", c.getString("ImageID")); 
                        map.put("ImageDesc", c.getString("ImageDesc"));
                        map.put("ImagePath", c.getString("ImagePath"));
                        MyArrList.add(map);

                    //  hidePDialog();



                    }


                    ImageAdapter ImageAdapter = new ImageAdapter();
                    lstView1.setAdapter(ImageAdapter);





                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                //  hidePDialog();
                    e.printStackTrace();
                }



                   new pbar().execute();
                return null;

            }
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting the related idioms
                pDialog.dismiss();
            }

        }


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ImageAdapter.java

 public class ImageAdapter extends BaseAdapter {


    private Context context;
    private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();

 // constructor
    public ImageAdapter() {

    }


    public ImageAdapter(Context c, ArrayList<HashMap<String, String>> list)  {
        // TODO Auto-generated method stub
        context = c;
        MyArr = list;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return MyArr.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_column, null); 
        }

        // ColImage
        ImageView imageView = (ImageView) convertView.findViewById(R.id.ColImgPath);
        imageView.getLayoutParams().height = 100;
        imageView.getLayoutParams().width = 100;
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         try
         {
             imageView.setImageBitmap(loadBitmap(MyArr.get(position).get("ImagePath")));
         } catch (Exception e) {
             // When Error
             imageView.setImageResource(android.R.drawable.ic_menu_report_image);
         }

        // ColPosition
        TextView txtPosition = (TextView) convertView.findViewById(R.id.ColImgID);
        txtPosition.setPadding(10, 0, 0, 0);
        txtPosition.setText("ID : " + MyArr.get(position).get("ImageID"));

        // ColPicname
        TextView txtPicName = (TextView) convertView.findViewById(R.id.ColImgDesc);
        txtPicName.setPadding(50, 0, 0, 0);
        txtPicName.setText("Desc : " + MyArr.get(position).get("ImageDesc"));

        //hidePDialog();

        return convertView;

    }



    private static final String TAG = "ERROR";
    private static final int IO_BUFFER_SIZE = 4 * 1024;
    public static Bitmap loadBitmap(String url) {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
            copy(in, out);
            out.flush();

            final byte[] data = dataStream.toByteArray();
            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inSampleSize = 1;

            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
        } catch (IOException e) {
            Log.e(TAG, "Could not load Bitmap from: " + url);
        } finally {
            closeStream(in);
            closeStream(out);
        }

        return bitmap;
    }



     private static void closeStream(Closeable stream) {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    android.util.Log.e(TAG, "Could not close stream", e);
                }
            }
        }

     private static void copy(InputStream in, OutputStream out) throws IOException {
     byte[] b = new byte[IO_BUFFER_SIZE];
     int read;
     while ((read = in.read(b)) != -1) {
         out.write(b, 0, read);
     }

 }
}

【问题讨论】:

  • 您的应用程序崩溃了吗?你到底得到了什么错误。发布日志。将下面代码的位置移动到 onPostExecute method.ImageAdapter ImageAdapter = new ImageAdapter(); lstView1.setAdapter(ImageAdapter);
  • 这里是应用程序 .. 我需要帮助 嗯嗯花了几个小时但无法解决代码... chrome://mega/content/secure.html#!phgG0IQK!cq93q7os4IpCpa4dbIFTwn1tFxjufvXYplg0QiFRiFY 我需要实现进度条thx...
  • Naava 链接坏了

标签: java android mysql android-asynctask


【解决方案1】:

我无法对其进行测试,所以我会指出一个错误,你肯定会遇到: 换行:

ImageAdapter ImageAdapter = new ImageAdapter();
lstView1.setAdapter(ImageAdapter);

例如那些:

ImageAdapter imageAdapter = new ImageAdapter();
lstView1.setAdapter(imageAdapter);

编辑:

然后试试这个:

ImageAdapter imageAdapter = new ImageAdapter(MainActivity.this, MyArrList);
lstView1.setAdapter(imageAdapter);

【讨论】:

  • gracias amigo lo provare!!你来吧
  • 当然,朋友,但是调用 MyArrList .. 的一些例子应该是 lstView1.setAdapter (new ImageAdapter (this, MyArrList));当我添加新的阴影 ImageAdapter 时,我将其标记为 bug 知道为什么吗?
  • 一些应该如何的例子。请
  • 你必须让我知道它给你带来了什么样的错误
  • la manera correcta es esta lstView1.setAdapter (new ImageAdapter (this, MyArrList)); pero al escribirlo asi me marca error (new ImageAdapter (this, MyArrList)); me dice esto:构造函数图像适配器(pbar arraylist>)未定义
猜你喜欢
  • 1970-01-01
  • 2013-09-16
  • 2016-06-11
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
相关资源
最近更新 更多