希望每个人都好;
我知道此问题已经提前几次审核过,但经过长时间的搜索后,我仍然没有找到解决方案。
我的自定义列表视图每6个项目重复一次。
已经检查并尝试过:
1 - layout_width和layout_height不包含wrap_content
2 - holder = new ListViewItem()在内容初始化之前
3 - 有一个“convertView!= null”
4 - holder.linearLayout.getChild()在我的情况下无法使用,因为布局不是线性的
5 - clear()
如果有人可以帮助我,这是我的代码
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;
}
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;
}
修改
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;
}
}
提前致谢, 问候, 克里斯
答案 0 :(得分: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);