Java Android项目:选中listView中的坏复选框
我是一名法国学生,为我的错误感到抱歉。 我必须做一个为期6个月的重大项目来验证我的研究。这个项目包括创建一个Android应用程序。 我的应用程序由一个带有自定义适配器(TextView和CheckBox)的listView组成。 我的问题是我想选中一个不在我的listView当前视图中的复选框。例如,如果我想选中列表底部的复选框,而我在列表顶部(在屏幕上不可见)。复选框不是正确的,它在当前视图中随机勾选一个复选框 以下是查看代码:Java Android项目:选中listView中的坏复选框,java,android,Java,Android,我是一名法国学生,为我的错误感到抱歉。 我必须做一个为期6个月的重大项目来验证我的研究。这个项目包括创建一个Android应用程序。 我的应用程序由一个带有自定义适配器(TextView和CheckBox)的listView组成。 我的问题是我想选中一个不在我的listView当前视图中的复选框。例如,如果我想选中列表底部的复选框,而我在列表顶部(在屏幕上不可见)。复选框不是正确的,它在当前视图中随机勾选一个复选框 以下是查看代码: @Override public View
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) selectActivity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.station_list_item, null);
}
//Handle TextView and display string from your list
TextView textViewListNomStation = (TextView)view.findViewById(R.id.textViewListNomStation);
//Log.i(tag, "nom :" +infosStationsCapteurs.cInfosStationArrayList.get(position).getNomStation() );
textViewListNomStation.setText(infosStationsCapteurs.getcInfosStationArrayList().get(position).getNomStation() + " ID : " + infosStationsCapteurs.getcInfosStationArrayList().get(position).getIdStation());
TextView textViewListInfosStation = (TextView)view.findViewById(R.id.textViewListInfosStation);
String formatInfos = "Lat : " + infosStationsCapteurs.getcInfosStationArrayList().get(position).getPositionGPS().getLatitude() + " Lon : " + infosStationsCapteurs.getcInfosStationArrayList().get(position).getPositionGPS().getLongitude();
textViewListInfosStation.setText(formatInfos);
//Handle buttons and add onClickListeners
CheckBox checkBoxStation = (CheckBox) view.findViewById(R.id.checkBoxStation);
checkBoxTab[position] = checkBoxStation;
checkBoxTab[position].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Log.i(tag, "Bouton :" + buttonView.getId() + " Status " + isChecked);
if (isChecked == true)
{
mCheckedState[position]=true;
selectActivity.getTableauCapteurs().addHashSetstationChecked((cInfosStation)getItem(position));
selectActivity.getTableauCapteurs().createTable();
}
if (isChecked == false){
mCheckedState[position]=false;
selectActivity.getTableauCapteurs().deleteHashSetstationChecked((cInfosStation)getItem(position));
selectActivity.getTableauCapteurs().createTable();
}
}
});
checkBoxTab[position].setChecked(mCheckedState[position]);
return view;
}
下面是允许我在listView中选中复选框的代码
public void checkListStation(int id, boolean etat)
{
//Log.i(tag,"CheckListStation : NBR STATION : " + infosStationsCapteurs.getcInfosStationArrayList().size());
for (int i = 0;i<infosStationsCapteurs.getcInfosStationArrayList().size();i++)
{
//Log.i(tag, "CHECK " + infosStationsCapteurs.getcInfosStationArrayList().get(i).getNomStation() + " : "+name);
if (infosStationsCapteurs.getcInfosStationArrayList().get(i).getIdStation()==id)
{
mCheckedState[i]=etat;
if (checkBoxTab[i]!=null)
{
checkBoxTab[i].setChecked(etat);
Log.i(tag, "Checkbox : " + i + " : " + checkBoxTab[i].isChecked() + "taille : " + checkBoxTab.length);
}
}
}
this.notifyDataSetChanged();
}
public void checkListStation(int-id,boolean-etat)
{
//Log.i(标签,“CheckListStation:NBR STATION:”+InfossStationCapturers.getcInfosStationArrayList().size());
对于(inti=0;i你能发布你想做什么的图片吗?不清楚。
但我认为您想要的是一个包含多个项目的列表视图,每个项目都有一个复选框。如果是这样,您可以通过使用带有适配器的自定义列表视图来实现。您可以参考下面的caode
public class GeneratedOrderListAdapter extends BaseAdapter {
Context mContext;
ViewHolder holder;
Typeface tf_bold, tf_regular;
ArrayList<GenerateOrder> arrayListgeneratOrder;
public GeneratedOrderListAdapter(Context context, ArrayList<GenerateOrder> arrayListgeneratOrder) {
mContext = context;
this.arrayListgeneratOrder = new ArrayList<>();
this.arrayListgeneratOrder = arrayListgeneratOrder;
this.tf_bold = Typeface.createFromAsset(mContext.getAssets(), "opensans_semibold.ttf");
this.tf_regular = Typeface.createFromAsset(mContext.getAssets(), "opensans_regular.ttf");
}
@Override
public int getCount() {
return arrayListgeneratOrder.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHolder {
TextView mOrderNO;
TextView mCustomerName;
TextView mStatus;
TextView mTotalAmount;
TextView mtextOrderNO;
TextView mtextCustomerName;
TextView mtextStatus;
TextView mtextTotalAmount;
RelativeLayout relback;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = mInflater.inflate(R.layout.order_list, null);
holder = new ViewHolder();
holder.mtextStatus = (TextView) view.findViewById(R.id.txt_t_status);
holder.mtextOrderNO = (TextView) view.findViewById(R.id.txt_title_orderno);
holder.mtextCustomerName = (TextView) view.findViewById(R.id.txt_t_customer);
holder.mtextTotalAmount = (TextView) view.findViewById(R.id.txt_t_amount);
holder.mStatus = (TextView) view.findViewById(R.id.txt_status);
holder.mOrderNO = (TextView) view.findViewById(R.id.txt_orderno);
holder.mCustomerName = (TextView) view.findViewById(R.id.txt_customer);
holder.mTotalAmount = (TextView) view.findViewById(R.id.txt_amount);
holder.relback=(RelativeLayout)view.findViewById(R.id.rel_back);
// setTypeFace();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
try {
holder.mStatus.setText(arrayListgeneratOrder.get(position).getOrderStatus());
holder.mTotalAmount.setText(arrayListgeneratOrder.get(position).getAmount());
holder.mCustomerName.setText(arrayListgeneratOrder.get(position).getCustomerName());
holder.mOrderNO.setText(arrayListgeneratOrder.get(position).getOrderNo());
if(position%2==0){
holder.relback.setBackgroundColor(Color.parseColor("#f4fff5"));
}else{
holder.relback.setBackgroundColor(Color.parseColor("#ffffff"));
}
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
private void setTypeFace() {
holder.mStatus.setTypeface(tf_regular);
holder.mTotalAmount.setTypeface(tf_regular);
holder.mCustomerName.setTypeface(tf_regular);
holder.mOrderNO.setTypeface(tf_regular);
holder.mtextStatus.setTypeface(tf_regular);
holder.mtextOrderNO.setTypeface(tf_regular);
holder.mtextCustomerName.setTypeface(tf_regular);
holder.mtextTotalAmount.setTypeface(tf_regular);
}
}
生成的公共类OrderListAdapter扩展了BaseAdapter{
语境;
视窗座;
字体tf_粗体,tf_规则;
ArrayList ArrayList生成顺序;
公共生成的OrderListAdapter(上下文上下文,ArrayList ArrayList GenerateOrder){
mContext=上下文;
this.arraylistgenerateOrder=新的ArrayList();
this.ArrayListGenerateOrder=ArrayListGenerateOrder;
this.tf_bold=Typeface.createFromAsset(mContext.getAssets(),“opensans_semibold.ttf”);
this.tf_regular=Typeface.createFromAsset(mContext.getAssets(),“opensans_regular.ttf”);
}
@凌驾
public int getCount(){
返回ArrayListGenerateOrder.size();
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共长getItemId(int i){
返回0;
}
静态类视窗夹{
TextView-mOrderNO;
TextView MCCustomerName;
文本视图mStatus;
文本视图mTotalAmount;
TextView mtexorderno;
TextView mtextCustomerName;
text查看mtextStatus;
text查看mtextTotalAmount;
相对延迟回复;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组视图组){
LayoutInflater mInflater=(LayoutInflater)mContext.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
如果(视图==null){
view=mInflater.充气(R.layout.order\u列表,空);
holder=新的ViewHolder();
holder.mtextStatus=(TextView)view.findViewById(R.id.txt\u t\u status);
holder.mtexorderno=(TextView)view.findViewById(R.id.txt\u title\u orderno);
holder.mtextCustomerName=(TextView)view.findViewById(R.id.txt\u t\u customer);
holder.mtextTotalAmount=(TextView)view.findViewById(R.id.txt\u t\u amount);
holder.mStatus=(TextView)view.findViewById(R.id.txt_状态);
holder.mOrderNO=(TextView)view.findViewById(R.id.txt_orderno);
holder.mccustomername=(TextView)view.findViewById(R.id.txt\u customer);
holder.mTotalAmount=(TextView)view.findViewById(R.id.txt\u amount);
holder.relback=(RelativeLayout)view.findViewById(R.id.rel\u back);
//setTypeFace();
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}
试一试{
holder.mStatus.setText(ArrayListGenerateOrder.get(position.getOrderStatus());
holder.mTotalAmount.setText(ArrayListGenerateOrder.get(position.getAmount());
holder.mcCustomerName.setText(ArrayListGenerateOrder.get(position.getCustomerName());
holder.mOrderNO.setText(arraylistgenerateOrder.get(position.getOrderNo());
如果(位置%2==0){
holder.relback.setBackgroundColor(Color.parseColor(“#f4fff5”);
}否则{
holder.relback.setBackgroundColor(Color.parseColor(“#ffffff”));
}
}捕获(例外e){
e、 printStackTrace();
}
返回视图;
}
私有void setTypeFace(){
holder.mStatus.setTypeface(tf_-rulary);
holder.mTotalAmount.setTypeface(tf_常规字体);
holder.mcCustomerName.setTypeface(tf_常规);
固定字体(tf_常规);
holder.mtextStatus.setTypeface(tf_常规);
固定字体(tf_常规);
holder.mtextCustomerName.setTypeface(tf_常规);
holder.mtextTotalAmount.setTypeface(tf_常规);
}
}
在模型类中添加CheckFlag参数并初始化为零
//Handle buttons and add onClickListeners
CheckBox checkBoxStation = (CheckBox) view.findViewById(R.id.checkBoxStation);
int check_flag=infosStationsCapteurs.getcInfosStationArrayList().get(position).getPositionGPS().getCheckFlag();
if(check_flag==1)
{
checkBoxStation.setChecked(true);
}
else
{
checkBoxStation.setChecked(false);
}
checkBoxStation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Log.i(tag, "Bouton :" + buttonView.getId() + " Status " + isChecked);
if (isChecked)
{
infosStationsCapteurs.getcInfosStationArrayList().get(position).getPositionGPS().setCheckFlag(1);
}
else{
{
infosStationsCapteurs.getcInfosStationArrayList().get(position).getPositionGPS().setCheckFlag(0);
}
notifyDataSetChanged();
}
});
使用下面的代码
这是adater类
public class AttendanceAdapter extends BaseAdapter{
Context ctx;
LayoutInflater lInflater;
ArrayList<Student> objects;
public AttendanceAdapter(Context context, ArrayList<Student> students) {
ctx = context;
objects = students;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.atndnc_items, parent, false);
}
Student s = getStudent(position);
((TextView) view.findViewById(R.id.txtRollno)).setText(s.rollno);
((TextView) view.findViewById(R.id.txtName)).setText(s.fname+" "+s.mname+" "+s.lname);
final CheckBox cbAtnd = (CheckBox) view.findViewById(R.id.chckTick);
cbAtnd.setOnCheckedChangeListener(myCheckChangList);
cbAtnd.setTag(position);
cbAtnd.setChecked(s.chck);
return view;
}
Student getStudent(int position) {
return ((Student) getItem(position));
}
public ArrayList<Student> getBox() {
ArrayList<Student> box = new ArrayList<Student>();
for (Student s : objects) {
box.add(s);
}
return box;
}
CompoundButton.OnCheckedChangeListener myCheckChangList = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getStudent((Integer) buttonView.getTag()).chck = isChecked;
}
};}
“我的问题是我想勾选一个不在当前视图中的复选框”对不起:“我的问题是我想勾选一个不在我的列表视图的当前视图中的复选框”但是你是如何勾选列表中的不可见项的?你有没有类似“全选”这样的选项?否则你怎么能检查它?我的意思是物理上不可能!物理上按钮是存在的,但它不可见,但它位于我的listView的底部,因此在屏幕上不可见。图像已添加。谢谢谢谢谢谢谢谢,但是如果我不在onClickListeners函数中,我如何检查复选框?例如e如果我想通过另一个函数选中列表中的3个复选框,请使用notifyDataSetChangedsetOnCheckedChangeListener调用notifyDataSetChanged.notifyDataSetChanged刷新列表。刷新时,如果(check_flag==1){checkBoxStation.setChecked(true);}或者{checkBoxStation.setChecked(false);}是call,您的复选框是check我允许自己在主题中添加将选中我的复选框的函数的代码
public void getValue(){
for (Student s :boxAdapter.getBox()) {
attendance.attendancelist.add(new Attendance( s.rollno,s.fname+" "+s.mname+" "+s.lname,s.chck));
}
}