Android ListView中的按钮始终从第一项发送数据
我正在ListView中实现一个“like”系统。每个列表项都包含一个视频、喜欢的数量、视频的id和“喜欢”按钮 视频id显示正确(每个视频不同),但无论我单击哪个“喜欢”按钮,它总是从列表中的第一项发送视频id 总而言之,我的“Like”方法似乎不知道按下了哪个按钮,它会自动选择第一个按钮并发送视频id。 感谢大家的帮助 单击“like”按钮时,将调用like方法:Android ListView中的按钮始终从第一项发送数据,android,android-listview,Android,Android Listview,我正在ListView中实现一个“like”系统。每个列表项都包含一个视频、喜欢的数量、视频的id和“喜欢”按钮 视频id显示正确(每个视频不同),但无论我单击哪个“喜欢”按钮,它总是从列表中的第一项发送视频id 总而言之,我的“Like”方法似乎不知道按下了哪个按钮,它会自动选择第一个按钮并发送视频id。 感谢大家的帮助 单击“like”按钮时,将调用like方法: holder.btnLike.setOnClickListener(new View.OnClickListener() {
holder.btnLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Like().execute();
}
});
class Like extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ThreadActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
Bundle extras = getIntent().getExtras();
protected String doInBackground(String... args) {
String price = fbID;
TextView videoid = (TextView)findViewById(R.id.video_id);
String video_id = videoid.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", video_id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_add_like,
"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), ThreadActivity.class);
i.putExtra(TAG_PID, pid);
i.putExtra("EXTRA_FACEBOOK_ID",fbID);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
以下是类似的方法:
holder.btnLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Like().execute();
}
});
class Like extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ThreadActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
Bundle extras = getIntent().getExtras();
protected String doInBackground(String... args) {
String price = fbID;
TextView videoid = (TextView)findViewById(R.id.video_id);
String video_id = videoid.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", video_id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_add_like,
"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), ThreadActivity.class);
i.putExtra(TAG_PID, pid);
i.putExtra("EXTRA_FACEBOOK_ID",fbID);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
类扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(ThreadActivity.this);
pDialog.setMessage(“创建产品…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
Bundle extras=getIntent().getExtras();
受保护的字符串doInBackground(字符串…args){
字符串价格=fbID;
TextView视频id=(TextView)findView视频id(R.id.video\u id);
字符串video_id=videoid.getText().toString();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“价格”,价格));
参数添加(新的BasicNameValuePair(“描述”,视频id));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u add\u like,
“POST”,params);
//检查cat日志以获取响应
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
Intent i=新的Intent(getApplicationContext(),ThreadActivity.class);
i、 putExtra(TAG_PID,PID);
i、 putExtra(“额外的FACEBOOK ID”,fbID);
星触觉(i);
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
}
}
您的holder对象需要对该行的视频文本视图进行引用。然后,单击按钮时,需要从该文本视图中获取文本并将其传递给任务。像这样:
holder.btnLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Like().execute(holder.videoTextView.getText().toString());
}
});
您的异步任务将如下所示:
class Like extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ThreadActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
Bundle extras = getIntent().getExtras();
protected String doInBackground(String... args) {
String price = fbID;
String video_id = args[0];
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", video_id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_add_like,
"POST", params);
// check log cat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), ThreadActivity.class);
i.putExtra(TAG_PID, pid);
i.putExtra("EXTRA_FACEBOOK_ID",fbID);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
类扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(ThreadActivity.this);
pDialog.setMessage(“创建产品…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
Bundle extras=getIntent().getExtras();
受保护的字符串doInBackground(字符串…args){
字符串价格=fbID;
字符串video_id=args[0];
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“价格”,价格));
参数添加(新的BasicNameValuePair(“描述”,视频id));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u add\u like,
“POST”,params);
//检查cat日志以获取响应
d(“创建响应”,json.toString());
//检查成功标签
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建产品
Intent i=新的Intent(getApplicationContext(),ThreadActivity.class);
i、 putExtra(TAG_PID,PID);
i、 putExtra(“额外的FACEBOOK ID”,fbID);
星触觉(i);
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
}
你说“视频id显示正确”,你是否调试过,以检查你在doInBackground()
中是否获得了正确的video\u id
?或者,它在哪里“正确显示”?它在相应视频旁边的列表视图中正确显示。我还没有完成调试,我只知道在“doInBackground”()“,'findViewById(R.id.video_id)'只是选择列表中的第一个视频id。您需要以某种方式传递该id,例如通过AsyncTask
的构造函数,或者将其设置为成员变量(如果它是内部类),谢谢您的帮助!在仔细查看/尝试和错误后,我发现了这篇对我帮助很大的SO帖子-