这是我的应用程序屏幕截图,
这是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);
}
}
答案 0 :(得分:-1)
我认为您的适配器类将使用viewholder类似于下面的内容:
public class Adapter1 extends BaseAdapter {
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) {
final ViewHolder holder;
View row = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.imgUnlike[position] = (ImageView) row
.findViewById(R.id.imgUnlike);
holder.imgLike[position] = (ImageView) row
.findViewById(R.id.imgLike);
/** TextView */
holder.txtLikeUnlike[position] = (TextView) row
.findViewById(R.id.txtLikeUnlike);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/** Initialize Widgets */
/** Imageview */
getData = arr.get(position);
holder.txtLikeUnlike[position].setText(getData
.get(Fragment1.TAG_TOTAL_LIKE_COUNT));
holder.imgUnlike[position]
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.imgUnlike[position]
.setVisibility(View.INVISIBLE);
holder.imgLike[position]
.setVisibility(View.VISIBLE);
holder.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();
}
});
holder.imgLike[position].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder.imgLike[position].setVisibility(View.INVISIBLE);
holder.imgUnlike[position].setVisibility(View.VISIBLE);
urlGetAllLike = Urls.BASE_URL + "getlike.php";
getCurrentPosition = position;
new getAllLikeData().execute();
}
});
return row;
}
public class ViewHolder {
ImageView imgUnlike[] = null, imgLike[] = null,
imgComments[] = null;
TextView txtLikeUnlike[] = null, txtComments[] = null;
}
}
答案 1 :(得分:-1)
这是伪代码在特定注释中添加所需代码
public class LikeModel{
private String likes ;
public void setLikes(String likes){
this.likes = likes ;
}
public String getLikes(){
return likes ;
}
}
现在,当你从服务器获取数据时,只需在那里设置likes值并将模型传递给适配器,或者你可以使用Gson将bean与服务器数据集成在一起
public class Adapter1 extends BaseAdapter {
public Adapter1(Context context,
ArrayList<HashMap<String, String>> arr) {
// your own implementation or reference check here
}
@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;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = null;
if (convertView == null) {
ayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.YOUR_LIKE_TEXT_VIEW = (TextView)convertView.findViewById(R.id.your_id);
// Same way refer other view(like image view etc)
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
LikeModel model = likeModelArray.get(position);
holder.YOUR_LIKE_TEXT_VIEW.setText(model.getLikes());
holder.YOUR_LIKE_IMAGE.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
urlGetAllLike = Urls.BASE_URL + "getlike.php";
getCurrentPosition = position;
new getAllLikeData().execute(PASS POISTION HERE);
}
});
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> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
LikeModel model = likeModelArray.get(position);
model.setLikes(SET COUNTER VALUE HERE);
notifyDataSetChanged();
//
}
}