我有20个按钮和GridView的Activity。 一键清除GridView和其他添加到GridView的不同值。 有时我按1-Add,2-Clear,3-Add但看到按钮清除被推(橙色)并执行1-Add,2-Add,3-Clear。 一些时间按钮清除变得长时间按,并且根本不回答,但其他按钮工作。 这怎么可能? 我的活动:
public class Smena extends Activity
{
public Nomenklatura Asortiment;
GridView gvMain;
GridView gvMain1;
GridView gvMain2;
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
ArrayAdapter<String> adapter2;
double Sum=0;
String Sdacha;
String filename="shops345";
Boolean PushX=false;
TextView tv1;
void Save()
{
LocalPersistence.witeObjectToFile(this, Asortiment, filename);
}
public void myClickHandler(View target){
Intent intent1;
switch (target.getId()){
case R.id.button1:
ButtonOn (Asortiment.SmenaButName[0],Asortiment.SmenaButPrice[0]);
break;
case R.id.button10:
ButtonOn (Asortiment.SmenaButName[9],Asortiment.SmenaButPrice[9]);
break;
case R.id.button11:
ButtonOn (Asortiment.SmenaButName[10],Asortiment.SmenaButPrice[10]);
break;
case R.id.button12:
ButtonOn (Asortiment.SmenaButName[11],Asortiment.SmenaButPrice[11]);
break;
case R.id.button13:
ButtonOn (Asortiment.SmenaButName[12],Asortiment.SmenaButPrice[12]);
break;
case R.id.button14:
ButtonOn (Asortiment.SmenaButName[13],Asortiment.SmenaButPrice[13]);
break;
case R.id.button15:
ButtonOn (Asortiment.SmenaButName[14],Asortiment.SmenaButPrice[14]);
break;
case R.id.button16:
intent1=new Intent(Smena.this,Napitki.class);
Save();
startActivity(intent1);
break;
case R.id.button17:
intent1=new Intent(Smena.this,Zhevacki.class);
Save();
startActivity(intent1);
break;
case R.id.button18:
intent1=new Intent(Smena.this,Dobavki.class);
Save();
startActivity(intent1);
break;
case R.id.button19:
intent1=new Intent(Smena.this,Presmena.class);
Save();
startActivity(intent1);
break;
case R.id.button2:
ButtonOn (Asortiment.SmenaButName[1],Asortiment.SmenaButPrice[1]);
break;
case R.id.button20:
intent1=new Intent(Smena.this,Orp.class);
Asortiment.data.clear();
Asortiment.data1.clear();
Save();
startActivity(intent1);
break;
case R.id.button21:
ButtonDel();
break;
case R.id.button22:
clear();
break;
case R.id.button23:
intent1=new Intent(Smena.this,Sdacha.class);
Save();
startActivity(intent1);
break;
case R.id.button25:
clear();
// checkTv();
break;
case R.id.button3:
ButtonOn (Asortiment.SmenaButName[2],Asortiment.SmenaButPrice[2]);
break;
case R.id.button4:
ButtonOn (Asortiment.SmenaButName[3],Asortiment.SmenaButPrice[3]);
break;
case R.id.button41:
if(PushX)
buttonx(3);
else buttonx(2);PushX=true;
break;
case R.id.button42:
if(PushX)
buttonx(4);
else buttonx(3);PushX=true;
break;
case R.id.button43:
if(PushX)
buttonx(5);
else buttonx(4);PushX=true;
break;
case R.id.button44:
if(PushX)
buttonx(6);
else buttonx(5);PushX=true;
break;
case R.id.button45:
if(PushX)
buttonx(7);
else buttonx(6);PushX=true;
break;
case R.id.button46:
if(PushX)
buttonx(8);
else buttonx(7);PushX=true;
break;
case R.id.button5:
ButtonOn (Asortiment.SmenaButName[4],Asortiment.SmenaButPrice[4]);
break;
case R.id.button6:
ButtonOn (Asortiment.SmenaButName[5],Asortiment.SmenaButPrice[5]);
break;
case R.id.button7:
ButtonOn (Asortiment.SmenaButName[6],Asortiment.SmenaButPrice[6]);
break;
case R.id.button8:
ButtonOn (Asortiment.SmenaButName[7],Asortiment.SmenaButPrice[7]);
break;
case R.id.button9:
ButtonOn (Asortiment.SmenaButName[8],Asortiment.SmenaButPrice[8]);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smena);
Intent i=getIntent();
Asortiment=(Nomenklatura) LocalPersistence.readObjectFromFile(this, filename);
adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
tv1=(TextView) findViewById(R.id.textView1);
gvMain = (GridView) findViewById(R.id.gridView1);
gvMain.setAdapter(adapter);
gvMain1 = (GridView) findViewById(R.id.gridView11);
gvMain1.setAdapter(adapter1);
gvMain2 = (GridView) findViewById(R.id.gridView2);
if(Asortiment.data2.size()<=0) data2clear();
if(i.getExtras().containsKey("Dobavki"))
{
ArrayList<String> DobavkiList=new ArrayList<String>();
DobavkiList=i.getExtras().getStringArrayList("Dobavki") ;
for(String item : DobavkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
Double sd;
if(i.getExtras().containsKey("Sdacha"))
{
sd=Double.parseDouble(Asortiment.data2.get(1));
Sdacha=i.getExtras().getString("Sdacha");
sd=Double.parseDouble(Sdacha)-sd;
}
else
{
if(Asortiment.data2.size()>0)
{
Sdacha=Asortiment.data2.get(3);
sd=Double.parseDouble(Asortiment.data2.get(1));
if(Double.parseDouble(Asortiment.data2.get(3))>0)
sd=Double.parseDouble(Asortiment.data2.get(3))-sd;
else sd=(double) 0;
}else {sd=(double) 0;Sdacha="0";}
}
Asortiment.data2.set(3, Sdacha);
Asortiment.data2.set(5, sd.toString());
adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);
gvMain2.setAdapter(adapter2);
}
void buttonx(int k){
if(!Asortiment.LastButton.get(0).equals("0")){
for (int i=0;i<k;i++){
ButtonOn (Asortiment.LastButton.get(0),Asortiment.LastButton.get(1));
}}
}
void checkTv()
{
Date dt1=new Date();
if(dt1.getHours()>19 && Asortiment.LastRequest.getDay()<dt1.getDay() && (Calendar.DAY_OF_WEEK==2 ||Calendar.DAY_OF_WEEK==4 || Calendar.DAY_OF_WEEK==6))
tv1.setText("ОТПРАВЬТЕ ЗАЯВКУ");
else tv1.setText("");
}
void clear()
{
Asortiment.data.clear();
adapter.clear();
Asortiment.data1.clear();
adapter1.clear();
data2clear();
}
void data2clear()
{
Asortiment.data2.set(1, "0");
Asortiment.data2.set(3, "0");
Asortiment.data2.set(5, "0");
gvMain2.setAdapter(adapter2);
}
private void ButtonDel()
{
int cnt= Asortiment.data.size();
if(cnt>0){
Double d=Double.parseDouble(Asortiment.data2.get(1));//1
d=d-Double.parseDouble(Asortiment.data1.get(cnt-1));
Asortiment.data2.set(1, d.toString());
//adapter2 = new ArrayAdapter<String>(this, R.layout.item2, R.id.tvText, Asortiment.data2);//1
gvMain2.setAdapter(adapter2);
Asortiment.data.remove(cnt-1);
Asortiment.data1.remove(cnt-1);
//adapter = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data);
//adapter1 = new ArrayAdapter<String>(this, R.layout.item, R.id.tvText, Asortiment.data1);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
}
}
private void ButtonOn (String name, String price)
{
PushX=false;
if(name.length()>15)name=name.substring(0, 15);
Asortiment.LastButton.set(0, name);
Asortiment.LastButton.set(1, price);
Asortiment.data.add(name);
Asortiment.data1.add(price);
gvMain.setAdapter(adapter);
gvMain1.setAdapter(adapter1);
Double d=Double.parseDouble(Asortiment.data2.get(1));
d=Double.parseDouble(price)+d;
Asortiment.data2.set(1, d.toString());
Double sd=Double.parseDouble(Asortiment.data2.get(3));
if (sd>0) {
sd=sd-d;
Asortiment.data2.set(5, sd.toString());
}
gvMain2.setAdapter(adapter2);
}
}
答案 0 :(得分:0)
要解决此问题,甚至缩小问题范围,您需要重构代码。每次点击都会发生一系列事情,你可能会淹没点击。你是否在点击之间留出足够的时间?
另外,请考虑使方法通用,例如
if(i.getExtras().containsKey("Napitki"))
{
ArrayList<String> NapitkiList=new ArrayList<String>();
NapitkiList=i.getExtras().getStringArrayList("Napitki") ;
for(String item : NapitkiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
if(i.getExtras().containsKey("Zhevacki"))
{
ArrayList<String> ZhevackiList=new ArrayList<String>();
ZhevackiList=i.getExtras().getStringArrayList("Zhevacki") ;
for(String item : ZhevackiList)
{
String Art=Asortiment.GetArtByName(item);
String [] np=Asortiment.GetNamePriceByArt(Art);
ButtonOn (np[0],np[1]);
}
}
像这样的块可以作为通用方法制作,也许你想遵循java约定。
只是一个建议:)