单击列表项时如何显示toast
本文关键字:显示 toast 何显示 列表 单击 | 更新日期: 2023-09-27 18:29:32
试图在单击列表项时显示toast,但在Visual studio中显示了许多错误。代码在android工作室中运行良好,我使用RemobjectC#为C#重新制作了它。以下是我的主要活动代码
using System;
using java.util;
using android.app;
using android.content;
using android.os;
using android.util;
using android.view;
using android.widget;
namespace org.me.androidapplication8
{
public class MainActivity: Activity {
protected override void onCreate(Bundle savedInstanceState) {
base.onCreate(savedInstanceState);
ContentView = R.layout.main;
// Simple array with a list of my favorite TV shows
String[] favoriteTVShows = {"Pushing Daisies", "Better Off Ted",
"Twin Peaks", "Freaks and Geeks", "Orphan Black", "Walking Dead",
"Breaking Bad", "The 400", "Alphas", "Life on Mars"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, favoriteTVShows);
ListView theListView = (ListView)findViewById(R.id.theListView);
theListView.setAdapter(theAdapter);
/*ERROR part
theListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public override void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String tvShowPicked = "You selected" +
String.valueOf(adapterView.getItemAtPosition(position));
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
}
});
*/
}
}
}
主布局xml文件的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/theListView"></ListView>
</LinearLayout>
我对C#知识不多。请建议我如何在C#上重新制作它,以及为什么我会收到这个错误
这些是的错误
theListView.setOnItemClickListener(new //Error:- cannot instantiate interface type
public override void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { //Syntax error Syntax error
String tvShowPicked = "You selected " + favoriteTVShows[position];//Unknown identifier (position)
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show(); //identifier expected(this)
}
});//Type expected
//(E374) opening parenthesis or less expected, got closing brace
//(E1) semicolon expected, got "public"
...
theListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public override void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String tvShowPicked = "You selected " + favoriteTVShows[position];
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
}
});
将其替换为您的代码。
尝试
String tvShowPicked = "You selected"+String.valueOf(theAdapter.getItemAtPosition(position));
而不是
String tvShowPicked = "You selected"+String.valueOf(adapterView.getItemAtPosition(position));
通过将适配器设置为全局适配器或将其设置为最终
问题是在C#中,匿名类型无法实现方法。在这种情况下,您需要使用lambda或实现主类中的方法。
我使用了@AnujSharma的答案,并稍微更改了代码:
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String tvShowPicked = "You selected " + favouriteTVShows[position];
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
}
});
代替:
theListView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
public override void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String tvShowPicked = "You selected " + favoriteTVShows[position];
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
}
});