-1
我正在學習Autocompetetextview以從Web服務獲取建議。爲此我使用這tutorial。他們的代碼中唯一的問題是他們已經使用MainActivity將自定義的適配器,自定義的autocompletetextview和本地數據庫鏈接到自定義的textwatcher中。我不想這樣做,因爲我想在多個類中使用自定義的autocompletetextview(除了MainActivity類)。所以,我想知道是否可以在不使用MainActivity作爲對象的情況下獲取這3件東西。將自定義適配器,自定義自動完成文本視圖和數據庫添加到自定義的textwatcher
public class MainActivity extends AppCompatActivity {
CustomAutoCompleteView myAutoComplete;
// adapter for auto-complete
ArrayAdapter<MyObject> myAdapter;
// for database operations
DatabaseHandler databaseH;
private static final String LOCATION = "http://example.com/android/location";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
// instantiate database handler
databaseH = new DatabaseHandler(MainActivity.this);
// put sample data to database
insertSampleData();
// autocompletetextview is in activity_main.xml
myAutoComplete = (CustomAutoCompleteView) 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));
// ObjectItemData has no value at first
MyObject[] ObjectItemData = new MyObject[0];
// set the custom ArrayAdapter
myAdapter = new AutocompleteCustomArrayAdapter(this, R.layout.list_view_row, ObjectItemData);
myAutoComplete.setAdapter(myAdapter);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void insertSampleData(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOCATION,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
try {
JSONArray jsonarray = new JSONArray(response);
for (int i = 0; i < jsonarray.length(); i++) {
String strValue = jsonarray.getString(i);
databaseH.create(new MyObject(strValue));
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("phrase", myAutoComplete.getText().toString());
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
}
public class CustomAutoCompleteTextChangedListener implements TextWatcher {
public static final String TAG = "AutoTextChangedListener";
Context context;
AutocompleteCustomArrayAdapter myAdapter;
DatabaseHandler databaseH;
CustomAutoCompleteView myAutoComplete;
public CustomAutoCompleteTextChangedListener(Context context){
this.context = context;
}
@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
}
@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
Log.e(TAG, "User input: " + userInput);
MainActivity mainActivity = ((MainActivity) context);
// These below 4 lines should be linked without "mainActivity" object.
mainActivity.myAdapter.notifyDataSetChanged();
MyObject[] myObjs = mainActivity.databaseH.read(userInput.toString());
mainActivity.myAdapter = new AutocompleteCustomArrayAdapter(mainActivity, R.layout.list_view_row, myObjs);
mainActivity.myAutoComplete.setAdapter(mainActivity.myAdapter);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
。
不,不,不,這是完全錯誤的:你不需要任何'TextWatcher's,沒有自定義'ArrayAdapter's,沒有自定義'AutoCompleteTextView's,請參閱[這] (http://stackoverflow.com/a/19860624/2252830)而不是 – pskink
什麼是String [] from和int []到這裏? @pskink – Sammy
我不明白。什麼通過?我只需要向ACTV添加數據列表 – Sammy