Android gridview image应用程序可能在其主线程上做了太多工作

时间:2017-01-05 08:58:24

标签: java android gridview

我正在使用警告对话框显示文件中的图像。但当我按下按钮显示警告对话框时,应用程序冻结了6秒钟,然后警报对话框将会出现...... 我的代码......

listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
            //listview click event handling
            TextView id = (TextView) arg1.findViewById(R.id.textView17);
            final int id_To_Search = Integer.valueOf(id.getText().toString());
            Cursor item=mydb.singlecons(id_To_Search);
            Cursor att=mydb.attrs(id_To_Search);
            Cursor picloc=mydb.singleconspic(id_To_Search);
            att.moveToFirst();
            List<String> list = new ArrayList<>();
            //  Log.d("temp",att.getColumnName(1));

            while (!att.isAfterLast())
            {
                int l=att.getColumnCount();
                Log.d("length", String.valueOf(l));
                for(int i=2;i<l;i++){
                    Log.d("for","for");
                    if(att.getString(i)!=null){
                        String b= att.getColumnName(i)+" "+att.getString(i);
                        list.add(b);
                        Log.d("att",b);
                    }
                }
                Log.d("while","while");
                att.moveToNext();
            }
            att.close();
            Log.d("list", String.valueOf(list));
            picloc.moveToFirst();

            FilePathStrings = new String[picloc.getCount()];
            int i=0;
            while (!picloc.isAfterLast()){
                Log.d("picloc",picloc.getString(2));
                FilePathStrings[i]=picloc.getString(2);
                i++;
                picloc.moveToNext();
            }

            item.moveToFirst();

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
            dialogBuilder.setView(dialogView);

            TextView con=(TextView)dialogView.findViewById(R.id.textView29);
            con.setText("Consultation on "+item.getString(4));
            TextView des=(TextView)dialogView.findViewById(R.id.textView28);
            des.setText(item.getString(2));
            TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
            pre.setText(item.getString(3));
            TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
            fee.setText(item.getString(5));
            adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
            grid = (GridView) dialogView.findViewById(R.id.gridview);
            grid.setAdapter(adapter1);
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file://" + FilePathStrings[position]), "image/*");
                    startActivity(intent);
                }

            });
            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();

Gridviewadapter.java

class GridViewAdapter extends BaseAdapter {

private String[] filepath;
private static LayoutInflater inflater = null;
GridViewAdapter(String[] fpath,Context context) {
    filepath = fpath;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
public int getCount() {
    return filepath.length;

}

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

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

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
        ImageView image = (ImageView) vi.findViewById(R.id.image);
        Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);

   Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filepath[position]),320,240);

    image.setImageBitmap(thumbBitmap);

        return vi;
}

}

我该如何解决这个问题。我知道asyctask会有所帮助......我该如何解决这个问题

2 个答案:

答案 0 :(得分:0)

使用处理程序,

default

答案 1 :(得分:-1)

我认为,为了获得最佳效果,请在另一项活动中执行此操作

无论如何,如果没有,请使用AsyncTask类,例如:

listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
            //listview click event handling
            TextView id = (TextView) arg1.findViewById(R.id.textView17);
            final int id_To_Search = Integer.valueOf(id.getText().toString());
            new displaysImages().execute(id_To_Search);
            }});
....
class displaysImages extends AsyncTask<Integer, Void, Void> {
ProgressDialog   dialog;
Cursor item=null; 
String FilePathStrings[];
 @Override
    protected void onPreExecute() {
        super.onPreExecute();
    /* show ProgressDialog before doing anything*/
       dialog = ProgressDialog.show(getApplicationContext(), "Veuillez patienter", "Chargement des données....", true);
    }

    @Override
    protected Void doInBackground(Integer... param) {
        try {
        item=mydb.singlecons(param[0]);
            Cursor att=mydb.attrs(param[0]);
            Cursor picloc=mydb.singleconspic(param[0]);
            att.moveToFirst();
            List<String> list = new ArrayList<>();
            //  Log.d("temp",att.getColumnName(1));

            while (!att.isAfterLast())
            {
                int l=att.getColumnCount();
                Log.d("length", String.valueOf(l));
                for(int i=2;i<l;i++){
                    Log.d("for","for");
                    if(att.getString(i)!=null){
                        String b= att.getColumnName(i)+" "+att.getString(i);
                        list.add(b);
                        Log.d("att",b);
                    }
                }
                Log.d("while","while");
                att.moveToNext();
            }
            att.close();
            Log.d("list", String.valueOf(list));
            picloc.moveToFirst();

            FilePathStrings = new String[picloc.getCount()];
            int i=0;
            while (!picloc.isAfterLast()){
                Log.d("picloc",picloc.getString(2));
                FilePathStrings[i]=picloc.getString(2);
                i++;
                picloc.moveToNext();
            }

            item.moveToFirst();

       } catch (Exception e) {
            e.printStackTrace();
            dialog.dismiss();
        }
        return null;
    }
     @Override
    protected void onPostExecute(Void res) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
            LayoutInflater inflater = getLayoutInflater();
            View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
            dialogBuilder.setView(dialogView);

            TextView con=(TextView)dialogView.findViewById(R.id.textView29);
            con.setText("Consultation on "+item.getString(4));
            TextView des=(TextView)dialogView.findViewById(R.id.textView28);
            des.setText(item.getString(2));
            TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
            pre.setText(item.getString(3));
            TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
            fee.setText(item.getString(5));
            adapter1 = new GridViewAdapter(FilePathStrings,getApplicationContext());
            grid = (GridView) dialogView.findViewById(R.id.gridview);
            grid.setAdapter(adapter1);
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse("file://" + FilePathStrings[position]), "image/*");
                    startActivity(intent);
                }
            });
            AlertDialog alertDialog = dialogBuilder.create();
dialog.dismiss();
     alertDialog.show();   
}}

我没有测试代码,但我认为它的工作原理