Android 在下拉列表中显示重复记录(基本上执行来自previus http调用的数据)
我试图在我的应用程序中执行像facebook这样的搜索好友。但是数据会重复/重复,并且当快速键入时,显示的内容会发生变化 如何防止数据重复?如何使用新值刷新列表、适配器和下拉列表?我已尝试了许多可能的方法,但数据仍在重复。请提供解决方案。我非常感谢您的帮助。提前感谢 基本活动Android 在下拉列表中显示重复记录(基本上执行来自previus http调用的数据),android,Android,我试图在我的应用程序中执行像facebook这样的搜索好友。但是数据会重复/重复,并且当快速键入时,显示的内容会发生变化 如何防止数据重复?如何使用新值刷新列表、适配器和下拉列表?我已尝试了许多可能的方法,但数据仍在重复。请提供解决方案。我非常感谢您的帮助。提前感谢 基本活动 public class SimpleAdapter extends BaseAdapter { private Activity activity; private ArrayList<HashM
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;
}
}
公共类SimpleAdapter扩展了BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共图像加载器;
位图图像;
私有DisplayImageOptions默认选项;
公共SimpleAdapter(活动a,ArrayList d){
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
defaultOptions=新建DisplayImageOptions.Builder().build();
ImageLoaderConfiguration配置=新建ImageLoaderConfiguration.Builder(活动)
.线程池大小(2)
.memoryCache(新的WeakMemoryCache())
.discCacheFileNameGenerator(新的Md5FileNameGenerator())
.build();
imageLoader=imageLoader.getInstance();
init(config);
init(ImageLoaderConfiguration.createDefault(活动));
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.list_行,空);
TextView title=(TextView)vi.findViewById(R.id.textView1);//title
TextView艺术家=(TextView)vi.findViewById(R.id.textView2);//艺术家姓名
TextView持续时间=(TextView)vi.findViewById(R.id.textView3);//持续时间
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);//thumb image
HashMap宋=新HashMap();
宋=数据。获取(位置);
//在listview中设置所有值
title.setText(song.get(“firstname”);
Artister.setText(song.get(“lastname”);
duration.setText(song.get(“time”));
imageLoader.displayImage(song.get(“link”)、thumb\u image、defaultOptions);
返回vi;
}
}
主要活动
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
}
}
}
}
公共类AutoCompleteTextViewActivity扩展活动实现TextWatcher{
/**在首次创建活动时调用*/
私有文本视图选择;
私有自动完成文本视图actv;
SimpleAdapter适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
actv=(AutoCompleteTextView)findviewbyd(R.id.actv);
actv.addTextChangedListener(本);
}
公共无效后文本已更改(可编辑){
//TODO自动生成的方法存根
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
//TODO自动生成的方法存根
}
public void onTextChanged(字符序列、int start、int before、int count){
建议Suggestdetails=新建议();
if(aList!=null){
Suggestdetails.execute();
a.clear();
}否则{
setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
公共类任务{
@凌驾
受保护的JSONObject doInBackground(无效…参数){
字符串ResponseBody=null;
试一试{
//这里的地理地址
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://example.com/suggest.php");
//添加您的数据
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“name”,actv.getText().toString());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
BufferedReader=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent(),“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
reader.close();
字符串结果=sb.toString();
Log.d(“Fb tr”,结果);
//解析数据
返回新的JSONObject(结果);
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(JSONObject结果){
如果(结果!=null){
//做点什么
//JSONObject tr=结果;
列表列表=新的ArrayList();
试一试{
JSONArray arr=result.getJSONArray(“数组”);
对于(int i=0;如果从您的代码中,我看不到列表中存在重复值的任何原因。您确定服务器没有创建重复值吗?否。我正在使用like
语句。此处从名为“$text%”的表中选择*
。请让我知道我正在做什么