Android 如何将addTextChangedListener正确添加到listView?
我喜欢将addTextChangedListener添加到我的listView中,但运行后,它不会在屏幕上显示任何内容,只是一个空白屏幕。logcat也只显示一个选项卡,它是“ActivityThread.PerformLaunchActivity(ActivityThread$ActivityClientRecord,Intent)行:2205”,下面显示“找不到源”,并带有一个按钮(编辑源查找路径)。。 我试过一些方法,但到目前为止还找不到使它起作用的方法 以下是主要活动:Android 如何将addTextChangedListener正确添加到listView?,android,listview,android-edittext,Android,Listview,Android Edittext,我喜欢将addTextChangedListener添加到我的listView中,但运行后,它不会在屏幕上显示任何内容,只是一个空白屏幕。logcat也只显示一个选项卡,它是“ActivityThread.PerformLaunchActivity(ActivityThread$ActivityClientRecord,Intent)行:2205”,下面显示“找不到源”,并带有一个按钮(编辑源查找路径)。。 我试过一些方法,但到目前为止还找不到使它起作用的方法 以下是主要活动: public c
public class MainActivity extends ListActivity {
ListView lv;
EditText inputSearch;
DefaultHttpClient httpclient;
HttpPost httppost;
HttpResponse response;
InputStream is;
BufferedReader reader;
StringBuilder sb;
String line,result;
String[] people;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getList();
lv= (ListView) findViewById(R.id.list_view);
inputSearch= (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==0){
lv.clearTextFilter();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString());
}
});
}
public void getList(){
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/testing/testList.php");
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
people = result.split("[*]");
runOnUiThread(new Runnable()
{
public void run(){
ArrayAdapter<String> list = new ArrayAdapter<String>(MainActivity.this,R.layout.list_item, R.id.user_name,people);
lv.setAdapter(list);
}
});
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public类MainActivity扩展了ListActivity{
ListView lv;
编辑文本输入搜索;
默认httpclient httpclient;
HttpPost-HttpPost;
HttpResponse响应;
输入流为;
缓冲读取器;
为某人做准备;
字符串行,结果;
串[]人;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getList();
lv=(ListView)findViewById(R.id.list\u视图);
inputSearch=(EditText)findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(新的TextWatcher(){
公共无效后文本已更改(可编辑){
如果(s.length()==0){
lv.clearTextFilter();
}
}
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
public void onTextChanged(字符序列、int start、int before、int count)
{
lv.setTextFilterEnabled(真);
lv.setFilterText(s.toString());
}
});
}
public void getList(){
新线程(){
公开募捐{
试一试{
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://www.kryptoquest.com/testing/testList.php");
response=httpclient.execute(httppost);
is=response.getEntity().getContent();
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}
//将响应转换为字符串
试一试{
reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8);
sb=新的StringBuilder();
行=空;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
Log.d(“test”,sb.toString());
is.close();
结果=sb.toString();
人员=结果。拆分(“[*]”);
runOnUiThread(新的Runnable()
{
公开募捐{
ArrayAdapter列表=新的ArrayAdapter(MainActivity.this,R.layout.list_项,R.id.user_名称,人员);
低压设置适配器(列表);
}
});
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
}
}.start();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!--Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search.."
android:inputType="textVisiblePassword"/>
<!--List View -->
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--Single ListItem -->
<!--User Name -->
<TextView android:id="@+id/user_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
此外,我在其清单中添加了使用权限android:name=“android.permission.INTERNET”>
提前感谢。来自您的日志:
Your content must have a ListView whose id attribute is 'android.R.id.list'
这意味着将ListView的id
更改为:
<!--List View -->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
ListActivities需要在布局中使用此确切id的ListView。发布所有Logcat错误,以便我们看到发生了什么。11-15 17:03:18.528:E/AndroidRuntime(23630):致命异常:main 11-15 17:03:18.528:E/AndroidRuntime(23630):java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.testlistview/com.example.testlistview.MainActivity}:java.lang.RuntimeException:您的内容必须具有id属性为“android.R.id.list”11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ActivityThread.access$600(ActivityThread.java:139)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.os.Handler.dispatchMessage(Handler.java:99)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.os.Looper.loop(Looper.java:156)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ActivityThread.main(ActivityThread.java:4987)11-15 17:03:18.528:E/AndroidRuntime(23630):在java.lang.reflect.Method.invokenactive(本机方法)11-15 17:03:18.528:E/AndroidRuntime(23630):在java.lang.reflect.Method.invoke(Method.java:511)11-15 17:03:18.528:E/AndroidRuntime(23630):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(zyoteInit.java:784)11-15 17:03:18.528:E/AndroidRuntime(23630):在com.android.internal.os.zyoteInit.main(ZygoteInit.java:551)11-15 17:03:18.528:E/AndroidRuntime(23630):在dalvik.system.NativeStart.main(本机方法)11-15 17:03:18.528:E/AndroidRuntime(23630):原因:java.lang.RuntimeException:您的内容必须具有id属性为“android.R.id.list”的ListView 11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.ListActivity.onContentChanged(ListActivity.java:250)11-15 17:03:18.528:E/AndroidRuntime(23630):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)11-15 17:03:18.528:E/AndroidRuntime(23630):在android.app.Activity.setContentView(Activity.java:1892)谢谢,这对我来说是可行的!!但是有一个问题发生了,那就是,当编辑框被按下时,会出现一个黑框,图像如下:我看不到指向的链接