单击列表项时尝试显示吐司,但在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"
...
答案 0 :(得分:0)
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();
}
});
将其替换为您的代码。
答案 1 :(得分:0)
尝试
String tvShowPicked = "You selected"+String.valueOf(theAdapter.getItemAtPosition(position));
而不是
String tvShowPicked = "You selected"+String.valueOf(adapterView.getItemAtPosition(position));
将适配器设为全局或将其设置为最终
答案 2 :(得分:0)
问题是在C#中匿名类型无法实现方法。在这种情况下,您需要使用lambda或实现主类中的方法。
答案 3 :(得分:0)
我使用了@ AnujSharma的答案并稍稍更改了代码:
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
而不是:
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();
}
});