【发布时间】:2014-12-17 11:34:15
【问题描述】:
这是我的应用程序屏幕截图,
这是 1 个列表项(包含 1 个 imageview 和 3 个 imagebutton 和 id)。我的 ListView 中有 50 个 ListItem 就像这样。
在此当用户单击喜欢按钮(第一个按钮)时,值会更新并将文本设置到该特定位置(这是有效的)但它会重复这样的值(当单击第一个喜欢按钮时 -> 位置 0 然后它重复第 4、7 等位置的值。
如何解决每 4 项重复设置文本的问题?
请指导我。 提前致谢。
我的代码是, /** 适配器类 */
public class Adapter1 extends BaseAdapter {
ImageView imgUnlike[] = null, imgLike[] = null,
imgComments[] = null;
TextView 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>();
String urlLike = null, urlCountLike = null, urlUnlike = null;
String strCountLike = null, strCountCommnets = null;
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.txtLikeUnlike = 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 */
imgUnlike[position] = (ImageView) row
.findViewById(R.id.imgUnlike);
imgLike[position] = (ImageView) row
.findViewById(R.id.imgLike);
/** TextView */
txtLikeUnlike[position] = (TextView) row
.findViewById(R.id.txtLikeUnlike);
getData = arr.get(position);
txtLikeUnlike[position].setText(getData
.get(Fragment1.TAG_TOTAL_LIKE_COUNT));
imgUnlike[position]
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgUnlike[position]
.setVisibility(View.INVISIBLE);
imgLike[position]
.setVisibility(View.VISIBLE);
strPostId = arr.get(position).get(
Fragment1.TAG_POST_ID);
urlLike = Urls.BASE_URL
+ "createpostlike.php?post_id=" + strPostId
+ "&user_id=" + myDetail.getUserId();
getCurrentPosition = position;
new sendLikeData().execute();
}
});
imgLike[position].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
imgLike[position].setVisibility(View.INVISIBLE);
imgUnlike[position].setVisibility(View.VISIBLE);
urlGetAllLike = Urls.BASE_URL + "getlike.php";
getCurrentPosition = position;
new getAllLikeData().execute();
}
});
return row;
}
public class sendLikeData extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh
.makeServiceCall(urlLike, ServiceHandler.GET);
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
urlCountLike = Urls.BASE_URL + "getalllike.php?post_id="
+ strPostId;
new getCountLikeData().execute();
};
}
/** Count the Total Like for Selected Items. */
private class getCountLikeData extends AsyncTask<Void, Void, Void> {
JSONObject jsonobject;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
dataList = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONFunctions.getJSONfromURL(urlCountLike);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("data");
for (int i = jsonarray.length() - 1; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
strCountLike = jsonobject.getString("Total");
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Adapter1.notifyDataSetChanged();
txtLikeUnlike[getCurrentPosition].setText(strCountLike);
}
}
【问题讨论】: