问题
I have an AutoCompleteTextView
that I need to suggest names from a MySQL
table. I have only once used this before with hard coding a string array
.
I have only seen one example, at this link, and it really wasn't much of a help. I need to do this with an AsyncTask
. I know how to handle the AsynTask
portion, but here are my questions:
I'm going to have to send each part of the phrase typed as a param
for the task
. Where do I get this text string
?
The only method I can see is a TextWatcher
maybe. Would that be it, or is there another method?
回答1:
Would that be it, or is there another method?
You simply need some mechanism that will "watch" changes in your inputbox and the most correct way how to achieve it is mentioned TextWatcher
So implement it and in some method that provides TextWatcher
for instance onTextChanged(), assign data from inputbox and send them as parameter to AsyncTask and in onPostExecute() method create new Adapter for your AutoCompleteTextView
with data retrieved from MySQL
and assign Adapter to your widget and you got it.
Pseudo-code:
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 1) {
insertString = s.toString();
new YourTask().execute(insertString);
}
}
and in your AsyncTask, perform something like this:
protected List<String> doInBackground() {
// fetchning data from MySQL
return list;
}
public void onPostExecute(List<String> result) {
if (!result.isEmpty()) {
SomeAdapter adp = new SomeAdapter(context, layout, result);
actv.setAdapter(adp);
}
}
Note: In your case is easer to make your AsyncTask
inner class of your Activity
class and you have direct access to UI
components without passing them via constructor.
回答2:
AutoCompleteTextView
already handles this for you.
Basically, by default, when it determinates that the list need to be filtered it calls the performFiltering
method.
So, if you want to reuse the existing logic, just create a AutoCompleteTextView
subclass which overrided the performFiltering
method and put there the code for perform the filtering (in your case the AsyncTask call). Something like this:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
public class MyAutoCompleteTextView extends AutoCompleteTextView {
public MyAutoCompleteTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void performFiltering(CharSequence text, int keyCode) {
//Here the AutompleteTextView has determined that the list should be filtered
}
}
回答3:
How to Add AsynTask Bellow code
public class MainActivity extends Activity {
AutoCompleteTextView txtSearch;
PeopleAdapter adapter;
List<People> mList;
TextView idd;
ImageView imgspn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = retrievePeople();
txtSearch = (AutoCompleteTextView) findViewById(R.id.txt_search);
adapter = new PeopleAdapter(this, R.layout.activity_main, R.id.lbl_name, mList);
txtSearch.setThreshold(1);
txtSearch.setAdapter(adapter);
idd = (TextView) findViewById(R.id.idd);
imgspn = (ImageView) findViewById(R.id.imgspn);
imgspn.setVisibility(View.INVISIBLE);
txtSearch.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String Name = mList.get(position).getId();
idd.setText(Name);
}
});
}
private List<People> retrievePeople() {
List<People> list = new ArrayList<People>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/stud", "root",
"Windouspass");
Statement st = (Statement) con.createStatement();
ResultSet rs = (ResultSet) st.executeQuery(
"select name,designation_name,id from umdlv_users ut,t_designation td where ut.designation_id = td.designation_id");
while (rs.next()) {
list.add(new People(rs.getString(1), rs.getString(2), rs.getString(3)));
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
}
return list;
}
}
来源:https://stackoverflow.com/questions/16108185/autocompletetextview-with-mysql-data