【发布时间】:2014-02-03 13:13:04
【问题描述】:
考虑在我的 android listview 中有项目列表。
如果我必须更改我的第三个项目的值意味着它正在获取最后一个项目的值。
我的代码有什么问题?请给我一个解决方案。
我在我的适配器文件中使用了 belo 代码:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null) {
vi = inflater.inflate(R.layout.list_profile_griditem, null);
followphotobtn = (Button)vi.findViewById(R.id.followphoto);
HashMap<String, String> Order = new HashMap<String, String>();
Order = data.get(position);
final String photofollowstatus = Order.get(Profile.TAG_PHOTO_FOLLOW_STATUS);
if(photofollowstatus.equalsIgnoreCase("Following")){
followphotobtn.setText("UnFollow");
System.out.println("FollowBtn"+" "+"UnFollow");
}
else {
followphotobtn.setText("Follow");
System.out.println("FollowBtn"+" "+"Follow");
}
它运行良好..这里得到了正确的输出。
02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.284: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.292: I/System.out(19871): FollowBtn Follow
但我在 onclicklister 中没有得到正确的结果:
followphotobtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if(photofollowstatus.equals("Following")){
new UnFollowPhoto().execute(photo_id);
}
else {
new FollowingPhoto().execute(photo_id);
}
}
});
});
return vi;
}
class FollowingPhoto extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//showDialog(DIALOG_LOADING);
}
protected String doInBackground(String... args) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://dev.xxx.com/xxx/client/follow.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
System.out.println("photo_id"+" "+args[0]);
nameValuePairs.add(new BasicNameValuePair("userid", LoginPage.userid));
nameValuePairs.add(new BasicNameValuePair("photo_id", args[0]));
nameValuePairs.add(new BasicNameValuePair("photofollow_status", "Following"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
result_status = EntityUtils.toString(entity);
//res = response.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return result_status;
}
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
//dismissDialog(DIALOG_LOADING);
Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_LONG).show();
followphotobtn.setText("UnFollow");
Profile.myphotolistAdapter.notifyDataSetChanged();
followphotobtn.setText("UnFollow");
System.out.println(followphotobtn.getText().toString());
}
}
但我得到的输出是:
02-03 19:45:10.534: I/System.out(19871): UnFollow
但是点击这个按钮时这个值不会在android适配器文件中更新..
但我写了如下代码:
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
//dismissDialog(DIALOG_LOADING);
Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_SHORT).show();
followprofilephotobtn.setText("UnFollow");
Intent intent = new Intent(activity,Profile.class);
activity.startActivity(intent);
}
一旦重新加载活动,该值就会更改。但我需要在不重新加载活动的情况下更新值。
我的代码有什么问题?为什么我会遇到这个问题?
请给我一个想法和建议?
【问题讨论】:
-
这是一个适当的声明吗?:followphotobtn = (Button)vi.findViewById(R.id.followphoto);
-
data.get(position) 这是什么?数据中有什么。
-
我得到了你的解决方案,请回复。
-
@Harshid 所选列表项的位置。
-
我认为您在
if(convertView==null)之后缺少{。
标签: android button android-listview listadapter