我能够创建一个数据库并用一些食物填充它!并在gridview中显示它们看看screenshot of my app
我想知道如何让搜索视图工作,这样当我搜索食物时,gridview也会更新!!!
第二个问题是:我为gridview中的每个子项实现了OnItemClickListener
公共类FoodList扩展了AppCompatActivity {
GridView gridView;
static ArrayList<Food> list;
FoodListAdapter adapter = null;
SearchView sv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food_list_activity);
gridView = (GridView) findViewById(R.id.gridView);
list = new ArrayList<>();
adapter = new FoodListAdapter(this, R.layout.food_items, list);
gridView.setAdapter(adapter);
// get all data from sqlite
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD ");
list.clear();
while (cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte[] image = cursor.getBlob(3);
list.add(new Food(name, price, image, id));
}
adapter.notifyDataSetChanged();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Food food = list.get(position);
Intent i = new Intent(FoodList.this,Details.class);
i.putExtra("position", position);
Bundle bundle = new Bundle();
startActivity(i);
}
});
}
}
但是当我添加searchview时,这不会工作!!因为子项目的位置会改变,而
时它不会返回正确的位置食物食品= list.get(position);
这是我点击子项目时开始的活动
public class Details extends AppCompatActivity {
int position;
Button b;
EditText edi;
ImageView im;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
b=(Button)findViewById(R.id.hotelbu);
edi=(EditText)findViewById(R.id.hoteled);
im = (ImageView)findViewById(R.id.hotelim);
text=(TextView)findViewById(R.id.hoteltx);
if(getIntent().getExtras() != null) {
position = getIntent().getExtras().getInt("position", 0);
}
switch(position){
case 9999:
break;
default:
text.setText(FoodList.list.get(position).getName());
edi.setText(FoodList.list.get(position).getPrice());
break;
}
}
}
我希望你们明白我在寻找什么,抱歉我的英语不好,感谢任何帮助谢谢你
答案 0 :(得分:0)
您必须使用AutoCompleteTextView而不是简单的EditText。 AutoCompleteTextView是一个可编辑的文本视图,在用户输入时自动显示下拉列表中的建议列表,用户可以从中选择一个项目来替换编辑框的内容。
IMongoQuery query = Query.And(Query.EQ("memberinfo.status", "approved"));
MongoCursor mongocursor = nsdb.GetCollection(DBPrefix, "Member").Find(query);
现在创建一个扩展BaseAdapter并实现Filterable接口的适配器类,覆盖BaseAdapter的所有方法和Filterable接口的getFilter()方法,并执行所需的所有操作。
您可以在此链接上看到AutoCompleteTextView的示例
[http://www.javatpoint.com/android-autocompletetextview-example]
这是创建AutoCompleteTextView
的xml代码auto_complete_textview = (AutoCompleteTextView)findViewById(R.id.auto_complete_textview);
auto_complete_textview.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
adapter.getFilter().filter(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});