我想填写Listview但是只重复imgLike,imgComments,txtLikeUnlike和来自所有项目的txtComments。因为我使用imgLike,imgComments,txtLikeUnlike和txtComments数组来获取(获取)所选位置。我的代码中有什么错误请指导我。
我的代码, / **适配器类* /
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;
}
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>
我的布局显示像
一样