4

希望大家都好;

我知道这个问题之前已经审查过几次,但经过长时间的搜索,我仍然没有找到解决方案。

我的自定义列表视图每 6 个项目重复项目。

已经检查并尝试过:

1- layout_width 和 layout_height 不包含 wrap_content

2- holder = new ListViewItem() 在任何内容初始化之前

3-有一个“convertView!= null”

4- holder.linearLayout.getChild() 在我的情况下不能使用,因为布局不是线性的

5-清除()

如果有人可以帮助我,这是我的代码

CustomListViewAdapter.java 的 getView()

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 类

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 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;
}

编辑

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;
}

}

片段适配器.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;

    }
 }

在此先感谢, 问候, 克里斯

4

1 回答 1

5

我现在看到了问题。不,这里没有重复。执行以下几个更改:

在您的静态类ListViewItem中,添加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
}

更改 的初始化items

items.add(new ListViewItem(){{
        ItemTitle = "Starter Title";
        Description= "Your description goes here";
        price=i;
        isChecked = false;
    }});

在 getView() 方法的 之后holder.Descriptiontitle.setText(item.Description);,添加:

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);
}

final关键字添加到ListViewItem item = items.get(position);

final ListViewItem item = items.get(position);
于 2013-07-23T01:23:52.133 回答