gpt4 book ai didi

android - 如何遍历android中 ListView 每一行中的每个复选框?

转载 作者:行者123 更新时间:2023-12-02 10:43:16 25 4
gpt4 key购买 nike

我有一个应用程序,在 ListView 的每一行中包含三个 TextView 和一个复选框。我希望通过单击按钮,我将能够获取每个复选框的状态以及与 (isChecked) 复选框对应的行被删除。还有一件事是我的复选框被硬编码在 xml 文件中。我搜索了很多,但找不到任何具体内容。提前谢谢。这是我的代码...

  public class recentcalllistultimate extends ListActivity implements OnClickListener {
CheckBox cb;
Button edit,done;

ImageButton contacts;
ListView lv;
ListView lvultimate;
listviewadapterultimate lvar;


int[] uniqueid;
String[] names;
String[] types;
;
RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

LayoutParams params=newRelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater layoutInflater = getLayoutInflater();
mainLayout.addView(layoutInflater.inflate(R.layout.listviewonly, null));
mainLayout.addView(layoutInflater.inflate(R.layout.allbuttons, null));
this.addContentView(mainLayout, params);

cb = (CheckBox) findViewById(R.id.checkboxdelete);

getContacts();

lv = (ListView) findViewById(android.R.id.list);
lvar = new listviewadapterultimate(this, names, types,uniqueid);
lv.setAdapter(lvar);

contacts = (ImageButton) findViewById(R.id.button_keypad);
contacts.setOnClickListener(this);

edit = (Button) findViewById(R.id.editbutton);
done=(Button)findViewById(R.id.donebutton);

done.setOnClickListener(new View.OnClickListener() {



------>>> public void onClick(View v) {
// TODO Auto-generated method stub
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, booleanisChecked) {
// TODO Auto-generated method stub

//WHAT TO DO HERE....

}

}
});
}
------>>> });


edit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

AddDialog ada=new AddDialog(recentcalllistultimate.this);
ada.show();
}
});

}// on create

public void getContacts() {

ContentResolver cr = getContentResolver();

Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI, null,
null, null, null);

if (cur.getCount() > 0) {
int i = 0;
int foo = 0;
names = new String[cur.getCount()];
types = new String[cur.getCount()];
duration = new long[cur.getCount()];
uniqueid = new int[cur.getCount()];

int n = cur.getColumnIndex(CallLog.Calls._ID);
int k = cur.getColumnIndex(CallLog.Calls.CACHED_NAME);
int y = cur.getColumnIndex(CallLog.Calls.NUMBER);
int z = cur.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE);


while (cur.moveToNext()) {

uniqueid[foo] = cur.getInt(n);

String str = cur.getString(k);

if (str == null) {
names[foo] = cur.getString(y);
}// if
else {
names[foo] = str;
}

int temp = cur.getInt(z);
switch (temp) {
case 0:
types[foo] = "unknown";
break;
case 1:
types[foo] = "home";
break;
case 2:
types[foo] = "mobile";
break;
case 3:
types[foo] = "work";
break;
}// switch

long doo = cur.getInt(d);
duration[foo] = doo;

foo++;

} // while
}// if

}//getcontacts

public void onClick(View v) {
// TODO Auto-generated method stub
if(v==contacts){
Intent intent = new Intent();
intent.setClassName("com.a.Activities",
"com.a.Activities.DialPad");
startActivity(intent);
finish();
}
}

}// class
.................................

public class listviewadapterultimate extends BaseAdapter {
viewHolder holder;

Activity context;
String[] names;
String[] types;
String[] duration;
int[] uniqueid;


public listviewadapterultimate(Activity context, String[] names,
String[] types, int[] uniqueid2 ) {
this.context = context;
this.names = names;
this.types = types;

uniqueid=uniqueid2;

}




public int getCount() {
// TODO Auto-generated method stub
return names.length;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public class viewHolder {
TextView top;
TextView bottom;

TextView down;


}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub



if (convertView == null) {
holder = new viewHolder();
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.recenttextviewonlyultimate, null);
holder.top = (TextView) convertView.findViewById(R.id.toptext_u);
holder.bottom = (TextView) convertView
.findViewById(R.id.bottomtext_u);

holder.down = (TextView) convertView.findViewById(R.id.recentuniqueid_u);

convertView.setTag(holder);

} else {
holder = (viewHolder) convertView.getTag();
//holder.cb.setVisibility(View.VISIBLE);
}

holder.top.setText(names[position]);
holder.bottom.setText(types[position]);

holder.down.setText("" + uniqueid[position]);


return convertView;
}

}
................

最佳答案

试试这个:

在 getView(...) 方法中...

 final CheckBox lChk = ((CheckBox) pConvertView.findViewById(R.id.myChkBoxID)); 
private List<lisInfo> m_lisInfo = new ArrayList<lisInfo>();

lChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Populate the listInfo with check box status
m_lisInfo.get(lPosition).setChkBoxStatus((isChecked));
}
});


public class lisInfo{
private boolean chkBoxStatus;

public boolean isChkBoxStatus() {
return chkBoxStatus;
}

public void setChkBoxStatus(boolean chkBoxStatus) {
this.chkBoxStatus = chkBoxStatus;
}
}

现在迭代listInfo,只要需要就可以获取 ListView 中的复选框状态

关于android - 如何遍历android中 ListView 每一行中的每个复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8178322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com