Trying to display toast when click on list items but showing many errors in Visual studio. The codes are working fine in android studio and i remake it for C# using RemobjectC#. Below are my mainactivity codes
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();
}
});
*/
}
}
}
codes of main layout xml file
<?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>
i don't have much knowledge of C#. Please suggest me how to remake it on C# and why i am getting this errors
These are the errors
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();
}
});
Replace this with your code.
Try
String tvShowPicked = "You selected"+String.valueOf(theAdapter.getItemAtPosition(position));
instead of
String tvShowPicked = "You selected"+String.valueOf(adapterView.getItemAtPosition(position));
by making your adapter Global or setting it to final
The problem is that in C# anonymous types cannot implement methods. In this case you need to use a lambda or implement the methods in the main class.
I used @AnujSharma's answer and changed the code a bit:
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();
}
});
Instead of:
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();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.