ListView 中的按钮仅返回第一个索引的值,不更新索引。因此所有按钮的结果都是相同的。
<Button
android:text="More Info"
android:id="@+id/btninfo"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="myClickHandler"
>
// Listens to user selecting on an item/row
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
Hashtable<String,String> listOfData = new Hashtable<String,String>();
/* Retrieve the data of the particular item selected based on the position of the item
* and pass it to the next activity
*/
listOfData = listData.get(position);
Intent detailedView = new Intent();
detailedView.setClassName("com.", "com.");
detailedView.putExtra("listOfData", listOfData);
startActivity(detailedView);
}
});
};
这工作正常,因为我可以单击意图进入另一个活动的行。
public void myClickHandler(View view){
String info = listData.get(position).get("coverImage");
Intent infoIntent = new Intent(android.content.Intent.ACTION_VIEW);
infoIntent.setData(Uri.parse("http://......=" + info + "......."));
startActivity(infoIntent);
}
问题从这里开始。当我单击第一个列表视图行中的按钮时,它会为该位置中的特定项目提供正确的 Web 视图。但是,当我单击列表视图行中的其余按钮时,它会不断返回仅用于列表视图中第一个位置的相同 Web 视图。嗯,我希望不同行上的按钮根据它们在列表视图中的位置意图进入不同的网址(意图网络视图)。我猜问题在于应用程序无法检测到位置?
我仍然是 android 新手,已经尝试了好几天了〜所以非常感谢所提供的任何帮助!提前致谢!如果我的问题的措辞方式看起来很混乱,抱歉~
// What I've declared in a Adapter~
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
<Button
android:text="More Info"
android:id="@+id/btninfo"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:onClick="myClickHandler"
>
// Listens to user selecting on an item/row
list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
Hashtable<String,String> listOfData = new Hashtable<String,String>();
/* Retrieve the data of the particular item selected based on the position of the item
* and pass it to the next activity
*/
listOfData = listData.get(position);
Intent detailedView = new Intent();
detailedView.setClassName("com.", "com.");
detailedView.putExtra("listOfData", listOfData);
startActivity(detailedView);
}
});
};
This works fine as I'm able to click on the row which intents into another activity.
public void myClickHandler(View view){
String info = listData.get(position).get("coverImage");
Intent infoIntent = new Intent(android.content.Intent.ACTION_VIEW);
infoIntent.setData(Uri.parse("http://......=" + info + "......."));
startActivity(infoIntent);
}
The problem starts here. When I click on the button in the first list view row, it intents to the correct web view for the particular item in the position. But when I click on the rest of the buttons in the list view rows it keeps returning the same webview intended only for the first position in the list view. Hmm, i want the buttons on the different rows to intent into different urls(intent to webview) according to their position in the list view. My guess the problem lies in the application not being able to detect the position?
I'm still a newbie with android and have been trying for days~ So any help given is much appreciated! Thanks in advance! Sorry if the way my question is phrased seemed confusing~
// What I've declared in a Adapter~
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
myClickHandler()
中的position
变量未声明,并且不变化
。因此默认它为您提供0 位置
并且您正在获取第一个索引数据如果您想重用它,则采用
position
变量global
并更新 listview 的onItemClick
事件中的值但是您必须使用自定义适配器来处理每个按钮的点击事件列表视图的行
在适配器的 getView() 方法中,您可以处理单击事件。
参考文献
position
variable inmyClickHandler()
is undeclared and notvaries
.so default it is giving you0 position
and you are getting first index dataIf you want to reuse this then take
position
variableglobal
and update the value inonItemClick
event of listviewBut you have to use Custom-adapter for handle click event of each button on a row of list-view
and in getView() method of adapter you can handle click event.
References