1

我打算使用网格视图在 android 中开发一个单词搜索游戏。我不知道如何在网格视图中选择单词。

4

1 回答 1

1

这是一个在网格视图中过滤数据的简单过程:

首先,打开main.xml文件

接下来,在xml文件中添加一个edittext控件和button控件

为此添加以下代码

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

接下来,编写如下代码执行搜索操作方法

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())); 

}   
于 2013-01-15T12:34:42.087 回答