问题
Hope everyone's good;
I know this issue was reviewed earlier couple of times but after a long search I still didn't find a solution.
My custom listview duplicates items every 6 item.
Already checked and tried:
1- layout_width and layout_height doesn't contain wrap_content
2- holder = new ListViewItem() is before any initialization of contents
3- There is a "convertView != null"
4- holder.linearLayout.getChild() can't be use in my case because the layout isn't Linear
5- clear()
If anyone can help me this is my codes
getView() of CustomListViewAdapter.java
public View getView(final int position, View convertView, ViewGroup parent) {
ListViewItem item = items.get(position);
ListViewItem holder;
View vi=convertView;
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(vi==null){
vi = inflater.inflate(R.layout.item, null);
holder = new ListViewItem();
holder.cb = (CheckBox) vi.findViewById(R.id.Item1);
holder.sp = (Spinner) vi.findViewById(R.id.Item2);
holder.title = (TextView) vi.findViewById(R.id.Item3);
holder.pricetitle= (TextView) vi.findViewById(R.id.item4);
holder.Descriptiontitle= (TextView) vi.findViewById(R.id.Item5);
vi.setTag(holder);
}else{
holder = (ListViewItem) vi.getTag();
}
holder.title.setText(item.ItemTitle);
holder.pricetitle.setText(item.price+"");
holder.Descriptiontitle.setText(item.Description);
return vi;
}
ListViewItem class
static class ListViewItem{
public String ItemTitle;
public int price;
public String Description;
public TextView title;
public TextView pricetitle;
public TextView Descriptiontitle;
public CheckBox cb;
public Spinner sp;
}
MainActivity
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.starterbottom, null);
ListView myList = (ListView)v.findViewById(R.id.MyListStarter);
List<ListViewItem> items = new ArrayList<ListViewItem>();
for(int i=0;i<10;i++){
items.add(new ListViewItem(){{
ItemTitle = "Starter Title";
Description= "Your description goes here";
price=0;
}});
}
CustomListViewAdapter listadapter = new CustomListViewAdapter(getActivity(), android.R.layout.simple_list_item_1, items);
myList.setAdapter(listadapter);
return v;
}
EDIT
MainmenuActivity.java
public class MainmenuActivity extends SlidingActivity{
Button buttononside;
TextView title;
FragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmenu);
setBehindContentView(R.layout.sidemenu);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(4);
mIndicator = (PageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
getSlidingMenu().setBehindOffset(200);
getSlidingMenu().setMode(SlidingMenu.RIGHT);
getSlidingMenu().setFadeDegree(0.35f);
title = (TextView) findViewById(R.id.Title);
title.setText("Starters");
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
public void onClick(View v) {
getSlidingMenu().toggle();
}
public void changecolor(View v){
buttononside = (Button) findViewById(v.getId());
buttononside.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_DOWN)
buttononside.setBackgroundColor(Color.BLACK);
else
buttononside.setBackgroundResource(R.drawable.buttonshape);
return true;
}
});
}
static class ListViewItem{
public String ItemTitle;
public int price;
public String Description;
public TextView title;
public TextView pricetitle;
public TextView Descriptiontitle;
public CheckBox cb;
public Spinner sp;
}
}
FragmentAdapter.java
public class FragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter{
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getIconResId(int index) {
return 0;
}
@Override
public Fragment getItem(int position)
{
android.support.v4.app.Fragment fragment = new StarterActivity();
switch(position){
case 0:
fragment = new MainActivity();
break;
case 1:
fragment = new SecondActivity();
break;
case 2:
fragment = new ThirdActivity();
break;
case 3:
fragment = new FourthActivity();
break;
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public CharSequence getPageTitle(int position){
String title = "";
switch(position){
case 0:
title = "First";
break;
case 1:
title = "Second";
break;
case 2:
title = "Third";
break;
case 3:
title = "Fourth";
break;
}
return title;
}
}
MainActivity.java
public class MainActivity extends Fragment {
int i=0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.starterbottom, null);
ListView myList = (ListView)v.findViewById(R.id.MyListStarter);
List<ListViewItem> items = new ArrayList<ListViewItem>();
for(i=0;i<10;i++){
items.add(new ListViewItem(){{
ItemTitle = "Title "+i;
Description= i+" Your Own description";
price= i;
}});
}
CustomListViewAdapter listadapter = new CustomListViewAdapter(getActivity(), R.layout.item, items);
myList.setAdapter(listadapter);
return v;
}
}
Thanks in advance, Regards, Chris
回答1:
I see the problem now. And no, there is no duplication here. Carry out these few changes:
In your static class ListViewItem
, add boolean isChecked;
static class ListViewItem{
public String ItemTitle;
public int price;
public String Description;
public TextView title;
public TextView pricetitle;
public TextView Descriptiontitle;
public CheckBox cb;
public Spinner sp;
public boolean isChecked; // <--- added
}
Change the initialization of items
:
items.add(new ListViewItem(){{
ItemTitle = "Starter Title";
Description= "Your description goes here";
price=i;
isChecked = false;
}});
In the getView() method, after holder.Descriptiontitle.setText(item.Description);
, add:
holder.cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
item.isChecked = true;
} else {
item.isChecked = false;
}
}
});
if (item.isChecked) {
holder.cb.setChecked(true);
} else {
holder.cb.setChecked(false);
}
Add the final
keyword to ListViewItem item = items.get(position);
:
final ListViewItem item = items.get(position);
来源:https://stackoverflow.com/questions/17799123/listview-duplicates-item-every-6-times