列表视图不总是刷新 + 屏幕错误

Listview not refresh always + screen errors

你好,我有一个问题 Listview。

我使用列表视图显示列表中的项目。

创建 activity 一切正常。 ... 如果我在项目上长按显示对话框,我可以在其中编辑名称项目,如果确认编辑我在 activity 中使用 listview 的 s 静态变量。 (true) 这个布尔变量在加载的 Timer 上。从 设置变量将读取项目。

我用

阅读了项目
 ListAdapter.clear();
    thadapter.clear();
    for (int i=1;i<prvy.getPacageItem2(indexpacage)+1;i++) {

        ListAdapter.add(String.valueOf(prvy.citajItem(indexpacage,i)));
Toast.makeText(this, "obnovujem" + " "+ prvy.citajItem(indexpacage,i), Toast.LENGTH_SHORT).show();

              }


        thadapter = new MyThumbnailAdapter(getApplication(), R.layout.list_row, ListAdapter);
        List.setAdapter(thadapter);

一切运行。但是,如果编辑项目 3-5 次此代码 ↑ 将所有行添加到 thadapter 并将其放在列表(列表视图)上,但列表视图无法在显示时重写列表视图。我在这个代码中有 TOAST,程序读起来很好。 每个都在这里坠毁

thadapter = new MyThumbnailAdapter(getApplication(), R.layout.list_row, ListAdapter);
        List.setAdapter(thadapter);

但我不明白为什么所有 运行 有时都很好,然后下一个有一些错误,而下一个 运行 也很好(有时) 如果 运行 不适合第一行的项目,那么 prgram 运行 有时也适合其他行。

它不是重复的随机...

请问您有什么想法吗?

谢谢。


此代码调用读取信息并将信息放入列表视图的代码。

 Timer timer = new Timer();
    //TODO timer
    timer.schedule(
            new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(
                            new Runnable() {
                                @Override
                                public void run() {
                                    if (obnovitem) {
ObnovItem();


                                        obnovitem= false;

                                    }
                                }
                            }
                    );
                }
            }, 0, 150);

对不起英语:/

____________________________________________reaction评论

这里我设置静态变量Boolean在timer中并且AddItemInPacage.prvy=prvy;是 class 我们在这里保存了我尝试放在列表视图中的信息

Statis 因为我是从弹出窗口(对话框)设置的

AddItemInPacage.obnovitem=true;
        AddItemInPacage.prvy=prvy;

这里有一些来自 class 的方法,我在其中保存了所有变量(我在这个问题上需要这些变量)。

String[][][] packinglist = new String[6][100][2];

  public String citajItem(int pacage, int item) {
    return packinglist[pacage][item][0];
}

 public void pridajItem(String nazovbalika,int pacagenum) {
    pocItem++;
    setPacageItem2(getPacageItem2(pacagenum)+1,pacagenum);
    packinglist[pacagenum][packageitem++][0]= nazovbalika;
}

_________________________________FULL 编码我的 __________________________________

Activity 我有列表视图。

