我是android的新手并尝试创建一个简单的Android应用程序。我在Visual Studio 2017和Xamarin中创建了这个Android应用程序。
在该应用程序中,我添加了一个列表视图,在点击列表视图的项目时,它会打开一个新活动。当我清理并构建应用程序时,它成功了。
我存档了该应用程序,并尝试在我的手机上进行测试。它会立即打开并关闭。
以下是参考代码: -
activity_main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:background="@drawable/done"
android:layout_width="match_parent"
android:layout_height="150px"
android:id="@+id/imageView1"
android:layout_alignParentTop="true" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView1"
android:layout_below="@id/imageView1" />
</RelativeLayout>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Android.Content;
using System;
namespace IH_Main
{
[Activity(Label = "MyApplication", Theme = "@android:style/Theme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
string[] tool_array = {"Name1","Name2","Name3" };
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
//Adding items in the list view using Array adapter
//Created an ArrayAdapter to store the list into Adapter so that we can set list string onto the list view.
ArrayAdapter<string>tools_list = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1,Android.Resource.Id.Text1,tool_array);
//Creating listview instance.
ListView listView = (ListView)FindViewById(Resource.Id.listView1);
//Setting adapter values to the listview.
listView.SetAdapter(tools_list);
listView.ItemClick += listView_ItemClick;
}
private void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
Toast.MakeText(this, e.ToString(), ToastLength.Long).Show();
Intent i = new Intent(this,typeof(info_display));
i.PutExtra("Tool_Name", e.ToString());
StartActivity(i);
}
}
}
Layout1.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<ImageView
android:background="@drawable/done"
android:layout_width="match_parent"
android:layout_height="150px"
android:id="@+id/imageView1"
android:layout_alignParentTop="true" />
<TextView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tool_title"
android:layout_below="@id/imageView1" />
</RelativeLayout>
Info_display.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace IH_Main
{
[Activity(Label = "info_display")]
public class info_display : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.layout1);
TextView t1 = FindViewById<TextView>(Resource.Id.tool_title);
string tool_name = Intent.GetStringExtra("Tool_Name");
t1.Text = tool_name;
}
}
}
在执行&#34; Build&#34;时,我收到以下警告。
&#39; AbsListView.SetAdapter(IListAdapter)&#39;已经过时了:&#39;请使用 适配器属性设置器&#39;
在MainActivity.cs的以下行
listView.SetAdapter(tools_list);
这种行为的可能原因是什么? 任何帮助都会很明显:)
答案 0 :(得分:0)
我可以通过更改以下行的语法来解决此问题:
listView.SetAdapter(tools_list);
至:
listView.Adapter = tools_list;