你好亲爱的Stackers,
我有一点问题,希望你能帮我解决这个问题。让我们切入追逐。
我的代码应该怎么做? - >我的代码应该设置一个带CheckBoxes的ListView,然后我应该能够检查框,然后计算按下按钮后检查了多少个框。 然后我应该将对象传递给下一个Activity,但这不应该是这个问题的一部分,我只是将其添加到上下文中,它可能对解决方案很重要。
我的代码没有做什么? - >我的代码不计算方框。 listView.getCheckedItemIds()。length)); line只返回0。
重新阅读ListView,List,Adapter,View等文档并尝试一些不同的aproaches(和教程,一些frome stackoverflow)我不知道如何处理这个或从哪里开始
这是守则 -------------------->主类< -----------------
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
int checkCounter = 0;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
private Button mButton;
private TextView mCounter;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this, getModel());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
mCounter = (TextView) findViewById(R.id.counter_text);
mButton = (Button) findViewById(R.id.count_button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = 0;
int size = list.size();
for (int i=0; i<size; i++){
if (list.get(i).isSelected()){
count++;
}
}
mCounter.setText(String.format("%d", count));
}
});
}
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString() + " " + isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if (checkbox.isChecked()) {
return "is checked";
} else {
return "is not checked";
}
}
private List<Model> getModel() {
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
list.add(new Model("Linux"));
list.add(new Model("Windows7"));
list.add(new Model("Suse"));
list.add(new Model("Eclipse"));
list.add(new Model("Ubuntu"));
list.add(new Model("Solaris"));
list.add(new Model("Android"));
list.add(new Model("iPhone"));
list.add(new Model("Java"));
list.add(new Model(".Net"));
list.add(new Model("PHP"));
return list;
}
}
--------------&GT;适配器类&lt; -------------------------
package com.example.christian.listviewwithcheckbox_model;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
} }
-------------------&GT;模型类&lt; -----------------------
package com.example.christian.listviewwithcheckbox_model;
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
嗯,这是我的问题,我很抱歉在最后一个之后很快打开一个帖子,但此时我遇到了一些麻烦,需要一个解决方案。 提前谢谢你,祝你有个愉快的一天。 伊欧墨
编辑:Hoang Nguyen和Hardy解决了这个问题,非常感谢你 上面的代码已经更新,现在显示了工作代码。
答案 0 :(得分:4)
您使用自定义复选框,因此无法使用方法getCheckedItemIds()
,请参阅doc和ListView Mode
对于您的问题,可以轻松更改按钮点击方法,如下所示:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = 0;
int size = list.size();
for (int i=0; i<size; i++){
if (list.get(i).isSelected()){
count++;
}
}
mCounter.setText(String.format("%d", count));
}
});
答案 1 :(得分:0)
放下以下的行
(\s(AUS)(T|RALIA)*)(\sPTY)?(\s(LTD|LIMITED))?
上面
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
所以你的代码最终会是这样的: -
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});