我计划使用网格视图在android中开发一个单词搜索游戏。我不知道如何在网格视图中选择单词。
答案 0 :(得分:1)
这是一个简单的过程,在网格视图中包含过滤数据:
首先,打开main.xml文件
接下来,在xml文件中添加一个edittext控件和按钮控件
为此添加以下代码
<EditText android:id="@+id/edtSearch"
android:hint="Enter the item no"
android:layout_width="200dip"
android:layout_height="50dip"/>
<Button android:id="@+id/SearchItem"
android:text="Search Item"
android:layout_width="180dip"
android:layout_height="50dip"
android:paddingLeft="10dip" />
接下来,保存文件并创建一个类文件,并在按钮事件中编写以下代码。
Button search = (Button) findViewById(R.id.SearchItem);
search.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
performsearchoperation();
Log.v("search button"," clicked");
}
});
接下来,编写以下代码performsearchoperation方法
private void performsearchoperation() {
// TODO Auto-generated method stub
Log.v("search button"," entered");
EditText edittext=(EditText) findViewById(R.id.edtSearch);
ITEMID=edittext.getText().toString();
int itemlength=ITEMID.length();
Log.v("ILvalue ",String.valueOf(itemlength));
Log.v("Ivalue ",String.valueOf(ITEMID.toString()));
if(itemlength==0)
{
Log.v("Item value ","zero");
BindListView();
}
else
{
Log.v("Item value ","Not zero");
try{
DBAdapter db = new DBAdapter(getBaseContext());
db.open();
Log.v("Entered","Item id");
Cursor myCur = db.getItems_From_Search(ITEMID);
gridview.setAdapter(new MyAdapter(this,myCur));
gridview.setVisibility(View.VISIBLE);
Log.v("parts count",String.valueOf(myCur.getCount()));
db.close();
}catch (Exception e) {
Toast.makeText(getBaseContext(),e.toString(),Toast.LENGTH_LONG).show();
Log.v("Error",e.toString());
}
}
Log.v("data ",String.valueOf(ITEMID.toString()));
}