当前位置: 首页 > 面试题库 >

如何用listview实现autocompletetextview?

锺离烈
2023-03-14
问题内容

我从服务器获取响应并使用listview显示它,并且工作正常,现在我正在尝试添加自动完成文本视图以按名称搜索项目,但是当我运行我的应用程序时,它崩溃并显示错误..我已经在问这个

Tab1Activity.java

   public class Tab1Activity  extends ListActivity{


private ProgressDialog pDialog;
JSONArray Product=null;
private ListView listview;
ArrayList<HashMap<String,String>> aList;

ArrayList<HashMap<String, String>> arrayTemplist;
private static String PRODUCT_URL = "";
private static final String PRODUCT_DATA="Product";
private static final String PRODUCT_ID="productid";
private static final String PRODUCT_NAME="product_name";
private static final String PRODUCT_CODE="skucode";
private static final String PRODUCT_IMAGE="product_photo";
private static final String PRODUCT_WEIGHT="weight";
private static final String PRODUCT_SALERATE="sale_rate";
//private static final String PRODUCT_LOCATION="weight";
private CustomAdapterTabone adapter;
private TextView noacpt;
private AutoCompleteTextView inputSearch;

@Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_tabone);
    //noacpt=(TextView)findViewById(R.id.no_acceptedlist);
    /*String strtext = getIntent().getStringExtra("id");
System.out.println("<<<<<<<< id : " + strtext);*/

    final ListView listview = (ListView)findViewById(android.R.id.list);
    //listview.setSelector( R.drawable.list_selector);

    //listview.setSelector(R.drawable.listselector);
    new LoadAlbums().execute();

    aList = new ArrayList<HashMap<String, String>>();
    final ListView lv = getListView();

    inputSearch = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

    inputSearch.addTextChangedListener(new TextWatcher() {


        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            arrayTemplist= new ArrayList<HashMap<String,String>>();
            String searchString =inputSearch.getText().toString().toLowerCase();

            for (int i = 0; i < aList.size(); i++)
            {
                String currentString =aList.get(i).get(Tab1Activity.PRODUCT_NAME);
                if (currentString.toLowerCase().startsWith(searchString ))
                {
                    arrayTemplist.add(aList.get(i));
                }
            }

            for (int i = 0; i < arrayTemplist.size(); i++)
            {
                String currentstrin = arrayTemplist.get(i).get(Tab1Activity.PRODUCT_NAME);
                //Toast.makeText(getApplicationContext(), currentstrin, Toast.LENGTH_LONG).show();

            }

            adapter=new CustomAdapterTabone(getApplicationContext(), arrayTemplist);
            listview.setAdapter(adapter);

    /*  SimpleAdapter adapters = new SimpleAdapter(Tab1Activity.this, arrayTemplist,R.layout.list_item_tabone, new String[] { PRODUCT_NAME
            }, new int[] {
                    R.id.txtpronameacptedlist});
            lv.setAdapter(adapters);
            */
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                         
        }
    });


  }

    @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getApplicationContext(), AllProductDetails.class);
