gpt4 book ai didi

android - addTextChangedListener 与 listview 与潜台词

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:58 24 4
gpt4 key购买 nike

我想在列表中搜索并再次在列表中显示结果所以我使用了 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查到.

其次是使用外部 jar Lambdaj这是从列表中过滤大量数据的最佳和有效方法。您也可以从 here 中查看.

关于android - addTextChangedListener 与 listview 与潜台词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com