因此我设计了这个活动,来自webservice的我的数据正常工作,但我无法获得下拉列表。在这里我将arraylist转换为String数组,以便在autocomplete中显示结果.Kindly帮助我。在此先感谢。
文件:proprety.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:background="@drawable/bg"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/menubar12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:background="@drawable/property_header"
android:gravity="left|center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="@drawable/home"
android:onClick="myclick_home" />
<LinearLayout
android:id="@+id/menubar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/logout"
android:onClick="myclick_logout" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/et_propertyact_propertyname"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/property_text"
android:cursorVisible="true"
android:ems="10"
android:hint="Property Name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textSize="14sp"
android:textStyle="bold" >
<requestFocus />
</AutoCompleteTextView>
<AutoCompleteTextView
android:id="@+id/et_propertyact_blockname"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/block_text"
android:cursorVisible="true"
android:ems="10"
android:hint="Block Name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textSize="14sp"
android:textStyle="bold" >
</AutoCompleteTextView>
<AutoCompleteTextView
android:id="@+id/et_propertyact_appartmentname"
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/apartment"
android:cursorVisible="true"
android:ems="10"
android:hint="Apartment Name"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textColorHint="#ffffff"
android:textSize="14sp"
android:textStyle="bold" >
</AutoCompleteTextView>
<LinearLayout
android:id="@+id/bg_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="@+id/btn_propertyact_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/view_button" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ListView
android:id="@+id/property_listView"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="6dp"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:clickable="true"
android:divider="@drawable/cellborder" android:dividerHeight="1dp" android:focusable="true" >
</ListView>
</LinearLayout>
文件:PropertyActivity.Java
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import com.example.webservice.JSONfunction_JSONArray;
import com.example.webservice.JSONfunction_JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.SimpleAdapter;
public class PropertyActivity extends Activity {
/* ====Components Declaration Part Begins here======= */
private ListView property_listView;
private ListView list_property;
private Button btn_view;
private AutoCompleteTextView propertyname, blockname, apartmentname;
private static final String TAG = "AppSquare";
/* ====ServiceVariables Declarations Part ======= */
JSONArray j_array,j_array1;
JSONObject j_obj,j_obj1;
ArrayList<HashMap<String, String>> arrList_proprty_list = new ArrayList<HashMap<String, String>>();
public static ArrayList<String> arrList_property_names = new ArrayList<String>();
public static ArrayList<String> arrList_block_names = new ArrayList<String>();
public static ArrayList<String> arrList_apartment_names = new ArrayList<String>();
public static ArrayList<String> arrList_property_names_id= new ArrayList<String>();
public static ArrayList<String> arrList_block_names_id= new ArrayList<String>();
public static ArrayList<String> arrList_apartment_names_id= new ArrayList<String>();
private ArrayAdapter<String> PropAdapter;
private ArrayAdapter<String> BlockAdapter;
private ArrayAdapter<String> ApartmentAdapter;
public static String[] arr_Prop_name;
public static String[] arr_block_name;
public static String[] arr_apartment_name;
/* ====API Declarations Part ======= */
public static final String ip = "192.168.0.6";
String API_propSelect = "http://" + ip + "/APP2/propertySelect.php";
String API_blockSelect = "http://" + ip + "/APP2/blockSelect.php";
String API_apartmentSelect = "http://" + ip + "/APP2/apartmentSelect.php";
String API_propDetails = "http://" + ip + "/App2/propertyDetails.php";
/* ====OnClick Declarations Part ======= */
/* ====OnClick Definition Part Begins here======= */
public void myclick_logout(View v) {
AlertDialog.Builder builder2 = new AlertDialog.Builder(
PropertyActivity.this);
builder2.setTitle("Warning");
builder2.setMessage("Closing Application..");
builder2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// finish();
// System.exit(0);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
finish();
}
});
builder2.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder2.show();
}
public void myclick_home(View v) {
startActivity(new Intent(PropertyActivity.this, DashBoardActivity.class));
}
/* ====OnCreate Definition Part Begins here======= */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.property_activity);
getPrefernces();
new PropName().execute();
new BlockName().execute();
new ApartmentName().execute();
setValue_AutoComplete();
Viewbtn_action();
}
public void getPrefernces()
{
property_listView = (ListView) findViewById(R.id.property_listView);
propertyname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_propertyname);
blockname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_blockname);
apartmentname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_appartmentname);
btn_view = (Button) findViewById(R.id.btn_propertyact_view);
}
public void setValue_AutoComplete()
{
/*-------Load Data into PropertList-----*/
arr_Prop_name = arrList_property_names.toArray(new String[arrList_property_names.size()]);
PropAdapter = new ArrayAdapter<String>(PropertyActivity.this,android.R.layout.simple_dropdown_item_1line,arr_Prop_name);
propertyname.setAdapter(PropAdapter);
propertyname.getDropDownBackground().setAlpha(255);
propertyname.setThreshold(1);
/*------Load Data into Block List------*/
arr_block_name = arrList_block_names.toArray(new String[arrList_block_names.size()]);
BlockAdapter = new ArrayAdapter<String>(PropertyActivity.this,android.R.layout.simple_dropdown_item_1line,arr_block_name);
blockname.setAdapter(BlockAdapter);
blockname.getDropDownBackground().setAlpha(255);
blockname.setThreshold(1);
/*------Load Data into apartment List------*/
arr_apartment_name = arrList_apartment_names.toArray(new String[arrList_apartment_names.size()]);
ApartmentAdapter = new ArrayAdapter<String>(PropertyActivity.this,android.R.layout.simple_dropdown_item_1line,arr_apartment_name);
apartmentname.setAdapter(ApartmentAdapter);
apartmentname.getDropDownBackground().setAlpha(255);
apartmentname.setThreshold(1);
}
private void Viewbtn_action() {
btn_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
class PropName extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
System.out.println("In Background...PropName");
System.out.println(API_propSelect);
j_obj=JSONfunction_JSONObject.getJSONfromURL(API_propSelect);
Log.d(TAG, "JSON_RES" + j_obj);
j_array = j_obj.getJSONArray("property");
for (int i = 0; i < j_array.length(); i++)
{
j_obj1 = j_array.getJSONObject(i);
String id= j_obj1.getString("Id");
String value=j_obj1.getString("Value");
System.out.println("Id------>"+id);
System.out.println("Value------>"+value);
//arrList_property_names_id.add(id);
arrList_property_names.add(value);
//System.out.println("Value in array list------>"+arrList_property_names_id);
System.out.println("Value in array list------>"+arrList_property_names);
}
} catch (Exception e) {
Log.d("Error", "Error in API" + e.getStackTrace().toString());
}
return "";
}
}
class BlockName extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
System.out.println("In Background...BlockName");
System.out.println(API_blockSelect);
j_obj=JSONfunction_JSONObject.getJSONfromURL(API_blockSelect);
Log.d(TAG, "JSON_RES" + j_obj);
j_array = j_obj.getJSONArray("apartment");
for (int i = 0; i < j_array.length(); i++)
{
j_obj1 = j_array.getJSONObject(i);
String id= j_obj1.getString("Id");
String value=j_obj1.getString("Value");
System.out.println("Id------>"+id);
System.out.println("Value------>"+value);
//arrList_block_names_id.add(id);
arrList_block_names.add(value);
//System.out.println("Value in array list------>"+arrList_block_names_id);
System.out.println("Value in array list------>"+arrList_block_names);
}
} catch (Exception e) {
Log.d("Error", "Error in API" + e.getStackTrace().toString());
}
return "";
}
}
class ApartmentName extends AsyncTask<Void, Void, String>
{
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try{
System.out.println("In Background...ApartmentName");
System.out.println(API_apartmentSelect);
j_obj=JSONfunction_JSONObject.getJSONfromURL(API_apartmentSelect);
Log.d(TAG, "JSON_RES" + j_obj);
j_array = j_obj.getJSONArray("block");
for (int i = 0; i < j_array.length(); i++)
{
j_obj1 = j_array.getJSONObject(i);
String id= j_obj1.getString("Id");
String value=j_obj1.getString("Value");
System.out.println("Id------>"+id);
System.out.println("Value------>"+value);
//arrList_apartment_names_id.add(id);
arrList_apartment_names.add(value);
//System.out.println("Value in array list------>"+arrList_apartment_names_id);
System.out.println("Value in array list------>"+arrList_apartment_names);
}
} catch (Exception e) {
Log.d("Error", "Error in API" + e.getStackTrace().toString());
}
return "";
}
}
}
答案 0 :(得分:2)
终于得到了答案 1.实施Texwatcher 2.重写OnTextChanged(); 多数民众赞成......
public class PropertyActivity extends Activity implements TextWatcher{
@Override
protected void onCreate(Bundle savedInstanceState) {
propertyname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_propertyname);
propertyname.addTextChangedListener(this);
blockname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_blockname);
blockname.addTextChangedListener(this);
apartmentname = (AutoCompleteTextView) findViewById(R.id.et_propertyact_appartmentname);
apartmentname.addTextChangedListener(this);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
PropAdapter = new ArrayAdapter<String>(PropertyActivity.this,R.layout.my_autolist_item,arr_Prop_name);
// propertyname.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
propertyname.getDropDownBackground().setAlpha(255);
propertyname.setThreshold(1);
propertyname.setAdapter(PropAdapter);
BlockAdapter = new ArrayAdapter<String>(PropertyActivity.this,R.layout.my_autolist_item,arr_block_name);
// blockname.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
blockname.getDropDownBackground().setAlpha(255);
blockname.setThreshold(1);
blockname.setAdapter(BlockAdapter);
ApartmentAdapter = new ArrayAdapter<String>(PropertyActivity.this,R.layout.my_autolist_item,arr_apartment_name);
// apartmentname.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
apartmentname.getDropDownBackground().setAlpha(255);
apartmentname.setThreshold(1);
apartmentname.setAdapter(ApartmentAdapter);
}
}