intent.putExtra("productid", aList.get(position).get(PRODUCT_ID));
startActivity(intent);
 }

  class LoadAlbums extends AsyncTask<String, String,   ArrayList<HashMap<String,String>>> {
/**
 * Before starting background thread Show Progress Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(Tab1Activity.this);
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(true);
   // pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
    pDialog.setCancelable(false);
    pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
    ServiceHandler sh = new ServiceHandler();
    // Making a request to url and getting response
    ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
    String jsonStr = sh.makeServiceCall(PRODUCT_URL, ServiceHandler.GET);

    Log.d("Response: ", "> " + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            Product = jsonObj.getJSONArray(PRODUCT_DATA);

            for (int i = 0; i < Product.length(); i++) {
                JSONObject c = Product.getJSONObject(i);
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                // adding each child node to HashMap key => value
                map.put(PRODUCT_ID, c.getString(PRODUCT_ID));
                map.put(PRODUCT_NAME,c.getString(PRODUCT_NAME));
                map.put(PRODUCT_CODE, c.getString(PRODUCT_CODE));
                map.put(PRODUCT_IMAGE, c.getString(PRODUCT_IMAGE));
                map.put(PRODUCT_WEIGHT, c.getString(PRODUCT_WEIGHT));
                map.put(PRODUCT_SALERATE, c.getString(PRODUCT_SALERATE)+getResources().getString(R.string.rupee));
               // map.put(INTEREST_ACCEPT_LOCATION, c.getString(INTEREST_ACCEPT_LOCATION));
                // adding HashList to ArrayList
                data.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }
    return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
    super.onPostExecute(result);

    // dismiss the dialog after getting all albums
    if (pDialog.isShowing())
        pDialog.dismiss();
    // updating UI from Background Thread

        aList = new ArrayList<HashMap<String, String>>();
        aList.addAll(result);
        adapter = new CustomAdapterTabone(getApplicationContext(),result);
        setListAdapter(adapter);

        aList.addAll(result);
        adapter.notifyDataSetChanged();



}

}

自定义适配器

  public class CustomAdapterTabone extends BaseAdapter{


private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;

private static final String TAG_NAME="product_name";
private static final String TAG_PROFILE="skucode";
private static final String TAG_IMAGE="product_photo";
private static final String TAG_CAST="weight";
private static final String TAG_AGE="sale_rate";
  // private static final String TAG_LOCATION="weight";

public CustomAdapterTabone(Context context,ArrayList<HashMap<String,String>> listData) {
    this.context = context;
    this.listData=listData;
    aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_tabone, null);
        holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronameacptedlist);
        holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastacptedlist);
        holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
       // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtproname.setText(listData.get(position).get(TAG_NAME));
    holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
    holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
    holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
    //holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

    aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);
    // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
    return convertView;
}
class ViewHolder{
    ImageView propic;
    TextView txtproname;
    TextView txtproid;
    TextView txtprofilecast;
    TextView txtprofileage;
    TextView txtprofileplace;
}
}

问题答案:

首先创建一个实现可过滤的自定义适配器:

public class MyFilterableAdapter extends BaseAdapter implements Filterable {
    private Context context;
    private List<String> items;
    private List<String> filteredItems;
    private ItemFilter mFilter = new ItemFilter();

    public MyFilterableAdapter(Context context, List<String> items) {
        //super(context, R.layout.your_row, items);
        this.context = context;
        this.items = items;
        this.filteredItems = items;
    }

    @Override
    public int getCount() {
        return filteredItems.size();
    }

    @Override
    public Object getItem(int position) {
        return filteredItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.row_search_merchant, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String location = filteredItems.get(position);
        if (!location.isEmpty() || viewHolder != null) {
            viewHolder.tvTitle.setText(location);
        }
        return convertView;
    }

    public static class ViewHolder {
        TextView tvTitle;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            String filterString = constraint.toString().toLowerCase();
            FilterResults results = new FilterResults();

            int count = items.size();
            final List<String> tempItems = new ArrayList<>(count);

            for (int i = 0; i < count; i++) {
                if (items.get(i).toLowerCase().contains(filterString)) {
                    tempItems.add(items.get(i));
                }
            }

            results.values = tempItems;
            results.count = tempItems.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredItems = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }
    }

    public Filter getFilter() {
        return mFilter;
    }
}

而不是创建一个文本监视器

public class MyTextWatcher implements TextWatcher {
    private MyCustomAdapter lAdapter;

    public MyTextWatcher(MyCustomAdapter lAdapter) {
        this.lAdapter = lAdapter;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        lAdapter.getFilter().filter(s.toString().toLowerCase());
    }
}

最后将您的TextWatcher添加到您的搜索编辑文本中:

final MyCustomAdapter lAdapter = new MyCustomAdapter(context, items);
EditText etSearch = (EditText) view.findViewById(R.id.et_search);
        etSearch.addTextChangedListener(new SearchTextWatcher(lAdapter));

希望对您有帮助。



 类似资料:
  • 问题内容: 我想在textview之前添加图片,并希望在每一行中自定义TextView,但是我很难实现它,因为已经内置了xml这样的布局文件 simple_list_item_1 。请帮助我如何实施它。 这是simple_list_item编码 主要活动 这里是 twit_list.xml 附带 MainActivity.java 问题答案: 创建一个内部布局文件夹 创建一个这样的自定义适配器 从

  • 当我用RobolectRic2.2对我的ListActivity运行JUnit测试时,我得到一个InflateException表示ListView未实现。我该怎么绕过这件事?我一直在研究如何使用.shadowof(),但我不确定如何做到这一点。

  • 问题内容: 我想要一个带有“人”名称的ListView,但是在启动应用程序时,在ListView上,我看到了一些奇怪的字符串而不是(字符串)名称。我认为这是“人”对象的强制问题。我不知道ListView应该是ListView还是ListView。因为当我尝试在listview上设置项目时,使用setItems(ObservableList)可以得到显示在列表上的怪异字符串。.这是代码: MainA

  • 我最近尝试为ListView创建一个自定义行。我试图从教程中完成它,但当我试图更改它时,它崩溃了,以便行与我希望我的应用程序显示的内容一起工作。错误读出在底部。 --------homescreen.xml--------------------- ----------读出错误--------------------------------------------- 04-07 14:06:40.

  • 本文向大家介绍Android ListView分页简单实现,包括了Android ListView分页简单实现的使用技巧和注意事项,需要的朋友参考一下 Android ListView分页简单实现 分页,开发应用中必不可少。那么,现在就来实现分页功能。 首先来想想实现它要有哪些步骤, 1, 实现的组件, 2、初始化第一页数据, 3,底部布局 , 4,加载数据的条件 5、获取下一页的数据。 有了思路