我在滚动视图里面有5个列表视图..列表视图根据具体情况显示..主要问题是getview方法调用两次..例如sizeof数据1表示,getview方法调用2次..所以附加view get created..i设置listview的高度来填充父级而不是work ..i给我的adaper类和视图
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.e("Count++++",getCount()+"");
// View superView = super.getView(position, convertView, parent);
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.checkbox_adapter, null);
final ViewHolder_check holder = new ViewHolder_check();
System.out.println(boxx+":::");
if(boxx.equalsIgnoreCase("checkbox")) {
// String customName = getItem(position);
holder.checkBox = (CheckBox) v.findViewById(R.id.custom_checkbox);;
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.set=holder.check_box_txt.getText().toString();
holder.checkBox.setVisibility(View.VISIBLE);
if(!ProductDetailView.checkboxView.contains(holder.checkBox))
ProductDetailView.checkboxView.add(holder.checkBox);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String response;
JSONObject obj=new JSONObject();
if(holder.checkBox.isChecked())
{
arrayset.add(ProductDetailView.checkboxop.get(position).getOptionTypeId());
arrayset_option.add(ProductDetailView.checkboxop.get(position).getOptionId());
}
else
{
arrayset_option.remove(ProductDetailView.checkboxop.get(position).getOptionId());
arrayset.remove(ProductDetailView.checkboxop.get(position).getOptionTypeId());
}
Log.e("arraysetption",arrayset_option+"");
Log.e("arrayset",arrayset+"");
ArrayList<String> vauecontains=new ArrayList<String>();
ProductDetailView.mainvalues=new JSONArray();
for(int i=0;i<arrayset_option.size();i++)
{
String check=arrayset_option.get(i).toString();
JSONArray optinidvalues=new JSONArray();
if(vauecontains.contains(check))
{
}
else
{
for(int j=0;j<arrayset_option.size();j++)
{
if(check.equalsIgnoreCase(arrayset_option.get(j).toString()))
{
Log.e("true", "true"+check);
vauecontains.add(check);
optinidvalues.put(arrayset.get(j).toString());
// break;
}
}
try {
JSONObject obj1=new JSONObject();
obj1.put(check,optinidvalues);
ProductDetailView.mainvalues.put(obj1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("mainvalues",ProductDetailView.mainvalues+"");
}
}
//Log.e("json", ProductDetailView.main_json+"sett");
ProductDetailView.checkPosition=position;
}
});
}
if(boxx.equalsIgnoreCase("radio")) {
holder.custom_radiobox = (ImageView) v.findViewById(R.id.custom_radiobox);;
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.custom_radiobox.setVisibility(View.VISIBLE);
holder.custom_radiobox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("check", "inside" + holder.check_box_txt.getText().toString() + "kk" + position);
ProductDetailView.radioPosition = position;
if (imageref != null) {
imageref.setImageResource(R.drawable.option_btn_deselected1);
}
holder.custom_radiobox.setImageResource(R.drawable.option_btn_selected1);
imageref = holder.custom_radiobox;
}
});
}
if(boxx.equalsIgnoreCase("date")) {
holder.dateView = (TextView) v.findViewById(R.id.datepicker);;
holder.dateView.setVisibility(View.VISIBLE);
holder.dateView.setText("Pick Delivery Date");
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.check_box_txt.setVisibility(View.GONE);
// holder.dateView.setId(editTag);
// editTag++;
if(!ProductDetailView.dateView.contains(holder.dateView)) {
ProductDetailView.dateView.add(holder.dateView);
}
Log.e("errormssg","dateviewcalled:::"+ProductDetailView.dateView+"id"+holder.dateView.getText()+"size"+values.size());
holder.dateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String date= showAlerttime("Date",view);
//holder.dateView.setText(date);
}
});
}
if(boxx.equalsIgnoreCase("time")) {
holder.timepicker = (TextView) v.findViewById(R.id.timepicker);
holder.timepicker.setText("Pick Delivery Time");
holder.timepicker.setVisibility(View.VISIBLE);
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.check_box_txt.setVisibility(View.GONE);
if(!ProductDetailView.timeView.contains(holder.timepicker))
ProductDetailView.timeView.add(holder.timepicker);
holder.timepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String date= showAlerttime("Time",view);
//holder.timepicker.setText(date);
}
});
}
if(boxx.equalsIgnoreCase("datetime")) {
holder.date_timepicker = (TextView) v.findViewById(R.id.date_timepicker);
holder.date_timepicker.setText("Pick Delivery Date and Time");
holder.date_timepicker.setVisibility(View.VISIBLE);
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.check_box_txt.setVisibility(View.GONE);
if(!ProductDetailView.datetimeView.contains(holder.date_timepicker))
ProductDetailView.datetimeView.add(holder.date_timepicker);
holder.date_timepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String date= showAlerttime("Date and Time",view);
//holder.date_timepicker.setText(date);
}
});
}
if(boxx.equalsIgnoreCase("text")) {
holder.customntext = (EditText) v.findViewById(R.id.customntext);
//holder.customntext.setText(");
holder.customntext.setVisibility(View.VISIBLE);
holder.check_box_txt=(TextView) v.findViewById(R.id.check_box_txt);
holder.check_box_txt.setVisibility(View.GONE);
holder.customntext.setId(editTag);
if(!ProductDetailView.editText.contains(holder.customntext))
ProductDetailView.editText.add(holder.customntext);
editTag++;
}
v.setTag(holder);
}
System.out.println(boxx+":::");
if(boxx.equalsIgnoreCase("checkbox")) {
String customName = getItem(position);
ViewHolder_check holder = (ViewHolder_check) v.getTag();
holder.check_box_txt.setText(customName);
}
if(boxx.equalsIgnoreCase("radio")) {
String customName = getItem(position);
ViewHolder_check holder = (ViewHolder_check) v.getTag();
holder.check_box_txt.setText(customName);
}
if(boxx.equalsIgnoreCase("text")) {
ProductDetailView.text_values=new JSONArray();
final ViewHolder_check holder = (ViewHolder_check) v.getTag();
Log.e("sign", "edittextgetcalled");
}
return v;
}
这就是我设置适配器的方式
if(isDatecustom)
{
date_list.setVisibility(View.VISIBLE);
RelativeLayout date_list_lay = (RelativeLayout) findViewById(R.id.date_list_lay);
date_list_lay.setVisibility(View.VISIBLE);
CustomCheckboxAdapter checkboxAdapter = new CustomCheckboxAdapter(ProductDetailView.this, datevalues,0,"date");
date_list.setAdapter(checkboxAdapter);
setListViewHeightBasedOnChildren(date_list);
}
xml页面中的
<RelativeLayout
android:id="@+id/drop_down_list_lay"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:layout_marginTop="20dp"
android:gravity="center" >
<ListView
android:id="@+id/drop_down_list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:visibility="gone" />
</RelativeLayout>
@SuppressLint("NewApi")
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter myListAdapter = listView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, listView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
WindowManager wm = (WindowManager) baseactivity.this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
int listViewWidth = screenWidth - 10 - 10;
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth,
MeasureSpec.AT_MOST);
listItem.measure(widthSpec, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (myListAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}