我喜歡addTextChangedListener添加到我的ListView但之後運行它,它不會顯示在屏幕anythings,只是一個空白屏幕。該logcat中還給出了什麼,但只顯示了一個標籤是「ActivityThread.performLaunchAcitvity(ActivityThread $ ActivityClientRecord,意圖)線:2205」,其下方顯示「未找到源」一個按鈕(編輯源查找路徑).. 我已經嘗試了一些辦法,但不能找到一種方法,使其工作至今..如何正確添加addTextChangedListener到listView?
這裏是在MainActivity:
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;
}
}
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>
此外,我在其Manifest中添加了使用權限android:name =「android.permission.INTERNET」>。
在此先感謝。
後所有logcat的錯誤,所以我們可以看到發生了什麼。 – Sam
11-15 17:03:18.528:E/AndroidRuntime(23630):致命異常:主 11-15 17:03:18.528:E/AndroidRuntime(23630):了java.lang.RuntimeException:無法啓動活動ComponentInfo { com.example.testlistview/com.example.testlistview.MainActivity}:了java.lang.RuntimeException:你的內容必須有一個ListView其id屬性爲 'android.R.id.list' 11-15 17:03:18.528: E/AndroidRuntime(23630):\t at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205) 11-15 17:03:18.528:E/AndroidRuntime(23630):\t at android.app.ActivityThread.handleLaunchActivity ActivityThread.java:2240) –
11-15 17:03:18.528:E/AndroidRuntime(23630):\t at android.app.ActivityThread.access $ 600(ActivityThread.java:139) 11-15 17:03:18.528: E/AndroidRu ntime(23630):\t at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262) 11-15 17:03:18.528:E/AndroidRuntime(23630):\t at android.os.Handler.dispatchMessage( Handler.java:99) 11-15 17:03:18.528:E/AndroidRuntime(23630):\t在android.os.Looper.loop(Looper.java:156) 11-15 17:03:18.528:電子/ AndroidRuntime(23630):\t at android.app.ActivityThread.main(ActivityThread.java:4987) –