2015-10-09 24 views
0

我使用String [] {“item 1”,“item 2”,“item 3”}作为将在我的ListView中显示的arrayadapter,但现在的问题是I不能过滤搜索目的。大多数在线教程都被过滤并从数据库中获取,但对于我的情况,我根本不触及数据库,所有这些都只是硬编码。对不起,我的英语和帮助,请:)如下如何使用searchManager搜索和过滤listview

代码: MainActivity.java:

String[] values = new String[] { "item 1", "item 2", "item 3" } ; 
ItemAdapter adapter = new ItemAdapter(this, values); 
setListAdapter(adapter); 

Adapter.java代码如下

public class BuildingAdapter extends ArrayAdapter<String> { 
private final Context context; 
private final String[] buildingname; 

    public BuildingAdapter(Context context, String[] buildingname) { 
    super(context, R.layout.row_buidling_item, buildingname); 
    this.context = context; 
    this.buildingname = buildingname; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.row_buidling_item, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.buildingname); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.buildingicon); 
    TextView hints = (TextView) rowView.findViewById(R.id.buildinghints); 
    textView.setText(buildingname[position]); 
    // Change the icon for Windows and iPhone 
    String s = buildingname[position]; 
    return rowView; 
} 




//that's all I did, hopefully someone can answer please... 
+0

如果我正确地低估了,你有2个Array,Array1和Array2,你想获得列表Array1 - Array2? –

+0

@RohitJain先生,我只有一个数组是“buildingname”。 – MellisaNg

回答

0

Arrary适配器的ListView

// ArrayList for Listview 
ArrayList<HashMap<String, String>> productList; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Listview Data 
    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE", 
          "iPhone 4S", "Samsung Galaxy Note 800", 
          "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"}; 

    lv = (ListView) findViewById(R.id.list_view); 
    inputSearch = (EditText) findViewById(R.id.inputSearch); 

    // Adding items to listview 
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products); 
    lv.setAdapter(adapter);  

} 

启用搜索功能

inputSearch.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
     // When user changed the Text 
     MainActivity.this.adapter.getFilter().filter(cs); 
    } 

    @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       
    } 
}); 

最后,在您的AndroidManifest.xml文件中添加以下属性以在加载Activity时隐藏键盘。

android:windowSoftInputMode="stateHidden" 

refference

+0

谢谢先生:)这真的解决了我的问题,但我现在又遇到了另一个问题,再次抱歉打扰.. 该项目在列表视图中很好地显示,但我需要在列表视图中显示不同的项目。我该怎么做?我的照片现在全部位于Resource内的Raw文件夹中 对不起,因为我的英文不好 – MellisaNg

+0

与自定义列表适配器一起使用..for refference..go通过thisk链接http://www.learn2crack.com/2013/10/android-custom -listview图像,文本example.html的 –

0

创建您的活动两个领域

String[] values; 
ArrayList<String> filteredValues; 

现在将值分配给适配器,如:

values = new String[]{"item 1", "item 2", "item 3"}; 
    filteredValues = new ArrayList<>(values.length); 
    for (int i = 0; i < values.length; i++) { 
     filteredValues.add(values[i]); 
    } 
    ItemAdapter adapter = new ItemAdapter(this, values); 
    setListAdapter(adapter); 

现在定义它调用过滤过滤方法:

private void filter(String s) { 
    filteredValues.clear(); 
    for (int i = 0; i < values.length; i++) { 
     if (TextUtils.isEmpty(s) || values[i].contains(s)) { 
      filteredValues.add(values[i]); 
     } 
    } 
    //notifyDatasetChange of adapter here; 
    }