チェックボックスをクリックするたびにTOASTをリストビューに表示する必要があります。リストビューでチェックボックスをオンにするたびにTOASTをどのように表示しますか?
public class MyListFragment extends Fragment implements
CompoundButton.OnCheckedChangeListener{
ListView lv;
ArrayList<Planet> planetList;
static PlanetAdapter plAdapter;
private TextView txtName;
private TextView txtEmail;
private Button btnLogout;
ListView listView;
String user="";
private Spinner spinner;
String selState;
EditText cristo;
private String zao;
private CheckBox ck;
private SQLiteHandler db;
private SessionManager session;
BirraAdapter biAdapter;
PlanetAdapter.PlanetHolder holder;
private static Context context = null;
private static FragmentActivity mInstance;
Integer[] imageId = {
R.mipmap.androtuto,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ok,
/*R.drawable.image6,
R.drawable.image7*/
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the custom_spinner_items for this fragment
//super.onDestroy();
SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
lv = (ListView) rootView.findViewById(R.id.listview);
ListAdapter listAdapter = new MyListAdapter(getContext());
lv.setAdapter(listAdapter);
context = getActivity();
mInstance = getActivity();
// txtName = (TextView) rootView.findViewById(R.id.name);
// txtEmail = (TextView) rootView.findViewById(R.id.numero_telefonico);
btnLogout = (Button) rootView.findViewById(R.id.btnLogout);
//spinner = (Spinner) rootView.findViewById(R.id.simpleSpinner);
// selState = (String) spinner.getSelectedItem().toString();
//cristo=(EditText)rootView.findViewById(R.id.editText2);
ck=(CheckBox)rootView.findViewById(R.id.chk_box);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
lv = (ListView)getView().findViewById(R.id.listview);
displayPlanetList();
}
private void displayPlanetList() {
planetList = new ArrayList<Planet>();
planetList.add(new Planet("Margherita", "6", "€","(pomodoro e mozzarella)"));
planetList.add(new Planet("Diavola", "7","€","(pomodoro,mozzarella e salsiccia piccante)"));
planetList.add(new Planet("Bufalina", "5","€","(pomodoro e mozzarella di bufala)"));
planetList.add(new Planet("Marinara", "5", "€","(pomodoro)"));
planetList.add(new Planet("Viennese", "4", "€", "(pomodoro,mozzarella e wrustel)"));
plAdapter = new PlanetAdapter(planetList, getContext(),imageId) {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = lv.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
Planet p = planetList.get(pos);
p.setSelected(isChecked);
/*Toast.makeText(
getActivity(),
"Clicked on Pizza: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
};
lv.setAdapter(plAdapter);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String pizzeOrdinate="";
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
pizzeOrdinate+="\n" + p.getName() + " " +p.getTipo() + " " +p.getDistance() + "€" + "q.tà :" + p.getQuantità();
}
}
Toast.makeText(
getActivity(),
"Clicked on Pizza: " + pizzeOrdinate + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public String showResultTotale2(View v) {
//selState=cristo.getText().toString();
int totalAmount=0;
String pizzeOrdinate="";
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
pizzeOrdinate+="\n" + p.getName() + " " +p.getTipo() + " " +p.getDistance() + "€" + "q.tà :" + p.getQuantità();
}
}
return pizzeOrdinate;
}
私はボタンをクリックした場合の方法showResultTotale2()は動作しますが、私が上で動作し、同じ方法をしたいすべてのチェックボックスをクリックしてください:
は、これは私のコードです。
誰が私を助けることができますか?ありがとうございます!
アダプタクラス:
public abstract class PlanetAdapter extends ArrayAdapter<Planet> implements CompoundButton.OnCheckedChangeListener, View.OnClickListener
{
private List<Planet> planetList;
private Context context ;
ArrayList<Birra> objects;
public boolean Checked;
private Integer[] imageId;
String ciao;
static PlanetAdapter plAdapter;
String selState ;
/* public PlanetAdapter(Context context) {
super(context, R.layout.single_listview_item);
}
*/
/*public PlanetAdapter(FragmentManager fm,List<Planet> planetList, Context context,Integer[] imageId) {
super(context, R.layout.single_listview_item,planetList);
this.fm=fm;
this.planetList = planetList;
this.context = context;
this.imageId = imageId;
}
*/
public PlanetAdapter(List<Planet> planetList, Context context,Integer[] imageId) {
super(context, R.layout.single_listview_item,planetList);
this.planetList = planetList;
this.context = context;
this.imageId = imageId;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
@Override
public void onClick(View v) {
}
public class PlanetHolder {
public TextView planetName;
public TextView distView;
public TextView valuta;
public CheckBox chkBox;
public EditText edit;
public TextView caratteristica;
public boolean checked;
public TextView id;
public ImageView imageView;
public Spinner spinner;
public EditText edit2;
public Button iterator;
}
@Override
public int getCount() {
return planetList.size();
}
@Override
public Planet getItem(int position) {
return planetList.get(position);
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View row = convertView;
PlanetHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.single_listview_item, parent, false);
holder = new PlanetHolder();
holder.planetName = (TextView) row.findViewById(R.id.name);
holder.distView = (TextView) row.findViewById(R.id.dist);
holder.valuta = (TextView) row.findViewById(R.id.valuta);
holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
holder.edit = (EditText) row.findViewById(R.id.editText);
holder.spinner = (Spinner) row.findViewById(R.id.simpleSpinner);
holder.edit2 = (EditText) row.findViewById(R.id.editText2);
// holder.iterator = (Button) row.findViewById(R.id.iterator);
// Button iterator=(Button) row.findViewById(R.id.iterator);
// iterator.setOnClickListener(this.PlanetHolder);
holder.caratteristica=(TextView) row.findViewById(R.id.caratteristica);
holder.imageView = (ImageView) row.findViewById(R.id.ivImage);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
holder.edit.setVisibility(View.GONE);
// holder.id = (TextView) row.findViewById(R.id.id);
row.setTag(holder);
} else {
holder = (PlanetHolder) row.getTag();
}
final Planet p = planetList.get(position);
System.out.println("stampato P" + p);
holder.imageView.setImageResource(imageId[position]);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.valuta.setText("" + p.getValuta());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.caratteristica.setText(p.getCaratteristica());
holder.edit.setEnabled(false);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
boolean isChecked = states.getBoolean("holder.chkBox" + holder.planetName.getText().toString(), false);
System.out.println(isChecked);
if (isChecked) {
// holder.chkBox.setChecked(true);
holder.edit.setVisibility(View.VISIBLE);
holder.edit.setEnabled(true);
holder.spinner.setVisibility(View.VISIBLE);
holder.edit2.setVisibility(View.VISIBLE);
SharedPreferences statess = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
String string = statess.getString("finalHolder.edit" + holder.planetName.getText().toString(), holder.edit.getText().toString().trim());
holder.edit.setText(string);
} else {
holder.chkBox.setChecked(false);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.spinner.setVisibility(View.GONE);
holder.edit2.setVisibility(View.GONE);
}
final PlanetHolder finalHolder2 = holder;
holder.chkBox.setOnCheckedChangeListener(PlanetAdapter.this);
// final BirraHolder finalHolder = birraHolder;
final PlanetHolder finalHolder = holder;
Planet z=getItem(position);
System.out.println(z);
final PlanetHolder finalHolder1 = holder;
final PlanetHolder finalHolder3 = holder;
holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (finalHolder.chkBox.isChecked()) {
finalHolder.edit.setVisibility(View.VISIBLE);
finalHolder.edit.setEnabled(true);
finalHolder.spinner.setVisibility(View.VISIBLE);
finalHolder.edit2.setVisibility(View.VISIBLE);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(),
R.array.country_arrays,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//finalHolder.spinner.setAdapter(adapter);
finalHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
finalHolder.spinner.setSelection(position);
// selState = finalHolder.spinner.getSelectedItem().toString();
// p.setQuantità(finalHolder.edit.getText().toString().trim());
p.setTipo(finalHolder.spinner.getSelectedItem().toString());
finalHolder.edit2.setText(selState);
// System.out.println(selState);
System.out.println("p.getTipo()"+p.getTipo());
/* Toast.makeText(
getContext(),
"Clicked on Planet: " + selState + "", Toast.LENGTH_SHORT).show();*/
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
editor.commit();
finalHolder.edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
p.setQuantità(finalHolder.edit.getText().toString().trim());
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putString("finalHolder.edit" + finalHolder.planetName.getText().toString(), finalHolder.edit.getText().toString().trim());
editor.commit();
}
});
/* SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox" + finalHolder.planetName.getText().toString(), true);
editor.commit();*/
//Utility.putPizzaItem(p);
//Utility.getPizzaItem(p);
} else {
finalHolder.edit.setVisibility(View.GONE);
finalHolder.edit.setEnabled(false);
finalHolder.edit.setText("");
finalHolder.spinner.setVisibility(View.GONE);
}
}
});
/*holder.chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (finalHolder.chkBox.isChecked()) {
finalHolder.edit.setVisibility(View.VISIBLE);
finalHolder.edit.setEnabled(true);
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putBoolean("holder.chkBox", true);
//pizzaSelected.put(p.getId(), p);
System.out.println(p.getId());
} else {
finalHolder.edit.setVisibility(View.GONE);
finalHolder.edit.setEnabled(false);
finalHolder.edit.setText(null);
pizzaSelected.remove(p.getId());
}
}
});
*/
/* finalHolder.edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
p.setQuantità(finalHolder.edit.getText().toString().trim());
SharedPreferences states = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = states.edit();
editor.putString("finalHolder.edit" + finalHolder.planetName.getText().toString(), finalHolder.edit.getText().toString().trim());
editor.commit();
}
});
*/
// holder.planetName.setText(p.getName());
// holder.distView.setText("" + p.getDistance());
// holder.valuta.setText("" + p.getValuta());
// holder.chkBox.setChecked(p.isSelected());
// holder.chkBox.setTag(p);
// holder.edit.setEnabled(false);
// holder.id.setId(p.getId());
return row;
}
ArrayList<Planet> getBox() {
ArrayList<Planet> box = new ArrayList<Planet>();
for (Planet p : planetList) {
if (p.isSelected())
box.add(p);
}
return box;
}
}
あなたの問題は、本当に多くの明確ではありません。また、関連コードのみを共有する – Shaishav
ボタンをクリックすると、showResultTotale2メソッドが機能します。しかし、私はボタンではないすべてのチェックボックスをクリックすると、このメソッドが動作するようにしたいです – Markus
あなたはいくつのチェックボックスを持っていますか?リスト項目にチェックボックスがあります(アダプタにチェック・チェンジ・リスナーが表示されているため)。 – Shaishav