我有一个带有 MultiAutoCompleteTextView 的应用程序,我需要将结果设置为来自 Web 源 JSON/XML 或任何格式,我该如何执行它,或者更清楚我在创建自己的自定义适配器时需要更改什么自动完成?
问问题
2830 次
1 回答
2
好的,我在这里找到了一种方法:
这是我的布局:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="135dp"
android:text="Button" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:completionThreshold="1"
android:ems="10" >
<requestFocus />
</MultiAutoCompleteTextView>
</RelativeLayout>
这是主要活动,这是从linkedin JSON获取技能的示例
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends Activity {
public MultiAutoCompleteTextView MultiAutocomplete1;
public String data;
public List<String> suggest;
public ArrayAdapter<String> aAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
suggest = new ArrayList<String>();
// multiAutoCompleteTextView1
MultiAutocomplete1 = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
// MultiAutocomplete1.setAdapter(adapter);
MultiAutocomplete1
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
MultiAutocomplete1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String newText = s.toString();
String[] parts = newText.split(",");
if(parts[parts.length -1].length() > 1){
new getJson().execute( parts[parts.length -1] );
}
}
});
// button1
final Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(
MainActivity.this,
String.valueOf("Your Input : "
+ MultiAutocomplete1.getText().toString()),
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class getJson extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... key) {
String newText = key[0];
newText = newText.trim();
newText = newText.replace(" ", "+");
try {
HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("http://www.linkedin.com/ta/skill?query="+newText);
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);
suggest = new ArrayList<String>();
JSONObject jobj = new JSONObject(data);
JSONArray jArray = jobj.getJSONArray("resultList");
for (int i = 0; i < jArray.length(); i++) {
String SuggestKey = jArray.getJSONObject(i).getString("displayName");
suggest.add(SuggestKey);
}
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
runOnUiThread(new Runnable() {
public void run() {
aAdapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_dropdown_item_1line,
suggest);
MultiAutocomplete1.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
}
}
于 2012-11-02T09:26:41.330 回答