1

조각 코드는응용 프로그램은에서 Andriod에 자사의 메인 스레드 (중포 기지 데이터베이스)에 너무 많은 일을 할 수있다

ref.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 

       list = new ArrayList<>(); 
       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 

        for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) { 

         try { 

          Model listdata = new Model(); 

          Model for_post_details = dataSnapshot2.child("info").getValue(Model.class); 

          //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show(); 

          String path = for_post_details.getPath(); 
          String location = for_post_details.getLocation(); 
          String caption = for_post_details.getCaption(); 
          String username = "" + dataSnapshot1.child("username").getValue(); 

          listdata.setPath(path); 
          listdata.setLocation(location); 
          listdata.setCaption(caption); 
          listdata.setUsername(username); 

          list.add(listdata); 

         } catch (Exception ignored) { 
         } 
        } 

       } 
       Adapter recyclerview = new Adapter(list, getActivity()); 
       RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity()); 
       recycler.setLayoutManager(layoutmanager); 
       recycler.setItemAnimator(new DefaultItemAnimator()); 
       recycler.setAdapter(recyclerview); 

      } 

      @Override 
      public void onCancelled(DatabaseError error) { 
      } 
     }); 

어댑터 코드는

public class Adapter extends RecyclerView.Adapter<Adapter.MyHoder> { 

    List<Model> list; 
    Context context; 

    public Adapter(List<Model> list, Context context) { 
     this.list = list; 
     this.context = context; 
    } 

    @Override 
    public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(context).inflate(R.layout.recycler_trending,parent,false); 
     MyHoder myHoder = new MyHoder(view); 
     return myHoder; 
    } 

    @Override 
    public void onBindViewHolder(MyHoder holder, int position) { 
     Model mylist = list.get(position); 
     holder.caption.setText(mylist.getCaption()); 
     holder.location.setText(mylist.getLocation()); 
     holder.username_top.setText(mylist.getUsername()); 
     holder.username_bottom.setText(mylist.getUsername()); 
     Glide.with(context) 
       .load(mylist.getPath()) 
       .into(holder.image); 
    } 

    @Override 
    public int getItemCount() { 
     return list.size(); 
    } 

    class MyHoder extends RecyclerView.ViewHolder{ 
     TextView total_likes, caption, location, username_top, username_bottom; 
     ImageView image; 
     ImageButton like, share; 

     public MyHoder(View itemView) { 
      super(itemView); 
      total_likes = (TextView) itemView.findViewById(R.id.total_likes); 
      caption = (TextView) itemView.findViewById(R.id.caption); 
      username_top = (TextView) itemView.findViewById(R.id.username_top); 
      username_bottom = (TextView) itemView.findViewById(R.id.username_bottom); 
      location = (TextView) itemView.findViewById(R.id.location); 
      image = (ImageView) itemView.findViewById(R.id.image); 
      like = (ImageButton) itemView.findViewById(R.id.like); 
      share = (ImageButton) itemView.findViewById(R.id.share); 

     } 
    } 

이 코드 Instagram과 같은 앱을 만드는 데 사용하고 있습니다.

코드는 정상적으로 실행되지만 UI가 느리며 스크롤 속도가 느려지고 응용 프로그램이 중지됩니다.

이 메시지가 나타납니다.

I/Choreographer (1378) : 65 프레임 건너 뛰기! 응용 프로그램은 일 수 있습니다 주 스레드에서 너무 많은 일을.

내가 뭘 잘못하고있어 ??

Instagram이 어떤 게시물을 표시하기 위해 어떤 기술을 사용하고 있는지 알고 계십니까? ?

감사

적 데이터로 UI를 만들어 그 변경 지속적으로
+0

당신이 –

+0

@AswinPAshok 이미지 무거운 이미지를로드 할 수 호출하는 또 다른 방법은 AsyncTask

사본을 사용하는 것입니다 단지 15kb-17kb 크기. –

+0

버튼 클릭시 데이터를 가져 오는 중입니까? –

