作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在列表中搜索并再次在列表中显示结果所以我使用了 addtextchangelistener,但找不到一种方法让它与带有子文本的 ListView 一起工作
这是我的代码:
package com.android;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MyListDemoActivity extends ListActivity {
/** Called when the activity is first created. */
TextView tv;
//** List<String> content;
EditText actv;
List<String> arr_sort;
//** ArrayAdapter<String> adapter;
SimpleAdapter simpleadapter;
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String line = " ";
LineNumberReader linenoreader = null;
StringTokenizer stringtokanixer = null;
//** content = new ArrayList<String>();
List<Map<String,String>> data= new ArrayList<Map<String,String>>();
lv = (ListView) findViewById(android.R.id.list);
try {
InputStream istream = getResources().openRawResource(R.raw.grelist);
InputStreamReader streamreader = new InputStreamReader(istream);
linenoreader = new LineNumberReader(streamreader);
linenoreader.mark(15);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}// try catch ends here
Log.v("getting", "working");
for(int i=0;i<8;i++)
{
Map<String,String> datum= new HashMap<String,String>(2);
try {
line = linenoreader.readLine();
Log.v("item",line);
} catch (IOException e) {
e.printStackTrace();
}
Log.v("getting", line);
stringtokanixer = new StringTokenizer(line);
String st = stringtokanixer.nextToken();
String meaning="";
while (stringtokanixer.hasMoreTokens()) {
meaning +=" " +stringtokanixer.nextToken();
}// for ends
// map is used to add word and meaning
datum.put("word",st);
datum.put("meaning",meaning);
data.add(datum);
//List<String> is usedto add
//** content.add(st);
}
simpleadapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[]{"word","meaning"}, new int[]{android.R.id.text1,android.R.id.text2});
// setListAdapter(adapter);
lv.setAdapter(simpleadapter);
tv = (TextView) findViewById(R.id.textView1);
actv = (EditText) findViewById(R.id.editText1);
/*
actv.addTextChangedListener(new TextWatcher() {
int len = 0;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
arr_sort = new ArrayList<String>();
len = actv.getText().length();
for (int i = 0; i < content.size(); i++) {
if (len <= content.get(i).length()) {
if (actv.getText()
.toString()
.trim()
.equalsIgnoreCase(
(String) content.get(i).subSequence(0,
len))) {
arr_sort.add(content.get(i));
Log.v("infor loop afterTextChanged", s.toString());
}
}
}
// adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(MyListDemoActivity.this,
android.R.layout.simple_list_item_1, arr_sort);
setListAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.v("beforetextchange","hello here");
}
@Override
public void afterTextChanged(Editable s) {
Log.v("aftertextchange","hello here");
}
}); // text watcher class ends here
*/
}// on create ends here
public void onListItemClick(ListView ls, View v, int position, long id) {
//tv.setText(content.get(position));
// tv.setText(content[position]) // in case of string
}// endsd here onListItemClick(
}
最佳答案
我已经回答了两个 StackOverflow 问题本身,您可以回答,
首先是使用 getFilter()
android 提供的过滤功能,使用适配器类的 Filterable 接口(interface)。你可以从here
查到.
关于android - addTextChangedListener 与 listview 与潜台词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579922/
我是一名优秀的程序员,十分优秀!