你好,我有一个问题是Listview。
我将列表视图用于列表中的显示项目。
创造活动就是一切都好。 ... 如果我在项目上显示长按对话框,我可以编辑名称项目,如果确认编辑我在listview中切换活动中的静态变量。 (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并将其放在List(Listview)上但Listview不能重写listview显示。我有这个代码和程序读取良好的TOAST。 每个人都在这里坠毁
thadapter = new MyThumbnailAdapter(getApplication(), R.layout.list_row, ListAdapter);
List.setAdapter(thadapter);
但是我不知道为什么所有人都运行良好一段时间,接下来有一些错误,然后运行良好(有些时候) 并且如果第一行中的项目运行不正常,prgram也会对其他行运行一段时间。
它不是重复的随机......
请你有一些想法吗?
感谢。
此代码调用代码,用于在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);
抱歉英语:/
____________________________________________对焦虑的反应
这里我设置静态变量Boolean是在timer和AddItemInPacage.prvy = prvy;是类我在哪里保存信息我尝试放在listview
Statis因为我是从弹出窗口(对话框)
设置的AddItemInPacage.obnovitem=true;
AddItemInPacage.prvy=prvy;
以下是我保存所有变量的类的一些方法(我需要解决这个问题)。
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;
}
_________________________________完整的代码__________________________________
我有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;
}
}
对话框
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没有重写。
答案 0 :(得分:0)
每当您更改ListView使用的数据时,您必须在列表的适配器上调用Memory.h:
#include <pthread.h>
class Memory {
static pthread_mutex_t mutex_;
}
Memory.cpp:
#include <Memory.h>
#include <pthread.h>
Memory::Memory() :
#ifdef __APPLE__
pthread_mutex_init(&mutex_, NULL);
#endif
。也许这会解决问题。
答案 1 :(得分:0)
我需要在Dialog中使用重写Listview的方法进行同步确认编辑。
或从
切换序列AddItemInPacage.obnovitem = true;
AddItemInPacage.prvy = prvy;
到
AddItemInPacage.prvy = prvy;
AddItemInPacage.obnovitem = true;
因为如果AddItemInPacage.obnovitem=true;
第一个Activity使用变量prvy中的旧信息。当变量prvy从对话框中填满时。 Listview使用此变量中的旧信息完成工作。