在我的活动中,我通过选中复选框添加一些东西,如果
list.size()>0
(这个条件在我的适配器中)显示按钮,它将我重定向到第二个活动。在第二个活动中,我显示listview,其中包含静态列表中的项目,当我点击它时,我从列表中删除对象,我也在第二个活动中创建了按钮,这使得list.clear(); finish();
当我返回到第一个活动时,我仍然可见即使清除静态列表也会按下按钮。怎么解决?我需要最简单的想法因为我是android的初学者。所有的答案,建议,线索都很好。如果您不知道如何操作,请弹出线程。感谢您的时间。
public class TowarAdapter extends ArrayAdapter<Towar> {
private List<Towar> items;
private Activity context;
private int i = 0;
ImageButton b_zatwierdz;
int counter = 0;
boolean user_checked = false;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public TowarAdapter(Activity context, int resource, List<Towar> items,
ImageButton b_zatwierdz) {
super(context, resource);
this.b_zatwierdz = b_zatwierdz;
this.items = items;
this.context = context;
}
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Towar getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
static class ViewHolder {
TextView tvNazwaT;
TextView tvCenaT;
ImageView ivTowar;
CheckBox chb_czy_zamowic;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder view;
// LayoutInflater inflator = activity.getLayoutInflater();
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.element, null);
view.tvNazwaT = (TextView) convertView.findViewById(R.id.tvNazwaT);
view.tvCenaT = (TextView) convertView.findViewById(R.id.tvCenaT);
view.chb_czy_zamowic = (CheckBox) convertView
.findViewById(R.id.chb_czy_zamowic);
view.ivTowar = (ImageView) convertView.findViewById(R.id.ivTowar);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.tvNazwaT.setText(items.get(position).getTow_nazwa());
view.tvNazwaT.setTextColor(Color.BLACK);
view.tvCenaT.setText(items.get(position).getTow_cena() + "zł");
for (int i = 0; i < items.size(); i++) {
String s = Integer.valueOf(items.get(position).Kat_id).toString();
int resourceId = context.getResources().getIdentifier("a" + s + i,
"drawable", context.getPackageName());
view.ivTowar.setImageResource(resourceId);
}
view.chb_czy_zamowic
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
final CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
user_checked = true;
if (user_checked == true) {
final Dialog d1 = new Dialog(context);
d1.setContentView(R.layout.ilosc);
d1.getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
d1.setTitle("Wybierz ilość");
final EditText et_Ilosc;
Button b_Ok;
Button b_Odejmij;
Button b_Dodaj;
et_Ilosc = (EditText) d1
.findViewById(R.id.et_Ilosc);
et_Ilosc.setText(String.valueOf(i));
view.chb_czy_zamowic.setClickable(false);
b_Dodaj = (Button) d1
.findViewById(R.id.b_Dodaj);
b_Dodaj.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String zmienna_pom = et_Ilosc.getText()
.toString();
i = Integer.valueOf(zmienna_pom);
if (i < 0) {
Toast t = Toast.makeText(
getContext(),
"Niepoprawna wartość",
Toast.LENGTH_SHORT);
t.show();
} else if (i == items.get(position)
.getTow_ilosc_value()) {
Toast t = Toast
.makeText(
getContext(),
"Osiągnięto wartość maksymalną "
+ items.get(
position)
.getTow_ilosc_value(),
Toast.LENGTH_SHORT);
t.show();
} else if (i > items.get(position)
.getTow_ilosc_value()) {
Toast t = Toast
.makeText(
getContext(),
"Przekroczono wartość maksymalną "
+ items.get(
position)
.getTow_ilosc_value(),
Toast.LENGTH_SHORT);
t.show();
}
else if (et_Ilosc.getText().toString()
.equals("")) {
Toast t = Toast.makeText(
getContext(),
"Uzupełnij pole ilość",
Toast.LENGTH_SHORT);
t.show();
} else {
setI(i);
int k = getI();
k++;
setI(k);
et_Ilosc.setText(String.valueOf(i));
}
}
});
b_Odejmij = (Button) d1
.findViewById(R.id.b_Odejmij);
b_Odejmij
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String zmienna_pom = et_Ilosc
.getText().toString();
i = Integer
.valueOf(zmienna_pom);
if (i < 0) {
Toast t = Toast
.makeText(
getContext(),
"Niepoprawna wartość",
Toast.LENGTH_SHORT);
t.show();
} else if (et_Ilosc.getText()
.toString().equals("")) {
Toast t = Toast
.makeText(
getContext(),
"Uzupełnij pole ilość",
Toast.LENGTH_SHORT);
t.show();
} else {
setI(i);
i--;
setI(i);
et_Ilosc.setText(String
.valueOf(i));
}
}
});
b_Ok = (Button) d1.findViewById(R.id.b_Ok);
b_Ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String zmiennna_pom = et_Ilosc
.getText().toString();
int k = Integer.valueOf(zmiennna_pom);
if (k <= 0
|| k > items.get(position)
.getTow_ilosc_value()) {
Toast t = Toast
.makeText(
getContext(),
"Wybierz z przedziału 1-"
+ items.get(
position)
.getTow_ilosc_value(),
Toast.LENGTH_SHORT);
t.show();
} else if (et_Ilosc.getText()
.toString().equals("")) {
Toast t = Toast.makeText(
getContext(),
"Uzupełnij pole ilość",
Toast.LENGTH_SHORT);
t.show();
} else {
view.chb_czy_zamowic
.setEnabled(false);
// String zmiennna_pom = et_Ilosc
// / .getText().toString();
// int k = Integer
// .valueOf(zmiennna_pom);
items.get(position).Tow_ilosc -= k;
Towar checkedObject = new Towar();
checkedObject.Tow_ilosc = k;
checkedObject.Kat_id = items
.get(position).Kat_id;
checkedObject.kategoria = items
.get(position).kategoria;
checkedObject.Tow_cena = items
.get(position).Tow_cena;
checkedObject.Tow_id = items
.get(position).Tow_id;
checkedObject.Tow_nazwa = items
.get(position).Tow_nazwa;
MainActivity.lista_wybranych_towarow
.add(checkedObject);
k = 0;
setI(0);
// et_Ilosc.setText("");
d1.dismiss();
}
// view.chb_czy_zamowic.setChecked(false);
if (MainActivity.lista_wybranych_towarow
.size() > 0) {
b_zatwierdz
.setVisibility(View.VISIBLE);
}
else
b_zatwierdz
.setVisibility(View.INVISIBLE);
}
});
d1.show();
}
;
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
要使按钮不可见,您需要执行以下操作(我只是提到隐藏按钮的逻辑 - 您必须在侦听器中实现此操作):
Button button = (Button) findViewById(R.layout.button_id); // Point it to the button
if(list_is_empty) {
button.setVisibility(Button.GONE); // This line hides the button
}
知道在Android中,'GONE'用于隐藏视图中的元素,现在布局中可以使用此空间。 “INVISIBLE”表示隐藏窗口小部件时,此窗口小部件的空间仍然不可用。
答案 1 :(得分:0)
您可以在调用活动时在意图中添加额外内容,或者将标记保存在共享首选项中。然后根据标志,您可以将可见性设置为true或false?
答案 2 :(得分:0)
您可以在此处使用startActivityForResult
。当你delete object from list
。传回boolean where like 'isDelete'
并在onActivityResult
中检查此变量(这是第一个活动),如果它是true i.e object is delete
,请将按钮visibility
设置为false,否则不执行任何操作。
你也可以在这里使用sharedpreferences。跟踪布尔变量并根据其值设置按钮可见性。
共享偏好的执行此操作:
删除对象时执行此操作,将布尔值写入共享首选项
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); //creating object of shared preference
SharedPreferences.Editor editor = preferences.edit(); //getting editor to write value
editor.putBoolean("isShow",false); //first value is key and second is the value which you are going to assign it
editor.commit();
and in your main adapter class do :
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean show = preferences.getBoolean("isShow",false); //first value is key and second value is used if isShow is not defined.
if(show)
//show the button
else
//hide the button