previous数据与新输入autocompletetextview [英] previous data is entered along with new autocompletetextview
本文介绍了previous数据与新输入autocompletetextview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有autocomletetextview andit会从服务器的JSON响应输入文本时逐一。它工作正常,但是当你快速输入文本。
它增加了每个字母这是。我已经尝试以下。如何在code我解决这个列表中的最后一个响应的价值?我提前真的AP preciate任何help.Thanks。
BaseActivity:
公共类SimpleAdapter扩展了BaseAdapter {
私人活动的活动;
私人的ArrayList< HashMap的<字符串,字符串>>数据;
私有静态LayoutInflater吹气= NULL;
公共ImageLoader的ImageLoader的;
位图图像;
私人DisplayImageOptions defaultOptions;
公共SimpleAdapter(活动一,ArrayList的< HashMap的<字符串,字符串>> D){
活性= A;
数据= D;
充气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
defaultOptions =新DisplayImageOptions.Builder()建立()。
ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(活动)
.threadPoolSize(2)
.memoryCache(新WeakMemoryCache())
.discCacheFileNameGenerator(新Md5FileNameGenerator())
。建立();
ImageLoader的= ImageLoader.getInstance();
imageLoader.init(配置);
imageLoader.init(ImageLoaderConfiguration.createDefault(活动));
}
公众诠释getCount将(){
返回data.size();
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
如果(convertView == NULL)
VI = inflater.inflate(R.layout.list_row,NULL);
TextView的标题=(TextView中)vi.findViewById(R.id.textView1); // 标题
TextView的艺术家=(TextView中)vi.findViewById(R.id.textView2); //艺术家的名字
TextView的持续时间=(TextView中)vi.findViewById(R.id.textView3); //时间
ImageView的thumb_image =(ImageView的)vi.findViewById(R.id.list_image); //拇指图像
HashMap的<字符串,字符串>歌=新的HashMap<字符串,字符串>();
歌= data.get(位置);
//设置列表视图中的所有值
title.setText(song.get(名字));
artist.setText(song.get(名字));
duration.setText(song.get(时代));
imageLoader.displayImage(song.get(链接),thumb_image,defaultOptions);
返回六;
}
}
MainActivity
公共类AutoCompleteTextViewActivity扩展活动实现TextWatcher {
/ **第一次创建活动时调用。 * /
私人TextView的选择;
私人AutoCompleteTextView ACTV;
SimpleAdapter适配器;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
选择=(TextView中)findViewById(R.id.selection);
ACTV =(AutoCompleteTextView)findViewById(R.id.actv);
actv.addTextChangedListener(本);
}
公共无效afterTextChanged(编辑S){
// TODO自动生成方法存根
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
// TODO自动生成方法存根
}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
建议Suggestdetails =新建议();
如果(ALIST!= NULL){
Suggestdetails.execute();
aList.clear();
} 其他{
listView.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
公共类推荐扩展AsyncTask的<虚空,虚空,JSONObject的> {
@覆盖
受保护的JSONObject doInBackground(空... PARAMS){
字符串ResponseBody = NULL;
尝试 {
//此处geoaddress
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://example.com/suggest.php);
//添加数据
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(1);
nameValuePairs.add(新BasicNameValuePair(姓名,actv.getText()的toString())。);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
//执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost);
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),ISO-8859-1),8。);
StringBuilder的SB =新的StringBuilder();
sb.append(reader.readLine()+\ N);
串行=0;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
reader.close();
字符串结果= sb.toString();
Log.d(FB TR,结果);
//解析数据
返回新的JSONObject(结果);
}赶上(例外五){
e.printStackTrace();
返回null;
}
}
@覆盖
保护无效onPostExecute(JSONObject的结果){
如果(结果!= NULL){
// 做一点事
// JSONObject的TR =结果;
名单< HashMap的<字符串,字符串>> ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
尝试{
JSONArray ARR = result.getJSONArray(阵列);
的for(int i = 0; I< arr.length();我++)
{
的JSONObject E1 = arr.getJSONObject(ⅰ);
JSONObject的JSON =(的JSONObject)e1.get(数据);
字符串名字= json.getString(名字)修剪()。
。字符串姓氏= json.getString(姓氏)修剪();
字符串link = json.getString(链接)修剪()。
字符串时间= json.getString(时代)修剪()。
HashMap的<字符串,字符串> HM =新的HashMap<字符串,字符串>();
hm.put(名字,名字);
hm.put(姓氏,姓氏);
hm.put(链接的链接);
hm.put(时间,时间);
aList.add(HM);
}
}赶上(例外五){
e.printStackTrace();
}
从字符串= {名字,姓氏,时间,链接} [];
INT []到= {R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};
适配器=新SimpleAdapter(AutoCompleteTextViewActivity.this,ALIST,R.layout.list_row,从,到);
actv.setAdapter(适配器);
adapter.notifyDataSetChanged();
} 其他 {
//出错
}
}
}
}
解决方案
您code更新,我希望它运行良好:D [更新]
公共类AutoCompleteTextViewActivity扩展活动实现TextWatcher {
/ **第一次创建活动时调用。 * /
私人TextView的选择;
私人AutoCompleteTextView ACTV;
名单< HashMap的<字符串,字符串>> ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
SimpleAdapter适配器;
私人建议Suggestdetails;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
选择=(TextView中)findViewById(R.id.selection);
ACTV =(AutoCompleteTextView)findViewById(R.id.actv);
从字符串= {名字,姓氏,时间,链接} [];
INT []到= {R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};
适配器=新SimpleAdapter(AutoCompleteTextViewActivity.this,ALIST,R.layout.list_row,从,到);
actv.setAdapter(适配器);
actv.addTextChangedListener(本);
}
公共无效afterTextChanged(编辑S){
// TODO自动生成方法存根
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
// TODO自动生成方法存根
}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(Suggestdetails = NULL和放大器;!&安培; Suggestdetails.getStatus()等于(AsyncTask.Status.RUNNING)){
Suggestdetails.cancel(真正的);
}
Suggestdetails =新建议();
Suggestdetails.execute();
}
公共类推荐扩展AsyncTask的<虚空,虚空,名单,其中,HashMap的<字符串,字符串>>> {
@覆盖
受保护的名单,其中,HashMap的<字符串,字符串>> doInBackground(空... PARAMS){
字符串ResponseBody = NULL;
尝试 {
//此处geoaddress
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://example.com/suggest.php);
//添加数据
名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(1);
nameValuePairs.add(新BasicNameValuePair(姓名,actv.getText()的toString())。);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
//执行HTTP POST请求
HTT presponse响应= httpclient.execute(httppost);
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),ISO-8859-1),8。);
StringBuilder的SB =新的StringBuilder();
sb.append(reader.readLine()+\ N);
串行=0;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
如果(isCancelled())返回NULL;
}
reader.close();
字符串结果= sb.toString();
Log.d(FB TR,结果);
名单< HashMap的<字符串,字符串>> resultList =新的ArrayList< HashMap的<字符串,字符串>>();
如果(isCancelled())返回NULL;
尝试{
JSONObject的resultJson =新的JSONObject的(结果);
JSONArray ARR = resultJson.getJSONArray(阵列);
的for(int i = 0; I< arr.length();我++)
{
的JSONObject E1 = arr.getJSONObject(ⅰ);
JSONObject的JSON =(的JSONObject)e1.get(数据);
字符串名字= json.getString(名字)修剪()。
。字符串姓氏= json.getString(姓氏)修剪();
字符串link = json.getString(链接)修剪()。
字符串时间= json.getString(时代)修剪()。
HashMap的<字符串,字符串> HM =新的HashMap<字符串,字符串>();
hm.put(名字,名字);
hm.put(姓氏,姓氏);
hm.put(链接的链接);
hm.put(时间,时间);
resultList.add(HM);
如果(isCancelled())返回NULL;
}
}赶上(例外五){
e.printStackTrace();
}
返回resultList;
}赶上(例外五){
e.printStackTrace();
}
返回null;
}
@覆盖
保护无效onPostExecute(名单< HashMap的<字符串,字符串>> ...结果){
如果(结果!= NULL){
aList.clear();
aList.addAll(结果[0]);
adapter.notifyDataSetChanged();
} 其他 {
//出错
}
}
}
}
I have autocomletetextview andit gets json response from server .It works fine when text is entered one by one but when you enter text quickly .
It adds the value of each letter which is the last response to the list .I have tried the code below .How do I fix this? I really appreciate any help.Thanks in Advance.
BaseActivity:
public class SimpleAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader ;
Bitmap image;
private DisplayImageOptions defaultOptions;
public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
defaultOptions = new DisplayImageOptions.Builder().build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity)
.threadPoolSize(2)
.memoryCache(new WeakMemoryCache())
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.textView1); // title
TextView artist = (TextView)vi.findViewById(R.id.textView2); // artist name
TextView duration = (TextView)vi.findViewById(R.id.textView3); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get("firstname"));
artist.setText(song.get("lastname"));
duration.setText(song.get("time"));
imageLoader.displayImage(song.get("link"), thumb_image, defaultOptions);
return vi;
}
}
MainActivity
public class AutoCompleteTextViewActivity extends Activity implements TextWatcher{
/** Called when the activity is first created. */
private TextView selection;
private AutoCompleteTextView actv;
SimpleAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
actv=(AutoCompleteTextView)findViewById(R.id.actv);
actv.addTextChangedListener(this);
}
public void afterTextChanged(Editable s) {
// 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) {
Suggest Suggestdetails=new Suggest();
if(aList!=null){
Suggestdetails.execute();
aList.clear();
} else{
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
public class Suggest extends AsyncTask<Void, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Void... params) {
String ResponseBody = null;
try {
//geoaddress here
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/suggest.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name", actv.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
String result = sb.toString();
Log.d("Fb tr",result);
// parsing data
return new JSONObject(result);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(JSONObject result) {
if (result != null) {
// do something
// JSONObject tr = result;
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
try{
JSONArray arr=result.getJSONArray("Array");
for(int i=0;i<arr.length();i++)
{
JSONObject e1 = arr.getJSONObject(i);
JSONObject json= (JSONObject) e1.get("data");
String firstname = json.getString("firstname").trim();
String lastname = json.getString("lastname").trim();
String link = json.getString("link").trim();
String time = json.getString("time").trim();
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("firstname", firstname);
hm.put("lastname", lastname );
hm.put("link", link);
hm.put("time", time);
aList.add(hm);
}
}catch(Exception e){
e.printStackTrace();
}
String[] from = { "firstname","lastname","time","link"};
int[] to = { R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};
adapter = new SimpleAdapter(AutoCompleteTextViewActivity.this,aList, R.layout.list_row,from,to);
actv.setAdapter(adapter);
adapter.notifyDataSetChanged();
} else {
// error occured
}
}
}
}
解决方案
your code updated, I hope it run well :D [Updated]
public class AutoCompleteTextViewActivity extends Activity implements TextWatcher {
/** Called when the activity is first created. */
private TextView selection;
private AutoCompleteTextView actv;
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter;
private Suggest Suggestdetails;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
actv=(AutoCompleteTextView)findViewById(R.id.actv);
String[] from = { "firstname","lastname","time","link"};
int[] to = { R.id.textView1,R.id.textView2,R.id.textView3,R.id.list_image};
adapter = new SimpleAdapter(AutoCompleteTextViewActivity.this,aList, R.layout.list_row,from,to);
actv.setAdapter(adapter);
actv.addTextChangedListener(this);
}
public void afterTextChanged(Editable s) {
// 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) {
if (Suggestdetails!=null && Suggestdetails.getStatus().equals(AsyncTask.Status.RUNNING)){
Suggestdetails.cancel(true);
}
Suggestdetails=new Suggest();
Suggestdetails.execute();
}
public class Suggest extends AsyncTask<Void, Void, List<HashMap<String,String>>> {
@Override
protected List<HashMap<String,String>> doInBackground(Void... params) {
String ResponseBody = null;
try {
//geoaddress here
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/suggest.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name", actv.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
if (isCancelled()) return null;
}
reader.close();
String result = sb.toString();
Log.d("Fb tr",result);
List<HashMap<String,String>> resultList = new ArrayList<HashMap<String,String>>();
if (isCancelled()) return null;
try{
JSONObject resultJson = new JSONObject(result);
JSONArray arr=resultJson.getJSONArray("Array");
for(int i=0;i<arr.length();i++)
{
JSONObject e1 = arr.getJSONObject(i);
JSONObject json= (JSONObject) e1.get("data");
String firstname = json.getString("firstname").trim();
String lastname = json.getString("lastname").trim();
String link = json.getString("link").trim();
String time = json.getString("time").trim();
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("firstname", firstname);
hm.put("lastname", lastname );
hm.put("link", link);
hm.put("time", time);
resultList.add(hm);
if (isCancelled()) return null;
}
}catch(Exception e){
e.printStackTrace();
}
return resultList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<HashMap<String,String>>... result) {
if (result != null) {
aList.clear();
aList.addAll(result[0]);
adapter.notifyDataSetChanged();
} else {
// error occured
}
}
}
}
这篇关于previous数据与新输入autocompletetextview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文