ListView 中的按钮仅返回第一个索引的值,不更新索引。因此所有按钮的结果都是相同的。

发布于 2024-11-26 17:12:45 字数 1665 浏览 2 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2024-12-03 17:12:45

myClickHandler() 中的 position 变量未声明,并且不变化。因此默认它为您提供 0 位置 并且您正在获取第一个索引数据

如果您想重用它,则采用 position 变量 global 并更新 listview 的 onItemClick 事件中的值

但是您必须使用自定义适配器来处理每个按钮的点击事件列表视图的行
在适配器的 getView() 方法中,您可以处理单击事件。

参考文献

position variable in myClickHandler() is undeclared and not varies.so default it is giving you 0 position and you are getting first index data

If you want to reuse this then take position variable global and update the value in onItemClick event of listview

But 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文