方法。setOnCheckedChangeListener(MyFragmentList.this)
以IDE
为红色下划线...我不明白我必须将哪个参数传递给setOnCheckedChangeListener
中的方法Fragment
。
感谢。
适配器代码:
public class PlanetAdapter extends ArrayAdapter<Planet>
{
private List<Planet> planetList;
private Context context;
ArrayList<Planet> objects;
public PlanetAdapter(List<Planet> planetList, Context context) {
super(context, R.layout.single_listview_item, planetList);
this.planetList = planetList;
this.context = context;
}
public class PlanetHolder {
public TextView planetName;
public TextView distView;
public TextView valuta;
public CheckBox chkBox;
public EditText edit;
public String quantità;
}
@Override
public View getView(int position, 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.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
row.setTag(holder);
} else {
holder = (PlanetHolder) row.getTag();
}
final Planet p = planetList.get(position);
holder.chkBox.setOnCheckedChangeListener(MyListFragment.this);
final PlanetHolder finalHolder = holder;
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);
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());
}
});
} else {
finalHolder.edit.setVisibility(View.GONE);
finalHolder.edit.setEnabled(false);
finalHolder.edit.setText(null);
}
}
});
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);
return row;
}
ArrayList<Planet> getBox() {
ArrayList<Planet> box = new ArrayList<Planet>();
for (Planet p : planetList) {
if (p.selected)
box.add(p);
}
return box;
}
}
活动:
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportFragmentManager().beginTransaction().
replace(R.id.fragmentContainer, new MyListFragment()).commit();
}
}
FRAGMENT:
public class MyListFragment extends Fragment implements
android.widget.CompoundButton.OnCheckedChangeListener {
ListView lv;
ArrayList<Planet> planetList;
PlanetAdapter plAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_list2, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showResult(v);
}
});
//return inflater.inflate(R.layout.fragment_list2, container, false);
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, "€"));
planetList.add(new Planet("Diavola", 7,"€"));
planetList.add(new Planet("Bufalina", 5,"€"));
planetList.add(new Planet("Marinara", 5,"€"));
planetList.add(new Planet("Viennese", 4,"€"));
plAdapter = new PlanetAdapter(planetList, getContext());
lv.setAdapter(plAdapter);
}
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 Planet: " + p.getName() + ". State: is "
+ isChecked, Toast.LENGTH_SHORT).show();*/
}
}
public void showResult(View v) {
String result = "Selected Product are :";
int totalAmount=0;
for (Planet p : plAdapter.getBox()) {
if (p.selected){
//System.out.println("pizza selezionata: " + p.name);
//System.out.println("nessuna pizza selezionata: ");
result += "\n" + p.name+" "+p.distance+"€"+"q.tà :"+p.getQuantità();
//if (p.quantità.equals("") && p.quantità.equals(null) ){
//System.out.println("leggo questo record:"+p.name + " " + p.distance + " " + p.quantità );
//System.out.println("leggo questo p.getquatità :"+p.quantità );
//}
//else{
//System.out.println("leggo questo in p.quantità: " + p.getQuantità());
int quantitaInt= Integer.parseInt(p.getQuantità() );
totalAmount+=p.distance * quantitaInt;
//}
}
}
Toast.makeText(getActivity(), result + "\n" + "Total Amount:=" + totalAmount, Toast.LENGTH_LONG).show();
//System.out.println("leggo result e total amount: " + result + " " + totalAmount);
//Integer.toString(totalAmount);
// Intent i = new Intent(getActivity(), TwoFragment.class);
/*i.putExtra("NomeDati1", result);
i.putExtra("NomeDati2", String.valueOf(totalAmount));
startActivity(i);
*/
}
}
编辑:
当我使用@Nathanael
的解决方案时,我收到以下错误答案 0 :(得分:0)
您需要传递CompoundButton.OnCheckedChangeListener
。
holder.chkBox.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Do what you need to do
}
});
Here是CheckedChangeListener
答案 1 :(得分:0)
我认为您应该更改方法,因为当您在适配器中使用复选框时,您的适配器列表项对象中应该有一个布尔值,并且所有检查的选中true和false应该在'getView'中处理并且在check上更改只有项目.setChecked应该更改,请参阅下面的代码,这只是示例代码,您也可以使用viewholder
public class MyAdapter extends BaseAdapter {
private List<MyItem> items;
private LayoutInflater inflater;
public MyAdapter(Context context, List<MyItem> items) {
this.items = items;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public MyItem getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyItem item=getItem(position);
View view=inflater.inflate(R.layout.adapteritem,null);
CheckBox checkBox=(CheckBox)view.findViewById(R.id.chb);
TextView txt=(TextView)view.findViewById(R.id.title);
if(item.isChecked)
{
txt.setText(item.title+" Selected");
}else{
txt.setText(item.title+" Not Selected");
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.isChecked=isChecked;
notifyDataSetChanged();
}
});
return view;
}
public class MyItem {
public boolean isChecked = false;
public String title;
}
}
答案 2 :(得分:0)
片段首先我们需要实现android.widget.CompoundButton.OnCheckedChangeListener看看这个例子
public class TabAdvertiser extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
CheckBox mCbShowPwd;
EditText userName,password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity());
View view = inflater.inflate(R.layout.login, container, false);
userName=(EditText)view.findViewById(R.id.input_email);
password=(EditText)view.findViewById(R.id.input_password);
// get the show/hide password Checkbox
mCbShowPwd = (CheckBox)view.findViewById(R.id.cbShowPwd);
mCbShowPwd.setOnCheckedChangeListener(TabAdvertiser.this);
return view;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
}