2013-01-19 75 views
0

我有關於SimpleCursorAdapter和ListView的問題。當我想通過查詢從數據庫創建動態列表菜單。問題是listview無法設置onClickListener在用戶單擊時執行某些操作。這些是我的代碼。Android如何從ListView和SimpleMenuAdapter創建可點擊菜單

在文件 「menu_header.xml」

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/menu_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" 
     android:background="@drawable/menu_background"> 

     <Button 
       android:id="@+id/bt_back" 
       android:layout_width="70dip" 
       android:layout_height="40dip" 
       android:layout_centerVertical="true" 
       android:text="@string/menu_back" /> 

     <ImageButton 
       android:id="@+id/bt_search" 
       android:layout_width="50dip" 
       android:layout_height="50dip" 
       android:background="@null" 
       android:src="@android:drawable/ic_menu_search" 
       android:layout_alignParentRight="true" 
       android:paddingLeft="5dip" 
       android:paddingRight="5dip" /> 

</RelativeLayout> 

文件 「menu_wrapper.xml」

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

    <include layout="@layout/menu_header" /> 

    <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

      <ListView 
        android:id="@android:id/list" 
        android:layout_below="@id/menu_bar" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 

    </RelativeLayout> 

</LinearLayout> 

在文件 「menu_choice.xml」

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/menu_selector" 
     android:clickable="true" 
     android:padding="10dip"> 

     <LinearLayout 
       android:id="@+id/thumbnail" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="3dip" 
       android:layout_alignParentLeft="true" 
       android:background="@drawable/image_bg" 
       android:layout_marginRight="10dip"> 

       <ImageView 
         android:id="@+id/list_image" 
         android:layout_width="60dip" 
         android:layout_height="60dip" 
         android:src="@drawable/building" /> 

     </LinearLayout> 

     <TextView 
       android:id="@+id/eng_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignTop="@+id/thumbnail" 
       android:layout_toRightOf="@+id/thumbnail" 
       android:text="Building" 
       android:textColor="#040404" 
       android:typeface="sans" 
       android:paddingTop="5dip" 
       android:textSize="20dip" 
       android:textStyle="bold" /> 

     <TextView 
       android:id="@+id/thai_name" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/eng_name" 
       android:textColor="#343434" 
       android:textSize="15dip" 
       android:layout_marginTop="3dip" 
       android:layout_toRightOf="@+id/thumbnail" 
       android:text="Test" /> 

     <ImageView android:layout_width="15dip" 
        android:layout_height="15dip" 
        android:src="@drawable/next_arrow" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" /> 

</RelativeLayout> 

,最後一個是,我用它來創建菜單

public class Menu extends ListActivity { 

    private ListView listView; 
    private ImageButton imageButtonSearch; 
    private Button buttonBack; 

    private Constants constants = Constants.getInstance(); 
    private Database database = new Database(this); 
    private Beans beans = Beans.getInstance(); 
    private Context context = this; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.menu_wrapper); 

     buttonBack = (Button)findViewById(R.id.bt_back); 
     imageButtonSearch = (ImageButton)findViewById(R.id.bt_search); 

     buttonBack.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       finish(); 
      } 
     }); 

     imageButtonSearch.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(); 
       i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivityForResult(new Intent(context, Search.class), beans.REQUEST_CODE); 
      } 
     }); 

     listView = this.getListView(); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
       Log.d("Item Click","item click"); 
      } 
     }); 

     database.open(); 
     Cursor cursorPlaceType = database.getPlaceType(); 
     startManagingCursor(cursorPlaceType); 

     if(cursorPlaceType != null){ 

      String[] columns = new String[]{ constants.PLACE_TYPE_IMAGE, constants.PLACE_TYPE_ENAME, constants.PLACE_TYPE_TNAME }; 
      int[] to = new int[]{ R.id.list_image, R.id.eng_name, R.id.thai_name }; 

      SimpleCursorAdapter menuAdapter = new SimpleCursorAdapter(this, R.layout.menu_choice, cursorPlaceType, columns, to); 
      menuAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
       @Override 
       public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
        if(view.getId() == R.id.list_image){ 

         try { 

          String imageFile = constants.IMAGE_ASSETS + cursor.getString(columnIndex) + ".png"; 
          Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(imageFile)); 
          ((ImageView)view).setImageBitmap(bitmap); 

         } catch (IOException e) { e.printStackTrace(); } 
         return true; 
        }//end if 

        return false; 
       }//end setViewValue 
      }); 
      this.setListAdapter(menuAdapter); 

     }else{ 
      new AlertDialog.Builder(this) 
        .setMessage("Try again") 
        .setNeutralButton("OK", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          finish(); 
         } 
        }).show(); 
     } 

    }//end onCreate() 

謝謝你確實的幫助文件。

+0

發佈您的logcat,最好是說出錯的地方,併發布相應的代碼片段,而不是發佈代碼牆。 – Kanth

回答

0

使用protected void onListItemClick(ListView l, View v, int position, long id)這種方法的ListActivity而不是您的listView.setOnItemClickListener方法。

相關問題