trip03 trip03 - 28 days ago 7
Java Question

Listview not refresh always + screen errors

Hello I have a problem witch Listview.

I use listview for show items in list.

On create activity is everything okay.
...
If I longtouch on item showed dialog where I can edit name item, If confirm editing I switch static variable in activity with listview. (true)This Boolean variable is on Timer which load. And set variable from witch will read items.

I read items with

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);


Everything run. BUT if edit item for 3-5 times thise code ↑ add all rows to thadapter and put it on List (Listview) but Listview can't rewrite listview on display. I have TOAST in thise code and program read good.
Every crashed here

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


But I don't have idea why all run good for some times and next have some error and next run good too (for some times)
And If isn't run good for item in first row, prgram run good for other rows for some times too.

It isn't repeatly its random...

Please Do you have some ideas?

Thanks.




This code call code which read and put info on listview..

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);


sorry for English :/

____________________________________________reaction for coments

Here I set static variable Boolean is in timer and AddItemInPacage.prvy=prvy; is class wehere are saved info who I try put on listview

Statis because I set it from popup (dialog)

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


Here are some method from class where I saved all variables (which i need on thise problem).

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 CODE MY __________________________________

Activity where I have listview.

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;


}


}


Dialog

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();


}

}


Screen
Wrong view

Sometimes if I edit row Toas read good string, but Listview no rewrite.

Answer

I needed a synchronized confirm edit in Dialog with a method which rewrote Listview.

Or switch sequence from

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

to

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

Because If AddItemInPacage.obnovitem=true; first Activity works with old information in variable prvy. And when variable prvy is full from dialog. Listview finish work with old information in this variable.

Comments