在同一活动中具有不同适配器的两个列表视图不通知

时间:2015-04-16 04:37:38

标签: android listview adapter

我有两个关于同一活动的列表视图。我正在运行一个定期任务来通知一个列表视图的数据更改,我有一个按钮来重新设置第一个列表视图上的数据(设置适配器再次调用此处为第一个列表视图)。 当我点击按钮再次将适配器设置为第一个列表视图时,即使我恢复了活动,我的第二个适配器也会停止通知。

我使用简单的功能来做到这一点。

重复任务:
我打电话给:secondAdapter.notifyDataSetChanged();
点击按钮:
我致电:firstlist.setadapter(firstadapter);

以下是重复出现的任务:

ScheduledExecutorService scheduler =
                    Executors.newSingleThreadScheduledExecutor();

            scheduler.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {

                    boolean willRun=false;
                    double size=0;
                    for (int i = 0; i < SoapCostants.downloadedList.size(); i++) {
                        size=size+Double.parseDouble(String.valueOf(SoapCostants.downloadedList.get(i).getSize()));
                        if (!SoapCostants.downloadedList.get(i).getProgress().equalsIgnoreCase("no")) {
                            willRun=true;
                        }
                    }
                    if (!willRun){
                        double d=Double.parseDouble(AppActivity.readFromFile("downloadedLImitMb", "0.0"));
                       // Log.v("Key>>>>>>>>>>>>>>>double", d+"" );
                        if (size>d){
                            AppActivity.writeToFile(size+"", "downloadedLImitMb");
                         //   Log.v("Key>>>>>>>>>>>>>>>double2", d+"" );
                        }

                    }
                    else if (willRun) {
                        SoapCostants.willInvalidate=true;
                        for (int i = 0; i < SoapCostants.downloadedList.size(); i++) {

                            for (int j = 0; j < SoapCostants.downloadList.size(); j++) {
                                try {
                                    if (SoapCostants.downloadList.get(j)!=null) {
                                     //   Log.v("Key>>>>>>>>>>>>>>>", SoapCostants.downloadList.get(j).getKey()+"  "+SoapCostants.downloadedList.get(i).getName()+"" );
                                        if (SoapCostants.downloadList.get(j).getKey()!=null) {

             if (!SoapCostants.downloadedList.get(i).getProgress().equalsIgnoreCase("no")) {
    if (SoapCostants.downloadList.get(j).getKey().equalsIgnoreCase(SoapCostants.downloadedList.get(i).getName())) {
        Download d;

        d = SoapCostants.downloadList.get(j).getValue();
        SoapCostants.downloadList.get(j).getValue().getProgress().getPercentTransferred();
        float downloded = getFileSize(new File(Environment.getExternalStorageDirectory() + File.separator + "ToteFlix" + File.separator + SoapCostants.downloadList.get(j).getKey()));
        float total = d.getProgress().getTotalBytesToTransfer();
        float percentage = ((downloded / total) * 100);
        DraggedVideoEntity entityy = SoapCostants.downloadedList.get(i);
        if (getFileSize(new File(Environment.getExternalStorageDirectory() + File.separator + "ToteFlix" + File.separator + SoapCostants.downloadList.get(j).getKey())) < (int) d.getProgress().getTotalBytesToTransfer()) {
            if (!(entityy.getProgressLevel() > (int) percentage)) {
                if (readFromFile(entityy.getName() + "5", "no").equalsIgnoreCase("no")) {
                    if (entityy.getProgressLevel() > 5) {
                        // writeToFile(Float.parseFloat(AppActivity.readFromFile(entityy.getName()+"size", "0.0"))+(entityy.getSize()/20)+"", entityy.getName()+"size");
                        AppActivity.writeToFile(Float.parseFloat(AppActivity.readFromFile("downloadedLImitMb", "0.0")) + (entityy.getSize() / 20) + "", "downloadedLImitMb");
                        writeToFile("yes", entityy.getName() + "5");
                        // Log.v("pr>>>>>>>>>>>>>>>>>", AppActivity.readFromFile("downloadedLImitMb", "0.0")+"");
                    }

                }


                int perc = (int) percentage;
                SoapCostants.downloadedList.get(i).setProgressLevel(perc);
                SoapCostants.downloadedList.get(i).setIsdone(false);
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        List<DraggedVideoEntity> newList = new ArrayList<DraggedVideoEntity>();
                        newList.addAll(SoapCostants.downloadedList);
                        SoapCostants.downloadedList.clear();
                        SoapCostants.downloadedList.addAll(newList);
                        DashboardActivity2.adaptr.notifyDataSetChanged();
                        if (ifInvalidate) {
                            adaptr.notifyDataSetChanged();
//DashboardActivity2.travelReady.setAdapter(adaptr);
                            ((DraggedVideoAdapter2) travelReady.getAdapter()).notifyDataSetChanged();
                            //travelReady.setAdapter(adaptr);
                            Log.v("pr>>>>>>>>>>>>>>>>>", "Invalidating");
                            if (!Utils.isInternetAvailable(FullTravelReady.this.getApplicationContext())) {
                                AppActivity.showAlertDialog(FullTravelReady.this, "Please check your Internet connection!");
                            }
                        }
                    }
                });
            }

        } else {

                                                    /*writeToFile(Float.parseFloat(AppActivity.readFromFile(entityy.getName()+"size", "0.0"))+(entityy.getSize()/20)+"", entityy.getName()+"size");
                                                    float total1=(Float.parseFloat(AppActivity.readFromFile("downloadedLImitMb", "0.0"))-Float.parseFloat(AppActivity.readFromFile(entityy.getName()+"size", "0.0")))+entityy.getSize();
                                                    AppActivity.writeToFile(total1+"","downloadedLImitMb");*/
            // Log.v("Hey This is the actual total", total1+" "+(Float.parseFloat(AppActivity.readFromFile("downloadedLImitMb", "0.0"))-Float.parseFloat(AppActivity.readFromFile(entityy.getName()+"size", "0.0")))+" "+entityy.getSize());
            AppActivity.writeToFile("no", entityy.getName() + "-download");
            SoapCostants.downloadedList.get(i).setProgress("no");
            SoapCostants.downloadedList.get(i).setProgressLevel(0);
            SoapCostants.downloadedList.get(i).setIsdone(true);
            DashboardActivity2.adaptr.notifyDataSetChanged();
            Log.v("pr>>>>>>>>>>>>>>>>>", "Invalidating" + entityy.getName());
            //  Log.v("prTotal>>>>>>>>>>>>>>>>>", AppActivity.readFromFile("downloadedLImitMb", "0.0")+"");
        }
    }
}
                                        }
                                    }
                                } catch (Exception e) {
                                    // TODO: handle exception
                                    //Log.v("Key>>>>>>>>>>>>>>>", /*SoapCostants.downloadList.get(j).getKey()+*/"  "+entityy.getName()+"" );
                                }
                            }

                        }
                        if (ifInvalidate) {
                            runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    List<DraggedVideoEntity> newList=new ArrayList<DraggedVideoEntity>();
                                    newList.addAll(SoapCostants.downloadedList);
                                    SoapCostants.downloadedList.clear();
                                    SoapCostants.downloadedList.addAll(newList);
                                    if (DashboardActivity2.adaptr!=null) {
                                        DashboardActivity2.adaptr.notifyDataSetChanged();
                                    }
                                   // DashboardActivity2.travelReady.setAdapter(adaptr);
                                    adaptr.notifyDataSetChanged();
                                    ((DraggedVideoAdapter2) travelReady.getAdapter()).notifyDataSetChanged();
                                    Log.v("pr>>>>>>>>>>>>>>>>>", "Invalidating");
                                    //travelReady.setAdapter(adaptr);
                                }
                            });
                        }
                    }

                }
            },0, 2, TimeUnit.SECONDS);

