列表项重复的安卓定制列表视图 [英] List item repeating in android customized listview
本文介绍了列表项重复的安卓定制列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的自定义列表视图中的项目是项目repeating.position是相同的所有项目。 code低于
ListAdapter.java -
公共类ListAdapter扩展了BaseAdapter {
私人列表<字符串> MNAME;
私人列表<可绘制>米康;
私人语境mContext;
公共ListAdapter(上下文mContext,列表和LT;字符串>名称,目录和LT;可绘制>图标){
this.mContext = mContext;
this.mName =名称;
this.mIcon =图标;
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回mName.size();
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(最终诠释的立场,观点V,ViewGroup中父){
查看mLayout;
TextView的多行文字;
ImageView的mImage;
复选框mCheckBox;
如果(V == NULL){
LayoutInflater充气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayout =新景(mContext);
mLayout =(的LinearLayout)inflater.inflate(R.layout.list_menu,NULL);
多行文字=(TextView中)mLayout.findViewById(R.id.Name);
mImage =(ImageView的)mLayout.findViewById(R.id.Icon);
mCheckBox =(复选框)mLayout.findViewById(R.id.mCheckbox);
mText.setText(mName.get(位置));
mImage.setImageDrawable(mIcon.get(位置));
mCheckBox.setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton检查,布尔器isChecked){
如果(check.isChecked()){
Toast.makeText(mContext,...+ mName.get(位置)+...+位置,Toast.LENGTH_SHORT).show();
}
}
});
}
其他{
mLayout =(查看)V;
}
返回mLayout;
}
}
解决方案
试试这个,你需要 setTag()
每个convertview。
@覆盖
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
最后ViewHolder mHolder;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.list_menu,NULL);
mHolder =新ViewHolder();
mHolder.mText =(TextView中)convertView.findViewById(R.id.appName);
mHolder.mImage =(ImageView的)convertView.findViewById(R.id.appIcon);
mHolder.mCheckBox =(复选框)convertView.findViewById(R.id.mCheckbox);
convertView.setTag(mHolder);
} 其他 {
mHolder =(ViewHolder)convertView.getTag();
}
返回convertView;
}
私有类ViewHolder {
私人TextView的多行文字;
私人ImageView的mImage;
私人复选框mCheckBox;
}
In my customized list view items are repeating.position of item is same for all item. code is below
ListAdapter.java-
public class ListAdapter extends BaseAdapter{
private List<String> mName;
private List<Drawable> mIcon;
private Context mContext;
public ListAdapter(Context mContext, List<String> Name, List<Drawable> Icon) {
this.mContext=mContext;
this.mName=Name;
this.mIcon=Icon;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mName.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View v, ViewGroup parent) {
View mLayout;
TextView mText;
ImageView mImage;
CheckBox mCheckBox;
if(v==null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayout=new View(mContext);
mLayout=(LinearLayout) inflater.inflate(R.layout.list_menu, null);
mText=(TextView) mLayout.findViewById(R.id.Name);
mImage=(ImageView) mLayout.findViewById(R.id.Icon);
mCheckBox=(CheckBox) mLayout.findViewById(R.id.mCheckbox);
mText.setText(mName.get(position));
mImage.setImageDrawable(mIcon.get(position));
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton check, boolean isChecked) {
if(check.isChecked()){
Toast.makeText(mContext, "..."+mName.get(position)+"..."+position, Toast.LENGTH_SHORT).show();
}
}
});
}
else{
mLayout=(View)v;
}
return mLayout;
}
}
解决方案
try this one, You need to setTag()
for each convertview.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_menu, null);
mHolder = new ViewHolder();
mHolder.mText=(TextView) convertView.findViewById(R.id.appName);
mHolder.mImage=(ImageView) convertView.findViewById(R.id.appIcon);
mHolder.mCheckBox=(CheckBox) convertView.findViewById(R.id.mCheckbox);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder {
private TextView mText;
private ImageView mImage;
private CheckBox mCheckBox;
}
这篇关于列表项重复的安卓定制列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文