public class AddItemInPacage extends Activity implements
        View.OnClickListener, Runnable, OnKeyListener {

    FlyOutContainer root;

    public static int ktore;
    public static String nazovtripu;
    public static int indexpacage;
    public static Boolean obnovitem=false;
    static myTrip prvy;

  //  public Activity c;
    public Dialog d;
    public Button yes;
    public ImageView no;
    public TextView title;

   // public String nazovtripu;
    Poradie  poradie= new Poradie();

    Boolean vysunuty=false;

    private ListView mainListView ;



    int cisloblik=0;

    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;
 //   List<String> list;
    EditText edt;

    private ListView List;
    MyThumbnailAdapter thadapter = null;
    ArrayList<String> ListAdapter = new ArrayList<String>();
    ArrayList<String> ListAdapterClone = new ArrayList<String>();
    int number_count = 1;
    int pomdeser=0;

/*
    public AddItemInPacage(Activity a,String nazovtripu,int indexpacage) {

        // TODO Auto-generated constructor stub
        this.c = a;
        this.nazovtripu=nazovtripu;
        this.indexpacage=indexpacage;
      //  list = new ArrayList<>();
    }
*/
    public AddItemInPacage(){

    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   //     requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_add_item_in_pacage,null);
        this.setContentView(root);

        List = (ListView) findViewById(R.id.listViewTST);

    //    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);


    //    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
     //   recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        NacitajBok();
     //   mainListView.setAdapter( listAdapter );



        TextView edit= (TextView)findViewById(R.id.editing);
        title= (TextView)findViewById(R.id.title);


        edt=(EditText)findViewById(R.id.editAdd);
        edt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if ( (actionId == EditorInfo.IME_ACTION_DONE)||(actionId == EditorInfo.IME_ACTION_NEXT)) {
                Add();
            return true;
        }
        else{
            return false;
        }
    }
});



        prvy= DeSerializuj(nazovtripu);
        title.setText(prvy.getNazovTripu());
        edit.setText("ADDING in,,"+ prvy.citajPackageName(indexpacage)+"''");

        for (int i=1;i<prvy.getPacageItem2(indexpacage)+1;i++) {

            ListAdapter.add(String.valueOf(prvy.citajItem(indexpacage,i)));
        }

        thadapter = new MyThumbnailAdapter(this, R.layout.list_row, ListAdapter);

        List.setAdapter(thadapter);



        View.OnClickListener myhandler1 = new View.OnClickListener() {
            public void onClick(View v) {
                Add();
            }
        };
        View.OnClickListener myhandlerfin = new View.OnClickListener() {
            public void onClick(View v) {
                InfoPackingTrip.PrepisImg(indexpacage);
                InfoPackingTrip.Pomocnapokliku();


          //      Collections.reverse(ListAdapter);


                /*
                for (int i=1;i<100;i++) {
                    try {
                        if(ListAdapter.get(i-1)!=null) {
                            prvy.pridajItemRemove(ListAdapter.get(i - 1), indexpacage, i);
                          //  prvy.setPackageitem(i);
                            prvy.setPacageItem2(i,indexpacage);
                            prvy.setPocItem(i);
                        }else {prvy.Nulak(indexpacage,i);}
                    } catch(IndexOutOfBoundsException e) {
                        prvy.Nulak(indexpacage,i);
                    }
                }
                */

                for (int i=ListAdapter.size()-1;i>=0;i--) {
          //      for (int i = 0; i < ListAdapter.size() ; i++) {
                    try {
                            prvy.pridajItemRemove(ListAdapter.get(i), indexpacage, i+1);
                    } catch(IndexOutOfBoundsException e) {
                        prvy.Nulak(indexpacage,i);
                    }
                }

                prvy.setPacageItem2(ListAdapter.size(),indexpacage);
                prvy.setPocItem(ListAdapter.size());

                for (int i=ListAdapter.size();i<99;i++) {
                    prvy.Nulak(indexpacage,i+1);
                }

                Serializuj(prvy,nazovtripu);

                InfoPackingTrip.ktore=ktore;
          //      Intent intent = new Intent(AddItemInPacage.this, InfoPackingTrip.class);
            //    startActivity(intent);
                ///////Hned otvorené vo full verzii nová activity
                startActivity(new Intent(v.getContext(), InfoPackingTrip.class));
                try {
                    this.finalize();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                overridePendingTransition(0, 0);
            }
        };

        View.OnClickListener myhandlertoogle = new View.OnClickListener() {
            public void onClick(View v) {
                root.toggleMenu();
                if(vysunuty) {vysunuty=false;} else {vysunuty=true;}
            }
        };



        FrameLayout Addlayout = (FrameLayout)findViewById(R.id.AddNext2);
        Addlayout.setOnClickListener(myhandler1);
        FrameLayout fin = (FrameLayout) findViewById(R.id.btndalej);
        fin.setOnClickListener(myhandlerfin);
        ImageView menu = (ImageView) findViewById(R.id.menu);
        menu.setOnClickListener(myhandlertoogle);
        ImageView back = (ImageView) findViewById(R.id.btnback);
        back.setOnClickListener(myhandlertoogle);


        Kontroler2();

    }



    public void Add() {

        if (!(String.valueOf(edt.getText()).equals(""))) {

            ListAdapter.add(ListAdapter.size(),String.valueOf(edt.getText()));
            thadapter = new MyThumbnailAdapter(this, R.layout.list_row, ListAdapter);
     //       Collections.reverse(ListAdapter);
            List.setAdapter(thadapter);


            prvy.pridajItem(String.valueOf(edt.getText()),indexpacage);
            edt.setText("");
      //      Serializuj(prvy,nazovtripu);

        }
    }


    @Override
    public void onClick(View v) {


    }



    public myTrip DeSerializuj( String odkial) {
        odkial=odkial+".dat";
        myTrip   prvy = null;
        try {
            File file = new File(this.getFilesDir(), odkial);
            FileInputStream fi = new FileInputStream(file);
            ObjectInputStream oi = new ObjectInputStream(fi);
            prvy = (myTrip) oi.readObject();
            oi.close();
            fi.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prvy;
    }

    public void Serializuj( myTrip serTrip,String kam){
        kam=kam + ".dat";
        try {
            File file = new File(this.getFilesDir(), kam);
            FileOutputStream fo = new FileOutputStream(file);
            ObjectOutputStream ou = new ObjectOutputStream(fo);
            ou.writeObject(serTrip);
            ou.close();
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /////Swipe

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Only if you need to restore open/close state when
        // the orientation is changed
        if (adapter != null) {
            adapter.restoreStates(savedInstanceState);
        }
    }

    // preblik pisania
    private void PreblikPis(int cislo) {

        EditText edt=(EditText)findViewById(R.id.editText);
        if (cislo%2==0) {
            edt.setText("|");
        } else {edt.setText("");}

    }

    @Override
    public void run() {

        try {
            Thread.sleep(500);

        cisloblik++;
        PreblikPis(cisloblik);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        // Listen to "Enter" key press

        if ((keyCode == KeyEvent.KEYCODE_ENTER)||(keyCode == EditorInfo.IME_ACTION_DONE))  {
            //Toast.makeText(c, "sdaaa", Toast.LENGTH_SHORT).show();
            Add();
            return true;
        }

        if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION) {
            //your code here
            Add();
            return true;
        }
        return false;

    }

    private void NacitajBok() {

        TextView tv = (TextView) findViewById(R.id.Yourtrips);
        Typeface firstTripText = Typeface.createFromAsset(this.getAssets(), "fonts/ROBOTOSLAB-BOLD.TTF");
        tv.setTypeface(firstTripText);

        tv = (TextView) findViewById(R.id.settings);
        tv.setTypeface(firstTripText);

        tv = (TextView) findViewById(R.id.about);
        tv.setTypeface(firstTripText);

        firstTripText = Typeface.createFromAsset(this.getAssets(), "fonts/Roboto-Regular.ttf");
        tv = (TextView) findViewById(R.id.addTrip);
        tv.setTypeface(firstTripText);

        ListView mainListView;
        ArrayAdapter<String> listAdapter;

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.listView);

        // Create and populate a List of planet names.
        String[] trips = new String[]{};
        ArrayList<String> tripList = new ArrayList<String>();
        tripList.addAll(Arrays.asList(trips));

        // Create ArrayAdapter using the planet list.
        listAdapter = new ArrayAdapter<String>(this, R.layout.listrow_bocny, tripList);



        poradie = DeSerializujporadie();

        if (poradie != null) {
            //int pppo =  poradie.getI();
            for (int i = 0; i < poradie.getI(); i++) {


                listAdapter.add(String.valueOf(poradie.Citaj(i)).substring(0, String.valueOf(poradie.Citaj(i)).length() - 4));
            }

            mainListView.setAdapter(listAdapter);
            mainListView.setOnItemClickListener(
                    new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {


                            view.setBackgroundColor(Color.argb(255,253,62,86));

                            TextView yourTextView = (TextView) view.findViewById(R.id.textView1);
                            yourTextView.setTextColor(Color.WHITE);


                            android.os.Handler mHandler = new android.os.Handler();
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    InfoPackingTrip.ktore=poradie.Zistikolke(poradie.Citaj(position));
                                    Intent intent = new Intent(AddItemInPacage.this, InfoPackingTrip.class);
                                    startActivity(intent);
                                    try {
                                        this.finalize();
                                    } catch (Throwable throwable) {
                                        throwable.printStackTrace();
                                        Toast.makeText(AddItemInPacage.this, "nevydal koniec", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }, 50);



                        }
                    }
            );
        }


        FrameLayout yourTrips=(FrameLayout)findViewById(R.id.yourtrips);
        yourTrips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddItemInPacage.this, IndexFull.class);
                startActivity(intent);
                try {
                    this.finalize();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                    Toast.makeText(AddItemInPacage.this, "nevydal koniec", Toast.LENGTH_SHORT).show();
                }
            }

        });

        FrameLayout toogle=(FrameLayout)findViewById(R.id.tooglem);
        toogle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {root.toggleMenu();}

        });



    }

    @Override
    public void onBackPressed() {
        // your code.
        if(vysunuty) {
            vysunuty=false;
            this.root.toggleMenu();
        }



    }

    public Poradie DeSerializujporadie() {
        Poradie  por = null;
        try {
            File file = new File(this.getFilesDir(), "poradie.dat");
            FileInputStream fi = new FileInputStream(file);
            ObjectInputStream oi = new ObjectInputStream(fi);
            por= (Poradie) oi.readObject();
            oi.close();
            fi.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return por;
    }

    //TODO listview
    public class MyThumbnailAdapter extends ArrayAdapter<String> {

        ArrayList<String> arr;
        private TextView text;

        public MyThumbnailAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {

            super(context, textViewResourceId, objects);
            this.arr = objects;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View view = null;
            LayoutInflater inflater = getLayoutInflater();
            view = inflater.inflate(R.layout.list_row, parent, false);
            TextView textnumber = (TextView) view.findViewById(R.id.text);
            ImageView delButton = (ImageView) view.findViewById(R.id.btn_del);

          Typeface  robtoLight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
            textnumber.setTypeface(robtoLight);

            delButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    arr.remove(position);
                    thadapter.notifyDataSetChanged();
                    Toast.makeText(getContext(), "Item deleted", Toast.LENGTH_SHORT).show();
                }
            });

            textnumber.setText(arr.get(position));


            textnumber.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    for (int i=ListAdapter.size()-1;i>=0;i--) {
                        //      for (int i = 0; i < ListAdapter.size() ; i++) {
                        try {
                            prvy.pridajItemRemove(ListAdapter.get(i), indexpacage, i+1);
                        } catch(IndexOutOfBoundsException e) {
                            prvy.Nulak(indexpacage,i);
                        }
                    }

                    prvy.setPacageItem2(ListAdapter.size(),indexpacage);
                    prvy.setPocItem(ListAdapter.size());

                    for (int i=ListAdapter.size();i<99;i++) {
                        prvy.Nulak(indexpacage,i+1);
                    }

                    Serializuj(prvy,nazovtripu);


                    EditItemDialog cdd=new EditItemDialog(AddItemInPacage.this,nazovtripu,position+1,indexpacage);
                    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    cdd.show();


                    return true;
                }

            });
            return view;
        }
    }


    private void Kontroler2() {
        Timer timer = new Timer();
        //TODO timer
        timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        if (obnovitem) {
                                            /*
                                            pomdeser++;
                                            if(pomdeser==5){
                                            obnovitem=false;
                                            pomdeser=0;}
                                            */
                                            ObnovItem();
                                            obnovitem= false;

                                        }
                                    }
                                }
                        );
                    }
                }, 0, 150);
    }

    public void ObnovItem() {

     //   prvy= DeSerializuj(nazovtripu);


        ListAdapter.clear();
        thadapter.clear();
        for (int i=1;i<prvy.getPacageItem2(indexpacage)+1;i++) {

            ListAdapter.add(String.valueOf(prvy.citajItem(indexpacage,i)));
            Toast.makeText(this, "obnovujem" + " "+ prvy.citajItem(indexpacage,i), Toast.LENGTH_SHORT).show();
        }


            thadapter = new MyThumbnailAdapter(getApplication(), R.layout.list_row, ListAdapter);
            this.List.setAdapter(thadapter);

        int tralal=0;


    }


}

