Android 将文本视图添加到列表视图

Android 将文本视图添加到列表视图,android,view,android-listview,textview,xamarin,Android,View,Android Listview,Textview,Xamarin,在Xamarin 我想向ListVIew添加一个简单的TextView,但不确定代码是否正确 这是我目前的代码: protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); ListView listView = new ListView(this.ApplicationContext); Te

Xamarin

我想向ListVIew添加一个简单的TextView,但不确定代码是否正确

这是我目前的代码:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView (Resource.Layout.Main);

    ListView listView = new ListView(this.ApplicationContext);
    TextView textView = new TextView (this.ApplicationContext);
    textView.Text = "Example text";
    View view = new View (this.ApplicationContext);
}
能给我一些帮助吗

编辑

这是我的密码:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    ListView lst = (ListView) FindViewById(Resource.Id.listView);

    // create the ArrayList to store the titles of nodes
    List<String> listItems = new List<String>();
    listItems.Add ("Test 1");
    listItems.Add ("Test 2");

    ArrayAdapter ad = new ArrayAdapter(this.ApplicationContext, Resource.Layout.simple_list_item_1, listItems);

    // give adapter to ListView UI element to render
    lst.SetAdapter(ad);

    }
可以帮我处理一下这个代码吗

提前感谢检查链接。您可以根据需要使用该链接

如果是一个简单的文本视图列表,请使用以下选项:

ListView lst = (ListView) findViewById(R.id.listView);

        // create the ArrayList to store the titles of nodes
        ArrayList<String> listItems = new ArrayList<String>();
XML文件:

<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"
  tools:context=".DisplayMessageActivity"
  android:orientation="vertical">

<ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:smoothScrollbar="true"
        android:fastScrollAlwaysVisible="false"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"/>

 </LinearLayout>

如果您只想在ListView中显示一些字符串:

声明以下变量:

private ArrayList<String> _listEntrys = new ArrayList<String>();    // String-ArrayList for storing the itmes that should be shown in the list
private ArrayAdapter<String> _arrayAdapter;                     // Arrayadapter to fill your list
private ArrayList\u listEntrys=new ArrayList();//用于存储应显示在列表中的ITME的字符串ArrayList
专用阵列适配器_ArrayAdapter;//Arrayadapter以填充您的列表
您的OnCreate应该如下所示:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Create ListView
    ListView listView = new ListView(this.ApplicationContext);

    // Set Array-Adapter
    _arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _listEntrys);
    listview.setAdapter(m_ArrayAdapter);
    listview.setOnItemClickListener(this);

    // Add here your desired List-Items
    _listEntrys.add("your string");

    _arrayAdapter.notifyDataSetChanged();   // Call this everytime you change the values in the ArrayList to refresh the view

}
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建列表视图
ListView ListView=新的ListView(this.ApplicationContext);
//设置阵列适配器
_arrayAdapter=新的arrayAdapter(这是android.R.layout.simple\u list\u item\u 1,\u listEntrys);
setAdapter(m_ArrayAdapter);
setOnItemClickListener(this);
//在此处添加所需的列表项
_添加(“您的字符串”);
_arrayAdapter.notifyDataSetChanged();//每次更改ArrayList中的值以刷新视图时都调用此函数
}

您需要创建一个自定义适配器,或使用simpleAdapter或ArrayAdapter在ListView中显示文本。您想在ListView的哪个位置添加此TextView?在第一个位置。@user22707为什么要添加它。在你的编辑中,你是在谈论将字符串项添加到列表中吗?它是否给了你NullPointerException@user22707?我得到了一个NullReferenceException@user22707这里贴哪一行?
private ArrayList<String> _listEntrys = new ArrayList<String>();    // String-ArrayList for storing the itmes that should be shown in the list
private ArrayAdapter<String> _arrayAdapter;                     // Arrayadapter to fill your list
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Create ListView
    ListView listView = new ListView(this.ApplicationContext);

    // Set Array-Adapter
    _arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _listEntrys);
    listview.setAdapter(m_ArrayAdapter);
    listview.setOnItemClickListener(this);

    // Add here your desired List-Items
    _listEntrys.add("your string");

    _arrayAdapter.notifyDataSetChanged();   // Call this everytime you change the values in the ArrayList to refresh the view

}