我试图创建autocompleteTextView
我正在关注此示例文章
https://www.androidcode.ninja/android-autocompletetextview-custom-arrayadapter-sqlite/
但是我没有使用CustomAutoCompleteView,而是使用import android.widget.AutoCompleteTextView;
听众正在关注
package com.fwd.android_google_001;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
public class CustomAutoCompleteTextChangedListener implements TextWatcher{
public static final String TAG = "CustomAutoCompleteTextChangedListener.java";
Context context;
public CustomAutoCompleteTextChangedListener(Context context){
this.context = context;
ObjectItemData[0] =new MyObject("abc");
ObjectItemData[1] =new MyObject("abc");
ObjectItemData[2] =new MyObject("abc");
ObjectItemData[3] =new MyObject("abc");
ObjectItemData[4] =new MyObject("abc");
ObjectItemData[5] =new MyObject("abc");
ObjectItemData[6] =new MyObject("abc");
ObjectItemData[7] =new MyObject("abc");
ObjectItemData[8] =new MyObject("abc");
ObjectItemData[9] =new MyObject("abc");
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
MyObject[] ObjectItemData = new MyObject[10];
@Override
public void onTextChanged(CharSequence userInput, int start, int before, int count) {
try{
// if you want to see in the logcat what the user types
MainActivity mainActivity = ((MainActivity) context);
// update the adapater
mainActivity.myAdapter.notifyDataSetChanged();
// get suggestions from the database
// MyObject[] myObjs =new MyObject[6];
// update the adapter
mainActivity.myAdapter = new AutocompleteCustomArrayAdapter(mainActivity, R.layout.list_view_row, ObjectItemData);
mainActivity.myAutoComplete.setAdapter(mainActivity.myAdapter);
mainActivity.myAutoComplete.showDropDown();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
主要活动XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<android.support.v7.widget.AppCompatAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/autoTextView"/>
<android.support.v7.widget.AppCompatAutoCompleteTextView
android:id="@+id/myautocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1" >
</android.support.v7.widget.AppCompatAutoCompleteTextView>
</LinearLayout>]
list_view_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textSize="15dp" />
</RelativeLayout>
在主要活动中
AutoCompleteTextVi myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete);
myAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
RelativeLayout rl = (RelativeLayout) arg1;
TextView tv = (TextView) rl.getChildAt(0);
myAutoComplete.setText(tv.getText().toString());
}
});
// add the listener so it will tries to suggest while the user types
myAutoComplete.addTextChangedListener(new CustomAutoCompleteTextChangedListener(this)); myAutoComplete.setThreshold(1);
// ObjectItemData has no value at first
MyObject[] ObjectItemData = new MyObject[10];
ObjectItemData[0] =new MyObject("abc");
ObjectItemData[1] =new MyObject("abc");
ObjectItemData[2] =new MyObject("abc");
ObjectItemData[3] =new MyObject("abc");
ObjectItemData[4] =new MyObject("abc");
ObjectItemData[5] =new MyObject("abc");
ObjectItemData[6] =new MyObject("abc");
ObjectItemData[7] =new MyObject("abc");
ObjectItemData[8] =new MyObject("abc");
ObjectItemData[9] =new MyObject("abc");
// set the custom ArrayAdapter
//myAdapter = new AutocompleteCustomArrayAdapter(this, R.layout.list_view_row, ObjectItemData);
myAdapter = new AutocompleteCustomArrayAdapter(this,android.R.layout.select_dialog_item, ObjectItemData);
myAutoComplete.setAdapter(myAdapter);
适配器
package com.fwd.android_google_001;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class AutocompleteCustomArrayAdapter extends ArrayAdapter<MyObject> {
final String TAG = "AutocompleteCustomArrayAdapter.java";
Context mContext;
int layoutResourceId;
MyObject data[] = null;
public AutocompleteCustomArrayAdapter(Context mContext, int layoutResourceId, MyObject[] data) {
super(mContext, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try{
/*
* The convertView argument is essentially a "ScrapView" as described is Lucas post
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
* It will have a non-null value when ListView is asking you recycle the row layout.
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout.
*/
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((MainActivity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
}
// object item based on the position
MyObject objectItem = data[position];
// get the TextView and then set the text (item name) and tag (item ID) values
TextView textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
textViewItem.setText(objectItem.objectName);
// in case you want to add some style, you can do something like:
textViewItem.setBackgroundColor(Color.CYAN);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
}
我的适配器getView被调用,但是我的建议没有出现在屏幕上。所有代码都在调试中触发
感谢帮助