Android在mainthread上做了太多工作

时间:2016-09-27 10:36:36

标签: android multithreading android-asynctask

我有一个问题,我一直收到一个错误,即在mainthread上做了太多的工作。代码如下:

在我的主要活动中,调用助手类(UcitavanjeUpozadini)在后台加载数据。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //String link = getResources().getString(R.string.blogLink);
        //Uri uri = Uri.parse(link); // missing 'http://' will cause crashed
        //Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        //startActivity(intent);

        List<View> listaView = new ArrayList<View>();
        int[] idView = {R.layout.fragment_first_apartment, R.layout.fragment_second_apartment, R.layout.fragment_third_apartment, R.layout.fragment_fourth_apartment};
        for(int i = 0; i < idView.length; i++)
        {
            View view = getLayoutInflater().inflate(idView[i], null);
            listaView.add(view);
        }

        UcitavanjeUpozadini ucitavanje = new UcitavanjeUpozadini(this, listaView);
        ucitavanje.execute();

    }

我还夸大了我稍后要使用的片段来填充我得到的数据。

然后我的所有代码都在类的doInBackground部分完成。

欢迎任何帮助。

public class UcitavanjeUpozadini extends AsyncTask<Void, Void, Void> {

    private Context context;
    private List<OglasHelper> lista;
    private List<TypedArray> listaId;
    private List<View> views;
    private ProgressDialog progressDialog;

    public UcitavanjeUpozadini(Context context, List<View> views){
        this.context = context;
        this.lista = new ArrayList<OglasHelper>();
        this.listaId = new ArrayList<TypedArray>();
        this.views = views;
        this.progressDialog = null;
    }

    @Override
    protected void onPreExecute()
    {
                /*
                 * This is executed on UI thread before doInBackground(). It is
                 * the perfect place to show the progress dialog.
                 */
        progressDialog = ProgressDialog.show(context, "",
                "Loading...");
    }

    @Override
    protected Void doInBackground(Void... params) {

        UcitavanjeHelperKlasa ucitavanjeHelperKlasa = new UcitavanjeHelperKlasa();
        String jsonString = ucitavanjeHelperKlasa.vratiJsonFajl(context, R.raw.source);
        lista = ucitavanjeHelperKlasa.vratiSveOglase(jsonString);

        int[] idEviArraya = {R.array.prviOglas, R.array.drugiOglas, R.array.treciOglas, R.array.cetvrtiOglas};
        for (int i = 0; i < idEviArraya.length; i++)
        {
            TypedArray oglas = context.getResources().obtainTypedArray(idEviArraya[i]);
            listaId.add(oglas);

        }



        napuniOglasePodacima();

        return null;
    }

    @Override
    protected void onPostExecute() {
        progressDialog.dismiss();
    }

    public void napuniOglasePodacima(){
        for(int i = 0; i < lista.size(); i++)
        {
            View view = (View) views.get(i);
            TypedArray array = listaId.get(i);
            Log.v("broj podataka u array-u", String.valueOf(array.getIndexCount()));
            if(array.getIndexCount() == 7)
            {
                fillApartment(array, lista.get(i), view);
            }
            else if(array.getIndexCount() == 9)
            {
                fillApartment(array, lista.get(i), view);
                TextView description = (TextView) view.findViewById(array.getIndex(7));
                String sredjenOpis = lista.get(i).getDescription().substring(0, array.getIndex(8)) + "...";
                description.setText(sredjenOpis);
            }
        }
    }

    public void fillApartment(TypedArray array, OglasHelper oglasHelper, View view)
    {
        ImageView borderTop = (ImageView) view.findViewById(array.getIndex(0));
        ImageView dugme = (ImageView) view.findViewById(array.getIndex(1));
        TextView rentBuy = (TextView) view.findViewById(array.getIndex(2));
        TextView currency = (TextView) view.findViewById(array.getIndex(3));
        TextView price = (TextView) view.findViewById(array.getIndex(4));
        TextView name = (TextView) view.findViewById(array.getIndex(5));
        TextView address = (TextView) view.findViewById(array.getIndex(6));
        int broj = array.getIndex(0);
        String string = String.valueOf(broj);
        Log.v("Ovo je testiranje", string);

        borderTop.setBackgroundColor(Color.parseColor(oglasHelper.getColor()));
        String rentBuyString = oglasHelper.getRentBuy();

        if(rentBuyString.equals("RENT"))
        {
            dugme.setImageResource(R.drawable.dugme_rent);
        }
        else if(rentBuyString.equals("BUY"))
        {
            dugme.setImageResource(R.drawable.dugme_buy);
        }

        rentBuy.setText(rentBuyString);
        rentBuy.setTextColor(Color.parseColor(oglasHelper.getColor()));

        currency.setText(oglasHelper.getCurrency());
        String sredjenaCena = NumberFormat.getNumberInstance(Locale.GERMANY).format(oglasHelper.getPrice());
        price.setText(sredjenaCena);
        name.setText(oglasHelper.getName());

        String[] poljaAdrese = {oglasHelper.getStreet() ,oglasHelper.getCity(), oglasHelper.getNeighbourhood(), oglasHelper.getZipcode()};
        String sredjenaAdresa = "";
        for(int i = 0; i < poljaAdrese.length; i++)
        {
            if(i == 3)
            {
                sredjenaAdresa = sredjenaAdresa.substring(0, -1) + ", " + poljaAdrese[i];
            }
            else if(!poljaAdrese[i].equals(""))
            {
                sredjenaAdresa += poljaAdrese[i] + " ";
            }
        }
        address.setText(sredjenaAdresa);
    }

}

0 个答案:

没有答案