gpt4 book ai didi

android - 自定义自动完成 TextView 空间不起作用

转载 作者:太空狗 更新时间:2023-10-29 15:06:19 25 4
gpt4 key购买 nike

我有一个 AutoCompleteTextView,它有两个值 name 和 id,当用户输入一些东西时只显示名称。我面临的问题当用户在输入一些字母后按下空格键时,显示下行的相对值消失了.我没有找到问题所在。谁能帮帮我。

AutoCompleteTextView CustomerNameAutoFill = (AutoCompleteTextView)findViewById(R.id.AutoCompleteCustName);

ArrayList<Map<String, String>> mPeopleList = new ArrayList<Map<String, String>>();
SimpleAdapter mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview, new String[] { "Customer_name", "Customer_id" }, new int[] { R.id.customer_name, R.id.customer_id });

//Inserting all values in the autocompletetextview
try
{
DatabaseHandler db = new DatabaseHandler(this);
db.open();
Cursor c = db.getAllRecordsFromCustomerMaster0();
int i = 0;

if (c.moveToFirst())
{
do
{
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Customer_name", c.getString(1));
NamePhoneType.put("Customer_id", c.getString(0));

mPeopleList.add(NamePhoneType);
i++;
}while (c.moveToNext());
}
else {
Toast.makeText(CustomerDailyAnalysis.this, "No Customer Name.Please Sync data.", Toast.LENGTH_LONG).show();
}

db.close();
}
catch (Throwable e) {
Log.d("SQL Error",e+"");
}

CustomerNameAutoFill.setThreshold(1);
CustomerNameAutoFill.setAdapter(mAdapter);


CustomerNameAutoFill.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View view, int position, long id)
{
Map<String, String> map = (Map<String, String>)listView.getItemAtPosition(position);

String Customer_name = map.get("Customer_name");
String Customer_id = map.get("Customer_id");
CustomerNameAutoFill.setText(Customer_name);

saveInPreference("CustomerMasterNameForCDA", Customer_name);
saveInPreference("CustomerMasterIdForCDA", Customer_id);

Log.d("Log", "CustomerMasterNameForCDA: "+ Customer_name + " CustomerMasterIdForCDA" + Customer_id);
}
});

最佳答案

Khalani 是对的。这是我使用广泛使用的集合 Map 类的实现。

package com.urugn.autocomplete.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import com.urugn.android.v_route_info.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
*
* @author UruGN
*/
public class PlacesAdapter extends ArrayAdapter {

private final String MY_DEBUG_TAG = "MapAdapter";
private ArrayList<Map> items;
private ArrayList<Map> itemsAll;
private ArrayList<Map> suggestions;

private int viewResourceId;
private final String key = "description";


public PlacesAdapter(Context context, int resource, int textViewResourceId, List objects) {
super(context, resource, textViewResourceId, objects);
viewResourceId = textViewResourceId;
itemsAll = new ArrayList(objects);
suggestions = new ArrayList(objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Map map = (Map) getItem(position);
if (map != null) {
if (v != null) {
TextView vriPlace = (TextView) v.findViewById(R.id.vriPlace);
if (vriPlace != null) {
vriPlace.setText((String) map.get(key));

}
}
}
return v;
}

/**
*
* @return
*/
@Override
public Filter getFilter() {
return nameFilter;
}

Filter nameFilter = new Filter() {
@Override
public String convertResultToString(Object resultValue) {
String str = (String) ((Map) resultValue).get(key);
return str;
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("VRIPlacesFilter", "Filter " + constraint);
if (constraint != null) {
suggestions.clear();
for (Map map : itemsAll) {
if (((String) map.get(key)).toLowerCase().startsWith(constraint.toString().toLowerCase())) {

Log.d("VRIPlacesFilter", "Found " + map.get(key));
suggestions.add(map);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.d("VRIPlacesFilter", "Publish " + constraint + " " + results.count);
if (results != null && results.count > 0) {
ArrayList<Map> filteredList = (ArrayList<Map>) results.values;
clear();
for (Map c : filteredList) {
add(c);
Log.d("VRIPlacesFilter", "Publish " + c.get(key));
}

// notifyDataSetChanged();
}

if (results.count > 0) {
notifyDataSetChanged();
} else {
//notifyDataSetInvalidated();
}
}
};

}

设置 AutoCompleteTextView 的适配器

List<Map<String, String>> places = new ArrayList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>();
map.put("description", "My place");
map.put("description", "Her place");
map.put("description", "Our place");
map.put("description", "There place");
map.put("description", "The place");
places.add(map);

PlacesAdapter adapter = new PlacesAdapter(getBaseContext(), R.layout.auto_complete, R.id.vriPlace, places);

AutoCompleteTextView myAuto = new AutoCompleteTextView();
myAuto.setAdapter(adapter);

对于适配器布局 auto_complete.xml,我有以下内容。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/vriPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ff7e00"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>

希望这对您有所帮助。

关于android - 自定义自动完成 TextView 空间不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670265/

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