public class DictionaryListActivity extends Activity {
TextView userTextView;
EditText searchEditText;
Button searchButton;
ListView dictionaryListView;
String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();
DictionaryDatabaseHelper myDictionaryDatabaseHelper;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary_list);
Log.d("DICTIONARY", "second activity started");
userTextView=(TextView) findViewById(R.id.personTextView);
userTextView.setText(getIntent().getStringExtra(FragmentA.USER_NAME_STRING2));
searchEditText=(EditText) findViewById(R.id.searchEditText);
searchButton=(Button) findViewById(R.id.searchButton);
dictionaryListView=(ListView) findViewById(R.id.dictionaryListView);
myDictionaryDatabaseHelper=new DictionaryDatabaseHelper(this, "Dictionary", null, 1);
sharedPreferences=getSharedPreferences(FragmentA.SHARED_NAME_STRING2, MODE_PRIVATE);
boolean initialized=sharedPreferences.getBoolean("initialized", false);
if (initialized==false) {
//Log.d(logTagString, "initializing for the first time");
initializeDatabase();
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("initialized", true);
editor.commit();
}else {
Log.d(logTagString, "db already initialized");
}
allWordDefinitions=myDictionaryDatabaseHelper.getAllWords();
dictionaryListView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View view, ViewGroup arg2) {
if (view==null) {
view=getLayoutInflater().inflate(R.layout.list_item, null);
}
TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
Typeface font = Typeface.createFromAsset(getAssets(), "AnjaliOldLipi.ttf");
textView.setTypeface(font);
textView.setText(allWordDefinitions.get(position).word +"\n" + allWordDefinitions.get(position).definition );
return view;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return allWordDefinitions.size();
}
});
dictionaryListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
Intent intent =new Intent("android.intent.action.JAM");
intent.putExtra("word", allWordDefinitions.get(position).word);
intent.putExtra("definition", allWordDefinitions.get(position).definition);
startActivity(intent);
}
});
searchButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String string=searchEditText.getText().toString();
WordDefinition wordDefinition=myDictionaryDatabaseHelper.getWordDefinition(string);
if (string ==null) {
Toast.makeText(DictionaryListActivity.this, "No word Entered", Toast.LENGTH_LONG).show();
}
else if (wordDefinition==null && string !=null) {
Toast.makeText(DictionaryListActivity.this, "Word not found", Toast.LENGTH_LONG).show();
}else {
Intent intent =new Intent("android.intent.action.JAM");
intent.putExtra("word", wordDefinition.word);
intent.putExtra("definition", wordDefinition.definition);
startActivity(intent);
}
}
});
}
private void initializeDatabase() {
InputStream inputStream=getResources().openRawResource(R.raw.dictionary);
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
DictionaryLoader.loadData(bufferedReader, myDictionaryDatabaseHelper);
}
}
我提供了我的班级文件, 我希望textview能够在文本框中输入单词时动态显示结果。我的意思是添加一个listner。我想要的代码可以帮助它工作而不是语法或东西:)
答案 0 :(得分:0)
AutoCompleteTextView最适合这种情况,tutorial可能对您有帮助。
答案 1 :(得分:0)
您可以制作自定义适配器并覆盖过滤器以符合您的要求,如下所示
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
public class CustomListAdapter extends ArrayAdapter<String> {
private Context context;
private int rowViewResourceId;
private List<String> = new ArrayList<String>();
private List<String> backupstringList = null;
private TextView widgetName;
public CustomListAdapter(Context context, int rowViewResourceId, List<String> objects) {
super(context, rowViewResourceId, objects);
this.context = context;
this.rowViewResourceId = rowViewResourceId;
this.stringList = objects;
backupstringList = new ArrayList<String>();
backupstringList.addAll(stringList);
}
public int getCount() {
return this.stringList.size();
}
public String getItem(int index) {
return this.stringList.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//Do whatever you are doing
return row;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
stringList = (List<String>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
constraint = ((String) constraint).toLowerCase();
stringList.clear();
if (constraint.length() == 0) {
stringList.addAll(backupstringList);
} else {
for (String wp : backupstringList) {
stringList.add(wp);
}
}
results.count = stringList.size();
results.values = stringList;
return results;
}
};
return filter;
}
}
并在您的活动中使用此过滤器,例如
adapter.getFilter().filter(<search text>);