我想將複選框動態添加到LinearLayout。我已經使用了以下代碼,android動態複選框
private class ListAdapters extends ArrayAdapter<ApplicationBean> {
private ArrayList<ApplicationBean> items;
private int position;
public ListAdapters(Context context, int textViewResourceId,
ArrayList<ApplicationBean> mTitleList) {
super(context, textViewResourceId, mTitleList);
this.items = mTitleList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.applicationlistitem, null);
}
final ApplicationBean o = (ApplicationBean) items.get(position);
if (o != null) {
txtAppName = (TextView) v.findViewById(R.id.app_name);
txtAppName.setText("" + o.getAppName());
launchButton = (Button) v.findViewById(R.id.launch_btn);
launchButton.setTag(position);
launchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final PackageManager pm = mContext.getPackageManager();
Intent LaunchIntent = pm
.getLaunchIntentForPackage(items
.get(Integer.parseInt(v.getTag()
.toString())).getPname());
mContext.startActivity(LaunchIntent);
}
});
rdgPassFail = (RadioGroup) v.findViewById(R.id.status_group);
rdgPassFail.setTag(position);
RadioButton passBtn = (RadioButton) v
.findViewById(R.id.pass_btn);
passBtn.setTag(position);
RadioButton failbtn = (RadioButton) v
.findViewById(R.id.fail_btn);
failbtn.setTag(position);
rdgPassFail
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
ApplicationBean o = (ApplicationBean) items
.get(Integer.parseInt(group.getTag()
.toString()));
switch (checkedId) {
case R.id.fail_btn:
Log.e("Fail button", "Clicked");
o.setFailState(true);
o.setPassState(false);
numOptions = 0;
Log.e("Fail button--1", "Clicked");
break;
case R.id.pass_btn:
Log.e("Pass button", "Clicked");
o.setFailState(false);
o.setPassState(true);
Log.e("Pass button-----1", "Clicked");
break;
}
items.set(Integer.parseInt(group.getTag()
.toString()), o);
}
});
Log.i("checkBoxFlag", "checkBoxFlag not true " + position);
LinearLayout featuresTable = (LinearLayout) v
.findViewById(R.id.failure_reasonslist);
for (int i = 0; i <= 5; i++) {
CheckBox feature1 = new CheckBox(this.getContext());
featuresTable.addView(feature1);
Log.i("Inside for loop", "creating check box " + position);
}
checkBoxFlag = true;
txtDescription = (EditText) v
.findViewById(R.id.description_text);
txtDescription.setTag(position);
if (txtDescription.isFocused()) {
InputMethodManager inputManager = (InputMethodManager) mContext
.getSystemService(INPUT_METHOD_SERVICE);
inputManager.restartInput(txtDescription);
}
txtDescription
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final EditText Caption = (EditText) v;
o.setDescription(Caption.getText()
.toString());
}
}
});
uninstallButton = (Button) v.findViewById(R.id.uninstall_btn);
uninstallButton.setTag(position);
// uninstallButton.setVisibility(View.INVISIBLE);
o.setUninstallVisible(false);
uninstallButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri packageUri = Uri.parse("package:"
+ items.get(
Integer.parseInt(v.getTag().toString()))
.getPname());
Intent uninstallIntent = new Intent(
Intent.ACTION_DELETE, packageUri);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(uninstallIntent);
mTitleList.remove(items.get((Integer) v.getTag()));
mListView.setAdapter(new ListAdapters(mContext,
R.id.app_name, mTitleList));
((BaseAdapter) mListView.getAdapter())
.notifyDataSetChanged();
isUninstallclicked = true;
}
});
submitButton = (Button) v.findViewById(R.id.submit_btn);
submitButton.setTag(txtDescription);
submitButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText tv = (EditText) v.getTag(); // get edittext
// object
txtDescription = tv;
if (txtTesterName.getText().toString().equals("")) {
showDialog("Please enter the name of tester",
mContext);
} else if (numOptions == 0) {
showDialog("Please select failure reason", mContext);
} else if (tv.getText().toString().equals("")) {
showDialog("Please enter the description", mContext);
} else if (!isNetworkAvailable()) {
showDialog(
"No network connection.Report won't be submitted",
mContext);
} else {
if (!o.isUninstallVisible()) {
uninstallButton.setVisibility(View.VISIBLE);
o.setUninstallVisible(true);
mListView.invalidate();
}
PostRequest p = new PostRequest(Integer.parseInt(tv
.getTag().toString()));
p.execute();
}
}
});
}
return v;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return items.size();
}
}
根據代碼,它應該是六個複選框應該在視圖中。但是當我檢查了六個以上的複選框時。請在下面找到日誌(從logcat複製)。日誌也來了兩次。我在列表視圖中有一些其他控件。這些是在佈局xml中定義的。但是我不能在設計時限制這些複選框的數量。所以我正在嘗試加載動態。有人可以幫忙嗎?
10:40:23.163: I/Inside for loop(440): creating check box 0
10:40:23.223: I/Inside for loop(440): creating check box 0
10:40:23.223: I/Inside for loop(440): creating check box 0
10:40:23.273: I/Inside for loop(440): creating check box 0
10:40:23.273: I/Inside for loop(440): creating check box 0
10:40:23.303: I/Inside for loop(440): creating check box 0
10:40:23.393: I/c
10:40:23.393: I/Inside for loop(440): creating check box 0
10:40:23.423: I/Inside for loop(440): creating check box 0
10:40:23.423: I/Inside for loop(440): creating check box 0
10:40:23.503: I/Inside for loop(440): creating check box 0
10:40:23.503: I/Inside for loop(440): creating check box 0
10:40:23.553: I/Inside for loop(440): creating check box 0
這不是一個完整的源代碼。看起來您發佈的部分的方法會執行兩次。檢查你的代碼。 –
檢查此。這是肯定幫助你:http://dj-android.blogspot.in/2012/04/milti-selection-listview-android-with.html –
檢查出我的答案... –