在android中滚动三个项目后在ListView中显示重复项目?
我想填写Listview,但只重复所有项目中的imgLike、imgComments、txtLikeUnlike和txtcoments。因为我使用数组来表示imgLike、imgComments、txtLikeUnlike和txtcoments来表示get(fetch)selected位置。我的代码有什么错误,请指导我 我的代码, /**适配器类*/在android中滚动三个项目后在ListView中显示重复项目?,android,listview,Android,Listview,我想填写Listview,但只重复所有项目中的imgLike、imgComments、txtLikeUnlike和txtcoments。因为我使用数组来表示imgLike、imgComments、txtLikeUnlike和txtcoments来表示get(fetch)selected位置。我的代码有什么错误,请指导我 我的代码, /**适配器类*/ public class Adapter1 extends BaseAdapter { ImageView imgImage =
public class Adapter1 extends BaseAdapter {
ImageView imgImage = null,imgUnlike[] = null, imgLike[] = null,imgComments[] = null, imgShare[] = null;
TextView txtId = null,txtLikeUnlike[] = null, txtComments[] = null;
public ArrayList<HashMap<String, String>> arr = null;
Context context = null;
LayoutInflater layoutInflater = null;
HashMap<String, String> getData = new HashMap<String, String>();
public Adapter1(Context context,
ArrayList<HashMap<String, String>> arr) {
this.context = context;
this.arr = arr;
layoutInflater = LayoutInflater.from(context);
this.imgUnlike = new ImageView[arr.size()];
this.imgLike = new ImageView[arr.size()];
this.imgComments = new ImageView[arr.size()];
this.imgShare = new ImageView[arr.size()];
this.txtLikeUnlike = new TextView[arr.size()];
this.txtComments = new TextView[arr.size()];
}
@Override
public int getCount() {
return arr.size();
}
@Override
public Object getItem(int position) {
return arr.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent,
false);
} else {
row = convertView;
}
/** Initialize Widgets */
/** Imageview */
imgImage = (ImageView) row
.findViewById(R.id.imgImage);
imgUnlike[position] = (ImageView) row
.findViewById(R.id.imgUnlike);
imgLike[position] = (ImageView) row
.findViewById(R.id.imgLike);
imgComments[position] = (ImageView) row
.findViewById(R.id.imgComments);
imgShare[position] = (ImageView) row
.findViewById(R.id.imgShare);
/** TextView */
txttId = (TextView) row
.findViewById(R.id.txtId);
txtLikeUnlike[position] = (TextView) row
.findViewById(R.id.txtLikeUnlike);
txtComments[position] = (TextView) row
.findViewById(R.id.txtComments);
getData = arr.get(position);
txtId.setText(getData.get(Fragment1.TAG_ID));
imgUnlike[position]
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgUnlike[position]
.setVisibility(View.INVISIBLE);
imgLike[position]
.setVisibility(View.VISIBLE);
getCurrentPosition = position;
}
});
imgLike[position].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgNewsFeedLike[position].setVisibility(View.INVISIBLE);
imgNewsFeedUnlike[position].setVisibility(View.VISIBLE);
}
});
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),
Detail.class);
startActivity(intent);
}
});
return row;
}
公共类适配器1扩展了BaseAdapter{
ImageView imgImage=null,imgUnlike[]=null,imgLike[]=null,imgComments[]=null,imgShare[]=null;
TextView txtId=null,txtLikeUnlike[]=null,txtComments[]=null;
公共ArrayList arr=null;
Context=null;
LayoutInflater LayoutInflater=null;
HashMap getData=新HashMap();
公共适配器1(上下文,
ArrayList(arr){
this.context=上下文;
this.arr=arr;
layoutInflater=layoutInflater.from(上下文);
this.imgUnlike=新图像视图[arr.size()];
this.imgLike=新图像视图[arr.size()];
this.imgComments=新图像视图[arr.size()];
this.imgShare=新图像视图[arr.size()];
this.txtLikeUnlike=新文本视图[arr.size()];
this.txtComments=新文本视图[arr.size()];
}
@凌驾
public int getCount(){
返回arr.size();
}
@凌驾
公共对象getItem(int位置){
返回arr.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@SuppressLint(“充气参数”)
@凌驾
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
视图行=空;
if(convertView==null){
充气机=((活动)上下文)
.getLayoutInflater();
行=充气机。充气(R.layout.list\u项目,父项,
假);
}否则{
行=转换视图;
}
/**初始化小部件*/
/**图像视图*/
imgImage=(图像视图)行
.findviewbyd(R.id.imgImage);
imgUnlike[位置]=(图像视图)行
.findviewbyd(R.id.imgUnlike);
imgLike[位置]=(图像视图)行
.findviewbyd(R.id.imgLike);
imgComments[位置]=(图像视图)行
.findViewById(R.id.imgComments);
imgShare[位置]=(图像视图)行
.findviewbyd(R.id.imgShare);
/**文本视图*/
txttId=(文本视图)行
.findviewbyd(R.id.txtId);
txtLikeUnlike[位置]=(文本视图)行
.findviewbyd(R.id.txtLikeUnlike);
txtComments[位置]=(文本视图)行
.findViewById(R.id.TXT注释);
getData=arr.get(位置);
setText(getData.get(Fragment1.TAG_ID));
类[位置]
.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
类[位置]
.setVisibility(视图.不可见);
伊姆格利克[职位]
.setVisibility(View.VISIBLE);
getCurrentPosition=位置;
}
});
imgLike[position].setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
imgNewsFeedLike[position].setVisibility(View.INVISIBLE);
imgnewsfeed[position].setVisibility(View.VISIBLE);
}
});
row.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(getActivity(),
细节(类别);
星触觉(意向);
}
});
返回行;
}
xml文件是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/masterLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_dp" >
<ImageView
android:id="@+id/imgImage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="@+id/imgBlueStrip"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:background="@null"
android:contentDescription="@string/content_description"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imgImage"
android:background="@drawable/strip"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/imgUnlike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="@string/content_description"
android:src="@drawable/unlike" />
<ImageView
android:id="@+id/imgLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/content_description"
android:src="@drawable/like"
android:visibility="invisible" />
<TextView
android:id="@+id/txtLikeUnlike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/imgUnlike"
android:background="@drawable/get_notification_bg"
android:gravity="center"
android:padding="2dp"
android:textColor="@color/white"
android:textSize="10sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/imgComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="@string/content_description"
android:src="@drawable/comments" />
<TextView
android:id="@+id/txtComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgComments"
android:background="@drawable/get_"
android:gravity="center"
android:padding="2dp"
android:textColor="@android:color/white"
android:textSize="10sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="@+id/imgShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="@string/content_description"
android:src="@drawable/share" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="id" />
<ImageView
android:id="@+id/imgBlueStrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txtId"
android:contentDescription="@string/content_description"
android:src="@drawable/blue_strip" />
</RelativeLayout>
我的布局显示如下所示,您应该使用holder。@Slade如果我使用ViewHolder,那么如何在构造函数中这样定义数组。imgLike=new ImageView[arr.size()];你不需要为此更改构造函数..参考此链接..@Slade ohk Thanx我可以试试,然后在我告诉你之后,会发生什么?@Slade如果我使用ViewHolder,如何在构造函数中初始化imgLike数组?