这是我的适配器类:

public class DraggedVideoAdapter extends ArrayAdapter<DraggedVideoEntity>{
    List<DraggedVideoEntity> videoList;
    Context context;
    View v;
    DraggedVideoEntity entity;
    LayoutInflater inflater;
    Dialog d1;

    int resource;
    VideoView myVideoView;
    public ImageLoader imageLoader;
    String from;
    ScheduledExecutorService scheduler;
    PieProgressDrawable pieProgressDrawable;
    //float height, width;


    public DraggedVideoAdapter(Context context, int resource,
            List<DraggedVideoEntity> videoList,float height, float width,String from) {
        super(context, resource, videoList);
        // TODO Auto-generated constructor stub
        this.videoList=videoList;
        this.resource=resource;
        this.context=context;
        this.from=from;
         pieProgressDrawable = new PieProgressDrawable();
        imageLoader=new ImageLoader(context.getApplicationContext());
        inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        if (videoList.size()>0) {
            if (!SoapCostants.onceLoaded) {
               // new AppActivity.progressUpdater(context).execute();
                SoapCostants.onceLoaded = true;
            }
        }
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        v = convertView;
        final ViewHolder holder;
        entity = videoList.get(position);

        if (convertView == null) {
            v = inflater.inflate(resource, null);
            holder = new ViewHolder();
            setViewHolder(holder);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }
        if (entity.getMpaaRating()!=null)
        holder.mpaaRating.setText(entity.getMpaaRating());
        if (entity.getMpaaRating()!=null)
        holder.mpaaRating1.setText(entity.getMpaaRating());
        if (entity.getTitle()!=null)
        holder.title.setText(entity.getTitle());
        if (entity.getTitle()!=null)
        holder.title2.setText(entity.getTitle());
        if (entity.getDuration()!=null)
        holder.duration.setText(entity.getDuration());
        if (entity.getDuration()!=null)
        holder.duration1.setText(entity.getDuration());
        if (entity.getGenre()!=null)
        holder.genre.setText(entity.getGenre());
        holder.genre1.setText(entity.getGenre());

        //holder.circularProgressbar.setProgressDrawable(pieProgressDrawable);
        if (holder.circularProgressbar != null) {
            holder.circularProgressbar.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        holder.circularProgressbar.setProgress(0);
            try {

            holder.circularProgressbar.setProgress(entity.getProgressLevel());
        //}
            }
            catch (Exception e){

            }
        if (entity.getProgress() != null) {
            if (entity.getProgress().equalsIgnoreCase("no")/*&& AppActivity.readFromFile(entity.getName()+"-download", "").equalsIgnoreCase("no")*/) {
                holder.circularProgressbar.setVisibility(View.GONE);


            } else {
                holder.circularProgressbar.setVisibility(View.VISIBLE);

            }
        } else {
            holder.circularProgressbar.setVisibility(View.GONE);
        }
    }
        holder.description2.setText(entity.getDescription());
        if (!SoapCostants.isfull || from.equalsIgnoreCase("dashboard")) {
            Drawable d = (Drawable) Drawable.createFromPath(Environment.getExternalStorageDirectory()+File.separator+"ToteFlixImages_cache"+File.separator+entity.getName().replace(".mp4", ".jpg"));
            holder.video1.setBackgroundDrawable(d);
        }
        else {
            imageLoader.DisplayImage(entity.getImageUrl(), holder.video,entity.getName().replace(".mp4", ".jpg"));
        }


        if (/*entity.getIsfull().equalsIgnoreCase("no")&&*/ !SoapCostants.isfull || from.equalsIgnoreCase("dashboard")) {
            holder.shortView.setVisibility(View.VISIBLE);
            holder.fullView.setVisibility(View.GONE);
        }
        else {
            holder.shortView.setVisibility(View.GONE);
            holder.fullView.setVisibility(View.VISIBLE);
        }
holder.circularProgressbar.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view) {
        if (!SoapCostants.isalreadyOPen) {
            if (SoapCostants.viewOpened.equals("removed") || SoapCostants.viewOpened.equals("remove")) {
                Intent i1 = new Intent(context, FullTravelReady.class);
                i1.putExtra("itemPos", position+"");
                context.startActivity(i1);
            } else {
                Intent i2 = new Intent(context, FullTravelReady.class);
                i2.putExtra("itemPos", position+"");
                context.startActivity(i2);
            }
        }
    }
});
        if (d1!=null) {
            d1.setOnKeyListener(new Dialog.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface arg0, int keyCode,
                        KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        // context.finish();
                        myVideoView.stopPlayback();
                        d1.dismiss();

                    }
                    return true;
                }
            }); 
        }

        return v;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        try {
            if (observer != null) {
                super.unregisterDataSetObserver(observer);
            }
        }
        catch(Exception e){

        }

    }
    private void setViewHolder(ViewHolder holder) {
        // TODO Auto-generated method stub
        holder.mpaaRating=(TextView)v.findViewById(R.id.mpaaRating);
        holder.mpaaRating1=(TextView)v.findViewById(R.id.mpaaRating1);
        holder.title=(TextView)v.findViewById(R.id.title);
        holder.duration=(TextView)v.findViewById(R.id.duration);
        holder.duration1=(TextView)v.findViewById(R.id.duration1);
        holder.genre=(TextView)v.findViewById(R.id.genre);
        holder.genre1=(TextView)v.findViewById(R.id.genres1);
        holder.title2=(TextView)v.findViewById(R.id.title2);
        holder.description2=(TextView)v.findViewById(R.id.description2);
        holder.play=(ImageView)v.findViewById(R.id.play);
        holder.shortView=(RelativeLayout)v.findViewById(R.id.shortView);
        holder.fullView=(LinearLayout)v.findViewById(R.id.fullView);
        holder.video=(ImageView)v.findViewById(R.id.video);
        holder.video1=(ImageView)v.findViewById(R.id.video1);
        holder.circularProgressbar=(ProgressPieView)v.findViewById(R.id.circularProgressbar);

    }

    class ViewHolder{
        TextView mpaaRating,mpaaRating1, title,duration,duration1,title2,description2,genre,genre1;
        ProgressPieView circularProgressbar;
        ImageView play,video,video1;
        RelativeLayout shortView;
        LinearLayout  fullView;

    }



}

0 个答案:

没有答案