我正在尝试在ListView中显示其子项后面的一些项目。为此,我已将Collection(ArrayList)传递给ListView Adapter类以显示每个项目。对于子项,我必须借助每个位置Id从SQLite数据库中获取数据,并为每个子项创建动态视图。幸运的是它显示所有项目和子项目,但多次。此外,当我点击一些动态视图加载后,列表再次创建相同的视图3/4次。
适配器类:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder mViewHolder;
LayoutInflater mInflater;
if(convertView == null){
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.customincidentquestion, null);
mViewHolder.customLayout = (LinearLayout)convertView.findViewById(R.id.linearLayoutCustomIncidentQuestionList);
mViewHolder.question = (TextView) convertView.findViewById(R.id.textViewQuestion);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder)convertView.getTag();
}
ArrayList<IncidentOptionHolder> mOptionCollection = new ArrayList<IncidentOptionHolder>();
ArrayList<String> optionText = new ArrayList<String>();
ArrayList<String> orderedId = new ArrayList<String>();
Log.v("POSITION", String.valueOf(position));
IncidentQuestionHolder mHolder = mQuestionData.get(position);
String mServerId = mQuestionData.get(position).getId();
Log.e("mServerId", mQuestionData.get(position).getId());
String mQText = mHolder.getqText();
Log.e("mQText", mQText);
String mType = mHolder.getType();
String mOrderId = mHolder.getOrderId();
String mIsMandatory = mHolder.getIsMandatory();
String mIsOption = mHolder.getIsOption();
Log.v("Equality", "Not Equal");
IncidentOption mOptions = new IncidentOption(mContext);
mOptions.open();
mOptionCollection = mOptions.getOptions(mServerId);
mOptions.close();
//if(mViewHolder.customLayout.getChildAt(0) == null){
mViewHolder.question.setText(mQText);
for (int i = 0; i < mOptionCollection.size(); i++) {
//optionText.add(mOptionCollection.get(i).getOptionText());
//orderedId.add(mOptionCollection.get(i).getOrderId());
Log.e("mOptionCollection.get(i).getOptionText()", mOptionCollection.get(i).getOptionText());
if (mType.equals("Cb")) {
Log.v("TYPE", "Check Box");
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setPadding(10, 10, 10, 10);
CheckBox product = new CheckBox(mContext);
product.setText(mOptionCollection.get(i).getOptionText());
product.setGravity(Gravity.CENTER_VERTICAL);
product.setBackgroundResource(R.drawable.bg_designs);
product.setPadding(50, 10, 10, 30);
ll.addView(product);
mViewHolder.customLayout.addView(ll);
}
else if (mType.equals("Ta")) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setPadding(10, 10, 10, 10);
Log.v("TYPE", "Text Box");
LinearLayout lll = new LinearLayout(mContext);
lll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(mContext);
tv.setText(mOptionCollection.get(i).getOptionText());
lll.addView(tv);
EditText ta = new EditText(mContext);
ta.setPadding(10, 10, 10, 10);
ta.setBackgroundResource(R.drawable.bg_designs);
lll.addView(ta);
ll.addView(lll);
mViewHolder.customLayout.addView(ll);
}
else if (mType.equals("Rb")) {
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setPadding(10, 10, 10, 10);
Log.v("TYPE", "Radio Button");
RadioButton rb = new RadioButton(mContext);
rb.setText(mOptionCollection.get(i).getOptionText());
rb.setGravity(Gravity.CENTER_VERTICAL);
rb.setPadding(50, 10, 10, 30);
rb.setBackgroundResource(R.drawable.bg_designs);
ll.addView(rb);
mViewHolder.customLayout.addView(ll);
}
else{
Log.v("TYPE", "Nothing");
}
}
return convertView;
}
public static class ViewHolder {
public LinearLayout customLayout = null;
public TextView question;
}
对于子项目,我不需要使用可扩展列表。
可以建议我在哪里犯了错误,迫使ListView多次加载相同的项目。