Android的加入字符串的ListView [英] Android adding string to ListView
问题描述
作为一个短信应用我提出的一部分,我需要从一个TextView在ListView动态地添加文本行的TextView的变化......每次加另一行的TextView的说不同的东西。
话虽这么说,我创建了一个小例子项目,试图得到这样的窍门,我已经看过了很多例子,但仍然没有工作。在启动的应用程序,它暂停了一下,然后崩溃。
我想从我的TextView文本。(ID:tvContent)从main.xml中的布局添加到ListView的行
下面是我的XML布局:
<?XML版本=1.0编码=UTF-8&GT?;<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@彩色/黑白><按钮
机器人:ID =@ + ID / addBtn
机器人:文字=添加新项
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
安卓的onClick =为addItems>
< /按钮><的TextView
机器人:ID =@ + ID / tvContent
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=嘿! >
< / TextView的>< ListView控件
机器人:ID =@机器人:ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:drawSelectorOnTop =假
机器人:背景=@彩色/黑白>
< /&的ListView GT;< / LinearLayout中>
我的活动课:
包com.example.addtolistview;进口的java.util.ArrayList;
进口android.os.Bundle;
进口android.app.ListActivity;
进口android.widget.ArrayAdapter;
进口android.widget.TextView;公共类ListViewDemo扩展ListActivity {ArrayList的<串GT;时listItems =新的ArrayList<串GT;();ArrayAdapter<串GT;适配器;TextView的theFact =(的TextView)findViewById(R.id.tvContent);
。字符串shareFact = theFact.getText()的toString();@覆盖
公共无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main);
适配器=新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_list_item_1,
listItems中);
setListAdapter(适配器); listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
}
下面是我的日志猫......我无法弄清楚如何实现它的问题niceley ...
D / AndroidRuntime(11420):关闭VM
W / dalvikvm(11420):主题ID = 1:螺纹未捕获的异常退出(组= 0x40c5fa68)
致命异常:主要
了java.lang.RuntimeException:无法实例活动ComponentInfo {com.example.addtolistview / com.example.addtolistview.ListViewDemo}:显示java.lang.NullPointerException
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
在android.app.ActivityThread.access $ 600(ActivityThread.java:128)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4514)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
在dalvik.system.NativeStart.main(本机方法)
显示java.lang.NullPointerException:产生的原因
在android.app.Activity.findViewById(Activity.java:1794)
在com.example.addtolistview.ListViewDemo<&初始化GT;(ListViewDemo.java:15)
在java.lang.Class.newInstanceImpl(本机方法)
在java.lang.Class.newInstance(Class.java:1319)
在android.app.Instrumentation.newActivity(Instrumentation.java:1027)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
... 11更多
您想你吹你的布局之前,所以这是一个简单的NullPointerExecption得到TextView的。
更改code:
公共类ListViewDemo扩展ListActivity { ArrayList的<串GT;时listItems =新的ArrayList<串GT;();
ArrayAdapter<串GT;适配器; TextView的theFact;
串shareFact;
@覆盖
公共无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main); theFact =(的TextView)findViewById(R.id.tvContent)
。shareFact = theFact.getText()的toString(); 适配器=新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_list_item_1,
listItems中);
setListAdapter(适配器); listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
}
如果您preSS的按钮,它会调用为addItems()函数(如你设置的话),但它尚未实施,因此会产生其他错误。
公共无效为addItems(视图v){
listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
行添加此功能下方的onCreate()方法,并移动listItems.add()和adapter.notifyDataSetChanged()这一新方法。
As part of a SMS app I am making, I need to add text from a TextView to a Row in a ListView dynamically as the TextView changes... adding another Row each time the TextView says something different.
That being said, I created a small example project to try and get the hang of doing this, and I have looked up many examples, but still not working. Upon starting the app, it pauses for a bit and then crashes.
I am trying to have the text from my TextView(id:tvContent) from the main.xml layout added to a row of the ListView.
Here is my XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black" >
<Button
android:id="@+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems" >
</Button>
<TextView
android:id="@+id/tvContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hey there!" >
</TextView>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:background="@color/black" >
</ListView>
</LinearLayout>
My Activity Class:
package com.example.addtolistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListViewDemo extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
TextView theFact = (TextView) findViewById(R.id.tvContent);
String shareFact = theFact.getText().toString();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
}
Here is my log cat... I couldn't figure out how to implement it in the question niceley...
D/AndroidRuntime(11420): Shutting down VM
W/dalvikvm(11420): threadid=1: thread exiting with uncaught exception (group=0x40c5fa68)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.addtolistview/com.example.addtolistview.ListViewDemo}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1794)
at com.example.addtolistview.ListViewDemo.<init>(ListViewDemo.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
... 11 more
You want to get the TextView before you inflate your layout, so it's a simple NullPointerExecption. Change your code:
public class ListViewDemo extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
TextView theFact;
String shareFact;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
theFact = (TextView) findViewById(R.id.tvContent)
shareFact = theFact.getText().toString();
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
}
If you press the button, it will call the addItems() function (as you set it), but it's not implemented yet, so it will generate an other error.
public void addItems(View v) {
listItems.add(shareFact);
adapter.notifyDataSetChanged();
}
Add this function below your onCreate() method, and move the listItems.add() and adapter.notifyDataSetChanged() lines to this new method.
这篇关于Android的加入字符串的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!