2014-01-21 115 views
0

Xamarin添加一个TextView到ListView

我想一个简单的TextView添加到ListView,但我不知道正确的代码。

这里是我当前的代码:

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); 

    } 

我收到此错误:

UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object 

我可以请有一定的帮助与此代码?

在此先感谢

+0

您需要创建一个自定义适配器或者使用simpleAdapter或ArrayAdapter在ListView中显示文本。 –

+0

你想在ListView的什么位置添加这个TextView? –

+0

在第一个位置。 – user22707

回答

0

如果它的一个简单的TextView名单,然后使用该命令:

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

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

然后加入全部ArrayList listItems中的文本 然后使用下面的方法

ArrayAdapter ad = new ArrayAdapter(ListActivity.this, 
       android.R.layout.simple_list_item_1, listItems); 

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

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> 
+0

你能看看我的编辑吗? – user22707

+0

@ user22707为什么添加它应该被添加。在你的编辑 – rup35h

+0

@ user22707解决?? – rup35h

0

如果你只是想显示在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 

你的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 

}