单击列表项时尝试显示 toast,但在 Visual Studio 中显示许多错误。这些代码在 android studio 中运行良好,我使用 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"
...