我有一个自动填充文本视图,显示来自sq-lite数据库的公司名称。从数据库记录得到正确没有问题。但问题是适配器.adapter不清楚像首先我搜索的东西和建议显示而不是删除它再次输入并且在数据库中没有建议但适配器显示旧的建议是在最后搜索。 下面我把图像清楚地标识出问题是什么
1:
public class TopupTransfer extends BaseActivity{
AutoCompleteTextView memberView;
ArrayList <String> name = null,name1= null;
Button btnSubmit;
String membercode2,amount;
EditText amnt,smspin;;
HashMap<String,String> memberDetail;
AutoCompleteAdapter2 adapter;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.topuptransfer);
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
{
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
}
/*ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back);*/
db = new DatabaseHelper(TopupTransfer.this);
memberView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
name = new ArrayList<String>();
name1 = new ArrayList<String>();
memberDetail = new HashMap<String,String>();
amnt = (EditText) findViewById(R.id.topup_amnt);
btnSubmit = (Button) findViewById(R.id.button);
smspin = (EditText) findViewById(R.id.smspin);
EditText mobileno = (EditText) findViewById(R.id.mobileno);
mobileno.setText(ResponseString.getMobno());
if(ResponseString.getRequiredSmsPin().equals("TRUE"))
{
smspin.setVisibility(View.VISIBLE);
}
else
{
smspin.setVisibility(View.INVISIBLE);
}
memberView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text = s.toString();
Log.d("text", "" + text);
//adapter.clear();
int len = text.length();
if (memberView != null)
{
if( len >= 3) {
Log.d("text", "" + text);
try {
if(name1 != null){name1.clear();}
name1 = GetList(text);
Log.d("name1", "" + name1.size());
if(ResponseString.getStcode().equals("0")) {
if (name1.size() != 0) {
Log.d("ADPTER LIST", name1.toString());
//adapter = new AutoCompleteAdapter(TopupTransfer.this, R.layout.listview_raw,R.id.desc, name1);
adapter = new AutoCompleteAdapter2(TopupTransfer.this,R.id.desc, name1);
memberView.setAdapter(adapter);
}
}
else {
Toast.makeText(TopupTransfer.this, "" + BaseActivity.sMsg, Toast.LENGTH_SHORT).show();
}
}catch(Exception e){
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(TopupTransfer.this));
//throw new RuntimeException("Check in Topup transfer");
}
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ResponseString.getRequiredSmsPin().equals("TRUE"))
{
String sms = smspin.getText().toString();
String rs = ResponseString.getSmspwd();
if (sms.length() == 0) {
toastValidationMessage(TopupTransfer.this, getResources().getString(R.string.plsentersmspin));
smspin.requestFocus();
}
else if (!sms.equals(rs)) {
toastValidationMessage(TopupTransfer.this, getResources().getString(R.string.pinentercorrect));
}
}
if (memberView.getText().toString().length() == 0) {
toastValidationMessage(TopupTransfer.this, getResources().getString(R.string.plsenterfirm));
memberView.requestFocus();
return;
}else if (amnt.getText().toString().length() == 0) {
toastValidationMessage(TopupTransfer.this, getResources().getString(R.string.plsenteramnt));
amnt.requestFocus();
return;
}
String Dpattern = memberView.getText().toString();
membercode2 = BaseActivity.detailMember.get(Dpattern);
amount = amnt.getText().toString();
Log.d("membercode",""+ membercode2);
Log.d("amount", "" + amount);
try {
if (membercode2.equals(null)) {
Toast.makeText(TopupTransfer.this, "Firm name is not Valid", Toast.LENGTH_SHORT).show();
} else {
boolean con = isInternetConnected();
if (con) {
AsyncTaskCommon asy = new AsyncTaskCommon(TopupTransfer.this, membercode2, amount);
stcode = asy.execute("TopupTransfer").get();
/*int i = 0;
//BaseActivity.showProgressDialog(LoginActivity.this);
while (!BaseActivity.baseResponse) {
i++;
}*/
if(stcode.equals("0"))
{
AlertDialog.Builder builder = new AlertDialog.Builder(TopupTransfer.this);
builder.setTitle(R.string.app_name);
builder.setMessage(BaseActivity.sMsg);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
BaseActivity.sMsg = "";
finish();
Intent m = new Intent(TopupTransfer.this,TopupTransfer.class);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
startActivity(m);
}
});
builder.show();
}
else
{
toastValidationMessage(TopupTransfer.this, sMsg);
}
} else {
Toast.makeText(TopupTransfer.this, "Check your Internet Connection", Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e)
{
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(TopupTransfer.this));
}
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.action_signout:
logout(TopupTransfer.this);
default:
return super.onOptionsItemSelected(item);
}
}
ArrayList<String> GetList(String text) {
ArrayList<String> arrayListtemp = new ArrayList<String>();
Cursor cursor = db.getTimeRecordList(text, "ChildUserInfo");
if (cursor != null){
if (cursor.moveToFirst()) {
do {
arrayListtemp.add(cursor.getString(cursor.getColumnIndex("FirmName")));// added your table value from database
} while (cursor.moveToNext());
}
}
return arrayListtemp;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
doExit();
}
return super.onKeyDown(keyCode, event);
}
private void doExit() {
finish();
Intent intent = new Intent(TopupTransfer.this,HomePage.class);
startActivity(intent);
}
}
适用于适配器类 AutoCompleteAdapter.java
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> fullList = null;
private ArrayList<String> mOriginalValues= null;
private ArrayFilter mFilter = null;
public AutoCompleteAdapter(Context context, int resource,int textViewResourceId, ArrayList<String> fullList) {
super(context, resource, textViewResourceId, fullList);
this.fullList = fullList;
mOriginalValues = new ArrayList<String>(fullList);
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public String getItem(int position) {
return fullList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<String>(fullList);
}
}
ArrayList<String> queryResults;
if (prefix != null && prefix.length() >= 3 && mOriginalValues != null) {
queryResults = autocomplete(prefix.toString());
} else {
queryResults = new ArrayList<String>(); // empty list/no suggestions showing if there's no valid constraint
}
results.values = queryResults;
Log.d("results2",""+results.values);
results.count = queryResults.size();
Log.d("results count2 ",""+results.count);
return results;
}
private ArrayList<String> autocomplete(String input) {
ArrayList<String> queryResults = new ArrayList<String>();
if(mOriginalValues != null) {
ArrayList<String> values = mOriginalValues;
int count = values.size();
if (count >= 1) {
for (int i = 0; i < count; i++) {
String item = values.get(i);
queryResults.add(item);
}
}
}
return queryResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
if (results.values != null) {
Log.d("results",""+results.values);
fullList = (ArrayList<String>) results.values;
Log.d("fullist",""+fullList.toString());
} else {
fullList = new ArrayList<String>();
}
if (results.count > 0) {
Log.d("result count", "" + results.count);
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
答案 0 :(得分:0)
更新适配器以填充新数据后,必须使用NotifyDataSetChanged
if(ResponseString.getStcode().equals("0")) {
if (name1.size() != 0) {
Log.d("ADPTER LIST", name1.toString());
//adapter = new AutoCompleteAdapter(TopupTransfer.this, R.layout.listview_raw,R.id.desc, name1);
adapter = new AutoCompleteAdapter2(TopupTransfer.this,R.id.desc, name1);
memberView.setAdapter(adapter);
adapter.NotifyDatasetChanged():
}
}
答案 1 :(得分:0)
这就是我在我的案例中取得的成就,请查看:
public class EmployeeAdapter extends BaseAdapter
{
Context context;
private ArrayList< Employee > employeelist ;
private ArrayList< Employee > arraylist;
public EmployeeAdapter( Context context2, ArrayList< Employee > employeeList2)
{
this.context = context2;
this.employeelist = employeeList2;
this.arraylist = new ArrayList<Employee>();
this.arraylist.addAll(employeelist);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return employeelist.size();
}
@Override
public Object getItem(int position) {
return employeelist.get(position).getEMPLOYEEID();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
LayoutInflater inflater = ( ( Activity ) context ).getLayoutInflater();
final ViewHolder holder;
if ( convertView == null )
{
convertView = inflater.inflate( R.layout.cursor_row, null );
holder = new ViewHolder();
holder.employeename = ( TextView ) convertView.findViewById( R.id.textcursor );
convertView.setTag( holder );
}
else
{
holder = ( ViewHolder ) convertView.getTag();
}
holder.empname=employeelist.get( position ).getNAME();
holder.empid=employeelist.get( position ).getEMPLOYEEID();
holder.employeename.setText(holder.empname );
convertView.setOnClickListener( new OnClickListener()
{
@Override
public void onClick( View v )
{
//context.startActivity(new Intent(context, EmployeeActivity.class));
Intent i = new Intent( context, EmployeeActivity.class );
int empid=employeelist.get( position ).getEMPLOYEEID();
i.putExtra( "emp_id", empid );
i.putExtra( Variables.EmployeeType, 1 );
context.startActivity(i);
}
} );
convertView.setOnLongClickListener( new OnLongClickListener()
{
@Override
public boolean onLongClick( View v )
{
Log.e( "", "Long clickeddddddd" );
notifyDataSetChanged();
return false;
}
} );
notifyDataSetChanged();
return convertView;
}
private class ViewHolder
{
TextView employeename;
String empname;
int empid;
// TextView FreeItemquantity;
// CheckBox check1;
}
public void filter( String charText )
{
charText = charText.toLowerCase( Locale.getDefault() );
employeelist.clear();
if ( charText.length() == 0 )
{
notifyDataSetChanged();
employeelist.addAll( arraylist );
}
else
{
for ( Employee wp : arraylist )
{
if ( wp.getNAME().toLowerCase( Locale.getDefault() ).contains( charText ) )
{
employeelist.add( wp );
}
}
if ( employeelist.size() < 1 )
{
Toast.makeText(context, "No Matches Found", Toast.LENGTH_SHORT).show();
//Toast.makeText( context, " No Matches Found ", 1 );
}
}
notifyDataSetChanged();
}
private Context getContext() {
// TODO Auto-generated method stub
return null;
}
/*public ItemClicked getItem(int position){
return items.get(position);
}*/
}