添加视图到ListView不起作用

时间:2015-05-05 15:26:35

标签: android listview

我实际上正在开展聊天活动,首先我尝试在 ListView 中设置仅包含一个 TextView 布局,但它不起作用。

这是我的布局(message.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message_content"
android:inputType="textMultiLine"/>
</LinearLayout>

这是我的ListView(activity_chat.xml):

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ChatActivity">

   <ListView
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_marginBottom="48dip"
    ></ListView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dip"
android:layout_alignParentBottom="true"
android:padding="0dp"
android:layout_margin="0dp"
android:background="#FFFFFF"
android:orientation="horizontal">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="10"
android:layout_marginLeft="10dp"
android:hint="Send a message"
android:id="@+id/newmsg"
></EditText>

<ImageButton
android:id="@+id/newmsgsend"
android:background="#FFFFFF"
android:src="@drawable/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">
</ImageButton>
</LinearLayout>
</RelativeLayout>

最后,我的活动:

package com.example.ismail.chat_test;

import android.app.ListActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ChatActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    SetConversation();

}

protected void SetConversation()
{

  ArrayList<String> ArrayMSG = new ArrayList<>();
  ArrayMSG.add(msg1);
  ArrayMSG.add(msg2);
  ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.message,ArrayMSG);
  ListView Lv  = (ListView) findViewById(R.id.list_item);
  Lv.setAdapter(myAdapter);
}
}

我的logcat在 Lv.addView(v)上显示错误,不知道为什么,我错过了什么?

1 个答案:

答案 0 :(得分:1)

您无法使用ListView方法添加addViewListView父类是AdapterView,因此您需要创建适配器,将数据设置为适配器,然后将此适配器设置为ListView。本教程将为您提供帮助 - ListView tutorial

你的例子:

ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("Title", "Hello1");
myArrList.add(map);


map = new HashMap<String, String>();
map.put("Title", "Hello2");
myArrList.add(map);

SimpleAdapter adapter = new SimpleAdapter(this, myArrList, android.R.layout.simple_list_item_2, 
        new String[] {"Title"}, 
        new int[] {android.R.id.text1});
Lv.setAdapter(adapter);

此外,您的LV为空,因为您在代码调用android:id="@android:id/list"中设置了ID - (ListView) findViewById(R.id.list_item)。将android:id="@+id/list_item"添加到xml布局中。