我有一个自定义listview
复选框,它还检查了所有功能。所以我想要点击按钮上的所有选中项目,但是当我选中任何复选框时,我只获得listview
的最后一个可见项目,如果我选中所有复选框并单击按钮,那么我会多次获得最后一个可见项目的值。我不知道为什么会发生这种情况,请帮助我。提前致谢。
我的代码是:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.list);
status=(Spinner) findViewById(R.id.status);
error=(TextView) findViewById(R.id.error);
status=(Spinner) findViewById(R.id.status);
approve=(Button) findViewById(R.id.approve);
reject=(Button) findViewById(R.id.reject);
if (isNotAdded) {
final View headerView = getLayoutInflater().inflate(R.layout.custom_list_view_header,listView, false);
cb1 = (CheckBox) headerView.findViewById(R.id.cb1);
listAdapter=new ListAdapter(this, R.layout.custom_list_view);
Bundle b = getIntent().getExtras();
//bundle_id = b.getString("string");
bundle_id = "T0001";
status_data=getResources().getStringArray(R.array.status);
ArrayAdapter<String> adapter=new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,status_data);
status.setAdapter(adapter);
status.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
status_changed=arg0.getItemAtPosition(arg2).toString();
if(status_changed.equals("Approved"))
{
LeaveApprovedStatus bkg=new LeaveApprovedStatus(MainActivity.this);
bkg.execute(bundle_id);
}
else if(status_changed.equals("Rejected"))
{
LeaveRejectedStatus bkg=new LeaveRejectedStatus(MainActivity.this);
bkg.execute(bundle_id);
}
else
{
LeavePendingStatus bkg=new LeavePendingStatus(MainActivity.this);
bkg.execute(bundle_id);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
});
cb1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/*
* Set all the checkbox to True/False
*/
for (int i = 0; i < count; i++) {
mChecked.put(i, cb1.isChecked());
}
/*
* Update View
*/
listAdapter.notifyDataSetChanged();
}
});
listView.addHeaderView(headerView);
isNotAdded = false;
}
listView.setAdapter(listAdapter);
approve.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(status_changed.equals("Approved"))
{
Toast.makeText(getApplicationContext(), "Already approved", Toast.LENGTH_LONG).show();
}
else
{
String data=getData("Yes");
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
}
}
});
}
public final String getData(String decision){
String json_string = null;
if(status_changed.equals("Pending"))
{
empcode_list.clear();
from.clear();
to.clear();
lv_type.clear();
nod.clear();
status_list.clear();
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
if (mChecked.get(i)) {
count=count+1;
empcode_list.add((String) listAdapter.detailsHolder.empcode.getText().toString());
from.add((String) listAdapter.detailsHolder.from.getText());
to.add((String) listAdapter.detailsHolder.to.getText());
lv_type.add((String) listAdapter.detailsHolder.lv_type.getText());
nod.add((String) listAdapter.detailsHolder.nod.getText());
}
}
status_list.add(decision);
JSONArray jArr1= new JSONArray();
for(String data:empcode_list)
{
jArr1.put(data);
}
JSONArray jArr2= new JSONArray();
for(String data:from)
{
jArr2.put(data);
}
JSONArray jArr3= new JSONArray();
for(String data:to)
{
jArr3.put(data);
}
JSONArray jArr4= new JSONArray();
for(String data:lv_type)
{
jArr4.put(data);
}
JSONArray jArr5= new JSONArray();
for(String data:status_list)
{
jArr5.put(data);
}
JSONArray jArr6= new JSONArray();
for(String data:nod)
{
jArr6.put(data);
}
JSONArray jArr7= new JSONArray();
for(String data:remark)
{
jArr7.put(data);
}
JSONArray jArraySet = new JSONArray();
jArraySet.put(jArr1);
jArraySet.put(jArr2);
jArraySet.put(jArr3);
jArraySet.put(jArr4);
jArraySet.put(jArr5);
jArraySet.put(jArr6);
jArraySet.put(jArr7);
json_string=String.valueOf(jArraySet);
Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
}
else {
}
return json_string;
}
//adapter class
public class ListAdapter extends ArrayAdapter<Object> {
Context c;
DetailsHolder detailsHolder;
public ListAdapter(Context context, int resource) {
super(context, resource);
this.c=context;
}
public void add(Details object) {
super.add(object);
list.add(object);
}
public void clearData() {
list.clear();
}
public int getCount() {
count=list.size();
return count;
}
@Override
public Details getItem(int position) {
return (Details) list.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
row=convertView;
//final DetailsHolder detailsHolder;
//Toast.makeText(c, String.valueOf(list.size()), Toast.LENGTH_LONG).show();
if(row==null)
{
LayoutInflater layoutInflater=(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.custom_list_view, parent, false);
detailsHolder=new DetailsHolder();
detailsHolder.empcode=(TextView) row.findViewById(R.id.empcode1);
detailsHolder.empname=(TextView) row.findViewById(R.id.empname1);
detailsHolder.appdate=(TextView) row.findViewById(R.id.appdate1);
detailsHolder.lv_type=(TextView) row.findViewById(R.id.lv_type1);
detailsHolder.from=(TextView) row.findViewById(R.id.from1);
detailsHolder.to=(TextView) row.findViewById(R.id.to1);
detailsHolder.nod=(TextView) row.findViewById(R.id.nod1);
detailsHolder.reason=(TextView) row.findViewById(R.id.reason1);
detailsHolder.status=(TextView) row.findViewById(R.id.status1);
detailsHolder.cb2=(CheckBox) row.findViewById(R.id.cb2);
detailsHolder.remark=(EditText) row.findViewById(R.id.remark);
row.setTag(detailsHolder);
}
else{
detailsHolder=(DetailsHolder) row.getTag();
}
final Details details=(Details)this.getItem(position);
detailsHolder.empcode.setText(details.getEmpcode());
detailsHolder.empname.setText(details.getEmpname());
detailsHolder.empname.setMovementMethod(new ScrollingMovementMethod());
detailsHolder.appdate.setText(details.getApplyDate());
detailsHolder.lv_type.setText(details.getLeave_type());
detailsHolder.from.setText(details.getFrom());
detailsHolder.to.setText(details.getTo());
detailsHolder.nod.setText(details.getNod());
detailsHolder.reason.setText(details.getReason());
detailsHolder.reason.setMovementMethod(new ScrollingMovementMethod());
detailsHolder.status.setText(details.getStatus());
//detailsHolder.cb2.setChecked(((View) list.get(position)).isSelected());
detailsHolder.cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mChecked.put(position, isChecked); // Saving Checked Position
// Find if all the check boxes are true
if (isAllValuesChecked()) {
MainActivity.cb1.setChecked(isChecked); //set HeaderCheck box to true
}
}
else {
mChecked.delete(position); // Removed UnChecked Position
MainActivity.cb1.setChecked(isChecked); // Remove Checked in Header
}
}
});
detailsHolder.cb2.setChecked((mChecked.get(position) == true ? true : false));
return row;
}
// Find if all values are checked.
protected boolean isAllValuesChecked() {
for (int i = 0; i < count; i++) {
if (!mChecked.get(i)) {
return false;
}
}
return true;
}
class DetailsHolder
{
TextView empcode;
TextView empname;
TextView appdate;
TextView lv_type;
TextView from;
TextView to;
TextView nod;
TextView reason;
TextView status;
EditText remark;
CheckBox cb2;
}
}
}