답변

1

은 조각에 onViewCreated() 안에이 복사, runOnUiThread() 내부에 위의 방법을 실행

runOnUiThread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      ref.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 

        list = new ArrayList<>(); 
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 

         for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) { 

          try { 

           Model listdata = new Model(); 

           Model for_post_details = dataSnapshot2.child("info").getValue(Model.class); 

           //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show(); 

           String path = for_post_details.getPath(); 
           String location = for_post_details.getLocation(); 
           String caption = for_post_details.getCaption(); 
           String username = "" + dataSnapshot1.child("username").getValue(); 

           listdata.setPath(path); 
           listdata.setLocation(location); 
           listdata.setCaption(caption); 
           listdata.setUsername(username); 

           list.add(listdata); 

          } catch (Exception ignored) { 
          } 
         } 

        } 
        Adapter recyclerview = new Adapter(list, getActivity()); 
        RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity()); 
        recycler.setLayoutManager(layoutmanager); 
        recycler.setAdapter(recyclerview); 

       } 

       @Override 
       public void onCancelled(DatabaseError error) { 
       } 
      }); 
     } 
    }); 
,

UPDATE 이 코드는 외부 onViewCreated()

private class LoadData extends AsyncTask<String, String, String> 
{ 
    final ProgressDialog pDialog = new ProgressDialog(getActivity()); 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     pDialog.setMessage("Loading please wait.."); 
     pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     pDialog.setIndeterminate(true); 

     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... strings) 
    { 
     ref.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 

       list = new ArrayList<>(); 
       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 

        for (DataSnapshot dataSnapshot2 : dataSnapshot1.child("Posts").getChildren()) { 

         try { 

          Model listdata = new Model(); 

          Model for_post_details = dataSnapshot2.child("info").getValue(Model.class); 

          //Toast.makeText(getActivity(), ""+dataSnapshot1.child("username").getValue(), Toast.LENGTH_SHORT).show(); 

          String path = for_post_details.getPath(); 
          String location = for_post_details.getLocation(); 
          String caption = for_post_details.getCaption(); 
          String username = "" + dataSnapshot1.child("username").getValue(); 

          listdata.setPath(path); 
          listdata.setLocation(location); 
          listdata.setCaption(caption); 
          listdata.setUsername(username); 

          list.add(listdata); 

         } catch (Exception ignored) { 
         } 
        } 

       } 

      } 

      @Override 
      public void onCancelled(DatabaseError error) { 
      Log.d("anyError", error.toString()); 
      } 
     }); 

    } 

    @Override 
    protected void onPostExecute(String lengthOfFile) 
    { 

       Adapter recyclerview = new Adapter(list, getActivity()); 
       RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getActivity()); 
       recycler.setLayoutManager(layoutmanager); 
       recycler.setAdapter(recyclerview); 

     if ((pDialog != null) && (pDialog.isShowing())) 
     { 
      pDialog.dismiss(); 
     } 
    } 
} 

onViewCreated() 내부 그냥 new LoadData().execute();

+0

그것은 효과가 있었지만 조금 아직도 얼었다. –

+0

기다려라 내 대답을 업데이 트하십시오. –

+0

두 번째 방법을 시도하십시오. 작동하는 경우 대답을 수락 :) –

0

addValueEventListener 호출 붙어 타이머 작업을 한 번만 데이터를 읽고 추가 할 주기적으로 응용 프로그램의 데이터를 업데이트 할 addListenerForSingleValueEvent를 사용하려고 만 것이다 타이머가 종료하고 UI를 부착하지 않을 경우 업데이트 데이터 가끔

+0

"addListenerForSingleValueEvent"를 사용해 보았지만 아무 것도 변경되지 않았습니다. 같은 결과. –

+0

다른 스레드 작업을 동시에 사용하고 있지 않은지 확인하십시오. –

+0

@jubinpatel 스레딩에 대해서는 아무 것도 모른다. 나를 안내 해줘. –

관련 문제