对话框

 package com.example.flyoutmenuexample;

/**
 * Created by Paradox on 25. 10. 2016.
 */


public class EditItemDialog extends Dialog implements
        android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button yes;
    public ImageView no;
    public ImageView lin;
    public EditText title;
    myTrip prvy;
    int poz;
    int indexpacage;



    public String nazovtripu;

    public EditItemDialog(){
        super(null);
    }

    public EditItemDialog(Activity a,String nazovtripu,int poz,int indexpacage) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
        this.nazovtripu=nazovtripu;
        this.poz=poz;
        this.indexpacage=indexpacage;





    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   //     requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_edit_dialog_items);

        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
     //   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        title = (EditText)findViewById(R.id.editText3) ;

        prvy= DeSerializuj(nazovtripu);
        title.setText(prvy.citajItem(indexpacage,poz));


     //   title.setBackgroundColor(Color.argb(0,254,254,254));


        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);


        title.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ( (actionId == EditorInfo.IME_ACTION_DONE)||(actionId == EditorInfo.IME_ACTION_NEXT)) {
                    if (!(String.valueOf(title.getText()).equals(""))) {
                        String tst = Add();

                        dismiss();
                    } else {
                        Toast.makeText(c, "Napíš dačo", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }
                else{
                    return false;
                }
            }
        });

    }

    private String Add(){

        String novypacage=String.valueOf(title.getText());

        prvy.EditItem(indexpacage,poz,novypacage);
      //  Serializuj(prvy,nazovtripu);

        //  InfoPackingTrip.PrepisImg(prvy.getPackageNum());
        AddItemInPacage.obnovitem=true;
        AddItemInPacage.prvy=prvy;

        return  nazovtripu ;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_yes:
                String tst= Add();
                dismiss();
                // c.finish();
                break;
            case R.id.end:
                dismiss();
                break;
            case R.id.endlin:
                dismiss();
                break;
            default:
                break;
        }
    }

    public myTrip DeSerializuj( String odkial) {
        odkial=odkial+".dat";
        myTrip   prvy = null;
        try {
            File file = new File(getContext().getFilesDir(), odkial);
            FileInputStream fi = new FileInputStream(file);
            ObjectInputStream oi = new ObjectInputStream(fi);
            prvy = (myTrip) oi.readObject();
            oi.close();
            fi.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return prvy;
    }

    public void Serializuj( myTrip serTrip,String kam){
        kam=kam + ".dat";
        try {
            File file = new File(getContext().getFilesDir(), kam);
            FileOutputStream fo = new FileOutputStream(file);
            ObjectOutputStream ou = new ObjectOutputStream(fo);
            ou.writeObject(serTrip);
            ou.close();
            fo.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onBackPressed() {


       dismiss();


    }    

}

屏幕 Wrong view

有时,如果我编辑行,Toas 会读取正确的字符串,但 Listview 不会重写。

无论何时更改 ListView 使用的数据,都必须在列表的适配器上调用 notifyDataSetChanged()。也许这会解决问题。

我需要在对话框中使用重写 Listview 的方法进行同步确认编辑。

或从

切换顺序
AddItemInPacage.obnovitem = true;
AddItemInPacage.prvy = prvy;

AddItemInPacage.prvy = prvy;
AddItemInPacage.obnovitem = true;

因为如果 AddItemInPacage.obnovitem=true; 首先 Activity 使用变量 prvy 中的旧信息。当对话中的变量 prvy 已满时。列表视图使用此变量中的旧信息完成工作。