嗨我是非常新的Android和我的应用程序我添加了AutoCompleteTextView确定
但是我的主要要求是我想在AutoCompleteTextView中添加多个字段
像行星,颜色,卢比字符串数组列表我想在我的AutoCompleteTextView中显示如下图像
当我点击AutoCompleteTextView时,任何数据必须在一个Textview中显示的行
我完全感到困惑,因为我是非常新的Android如何做到这一点,请帮助我
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};
String[] colors = new String[] { "Red", "Blue", "Green", "Yellow",
"Orange", "Cyan", "Black", "Megenta"};
String[] Ruppes = new String[] { "100", "200", "300", "400",
"500", "600", "700", "800"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.AutoCompletion);
// Conutry_Names = getResources().getStringArray(R.array.planets);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,planets);
autoCompleteTextView.setAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<AutoCompleteTextView
android:id="@+id/AutoCompletion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:hint="@string/Enter_Name_Here"
/>
</LinearLayout>
答案 0 :(得分:0)
@SuppressLint("NewApi")
public class Compose_message extends Fragment {
Context con;
CustomAutoCompleteTextView to;
public static SQLiteDatabase simpledb=null;
public static String dbname="your database name";
String tablename="user";
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
con=this.getActivity();
simpledb=con.openOrCreateDatabase(dbname, con.MODE_PRIVATE, null);
View rootView = inflater.inflate(R.layout.activity_compose_message, container, false);
to=(CustomAutoCompleteTextView)rootView.findViewById(R.id.comose_to);
to.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
Cursor c=simpledb.rawQuery("select id,first_name,last_name,city from '"+tablename+"' where first_name like '%"+to.getText().toString()+"%' ",null);
aList.clear();
if(c.getCount()>0)
{
c.moveToFirst();
do
{
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("id", c.getString(c.getColumnIndex("id")));
hm.put("first_name", c.getString(c.getColumnIndex("first_name")));
hm.put("last_name", c.getString(c.getColumnIndex("last_name"))+" <");
hm.put("city", c.getString(c.getColumnIndex("city"))+" >");
aList.add(hm);
}while(c.moveToNext());
String[] from = {"first_name","last_name","city"};
int[] to1 = { R.id.first_name,R.id.last_name,R.id.city};
SimpleAdapter adapter = new SimpleAdapter(con, aList, R.layout.autocomplete_layout, from, to1);
to.setAdapter(adapter);
}
else
{
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
/*CustomAutoCompleteTextView class */
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
String name;
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/** Returns the country name corresponding to the selected item */
@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
/** Each item in the autocompetetextview suggestion list is a hashmap object */
HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
for(String key:hm.keySet())
{
name=(String)hm.get("first_name")+" ";
name+=(String)hm.get("last_name")+" ";
name+=(String)hm.get("city");
}
return name;
}
}
/* autocomplate layout*/
<?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="match_parent"
android:orientation="horizontal"
android:background="@color/white"
>
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="2dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="2dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="2dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="2dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:background="@color/white"
/>
</LinearLayout>
答案 1 :(得分:0)
在构建adapter
时,您可以根据需要组合值:
例如:
String[] keywords;
counter =0;
for(i=0; i< palnets.length(); i++)
{
for(j=0; j< colors.length(); j++)
{
keywords[counter] = "Planet" + planets[i] + "Color:" + colors[j] + "Price:" + prices[j];
counter++;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, keywords);
final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
textView.setAdapter(adapter);