2010-12-01 112 views
2

我想要做的是捕獲由CustomCursorAdapter管理的ListView中的按鈕單擊。點擊時,我需要使按鈕不可見並更新數據庫中的值。這裏是我用於ListActivity和CursorAdapter的代碼。在ListView中的CustomCursorAdapter上註冊按鈕時出現問題

public class MainTabView extends ListActivity{ 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    fillListData(); 
} 


private void fillListData(){ 
DataBaseNamesHelper myDbNamesHelper = new DataBaseNamesHelper(this); 
myDbNamesHelper.openDataBase(); 
Cursor cursor = myDbNamesHelper.getCursorQueryWithAllTheTaxiStations(); 
startManagingCursor(cursor); 
    // the desired columns to be bound 
    String[] columns = new String[] { DataBaseNamesHelper.COLUMN_NAME, DataBaseNamesHelper.COLUMN_PEOPLE}; 
    // the XML defined views which the data will be bound to 
    int[] to = new int[] { R.id.name_entry, R.id.number_entry }; 

    // create the adapter using the cursor pointing to the desired data as well as the layout information 
    CustomCursorAdapter mAdapter = new CustomCursorAdapter(this, R.layout.list_entry, cursor, columns, to); 
    // set this adapter as your ListActivity's adapter 
    this.setListAdapter(mAdapter); 
    this.getListView().setOnItemClickListener(mAdapter); 
    myDbNamesHelper.close(); 

} 

和適配器:

public class CustomCursorAdapter extends SimpleCursorAdapter implements SectionIndexer,Filterable, 
    android.widget.AdapterView.OnItemClickListener{ 

private Context context; 
private int layout; 
private AlphabetIndexer alphaIndexer; 

public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { 
    super(context, layout, c, from, to); 
    this.context = context; 
    this.layout = layout; 
    alphaIndexer=new AlphabetIndexer(c, c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 

    Cursor c = getCursor(); 

    final LayoutInflater inflater = LayoutInflater.from(context); 
    View v = inflater.inflate(layout, parent, false); 

    int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME); 

    String name = c.getString(nameCol); 

    /** 
    * Next set the name of the entry. 
    */ 
    TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
    if (name_text != null) { 
     name_text.setText(name); 
    } 

    int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED); 
    int fav = c.getInt(favCol); 

    Button button = (Button) v.findViewById(R.id.Button01); 
    if(fav==1){ 
    button.setVisibility(View.INVISIBLE); 
    } 

    return v; 
} 

@Override 
public void bindView(View v, Context context, Cursor c) { 

    int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME); 

    String name = c.getString(nameCol); 

    /** 
    * Next set the name of the entry. 
    */ 
    TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
    if (name_text != null) { 
     name_text.setText(name); 
    } 
    int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED); 
    int fav = c.getInt(favCol); 

    Button button = (Button) v.findViewById(R.id.Button01); 
    Log.e("fav",String.valueOf(fav)); 
    if(fav==1){ 
    button.setVisibility(View.INVISIBLE); 
    } 
} 

@Override 
public int getPositionForSection(int section) { 
    return alphaIndexer.getPositionForSection(section); 
} 

@Override 
public int getSectionForPosition(int position) { 
    return alphaIndexer.getSectionForPosition(position); 
} 

@Override 
public Object[] getSections() { 
    return alphaIndexer.getSections(); 
} 
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    Log.e("item Click", arg1.toString()+ " position> " +arg2); 
} 

我已經設置按鈕可點擊(真)和可調焦的(假)。

與此代碼我可以實現我想要什麼,但通過點擊列表視圖行(僅記錄項的點擊上的LinearLayout持有該按鈕。如何使按鈕收到單擊完全一樣的LinearLayout呢?

這裏是行佈局:

<?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="horizontal" android:focusable="false"> 
<TextView 
    android:id="@+id/name_entry" 
    android:layout_height="wrap_content" 
    android:textSize="28dip" android:layout_width="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/> 
     <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fav" android:layout_gravity="center_vertical" android:layout_marginRight="10dp" android:focusable="false" android:clickable="true"></Button><TextView 
    android:id="@+id/number_entry" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="28dip" /> 


</LinearLayout> 

回答

2

你需要一個新的形式給出,還擁有在button documentation描述

然而,不是施加OnClickListener到您的活動按鈕,就可以阿西gn使用android:onClick屬性在XML佈局中按鈕的方法。例如:

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/self_destruct" 
    android:onClick="selfDestruct" /> 

現在,當用戶單擊按鈕時,Android系統調用活動的selfDestruct(View)方法。爲了使其發揮作用,該方法必須是公開的,並且接受View作爲其唯一參數。例如:

public void selfDestruct(View view) { 
    // Kabloey 
} 

傳遞給方法的視圖是對被單擊的小部件的引用。您可以在適配器中查看setTag()以識別哪個按鈕被點擊。

+0

是的,我已經試過,但在這種情況下的問題是,我不知道在列表中單擊項目的位置。我剛剛發現了一篇文章http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons。現在我將onClickListener設置爲適配器中的按鈕,並將其標記設置爲列行的_id。所以現在我可以讓它工作。基本上你的答案是完全相同的選擇。 TNX。 – DArkO 2010-12-01 13:51:13

0

嘗試在項目佈局xml文件中添加以下行。這應該被添加到根佈局。

<LinearLayout ..... 
       android:descendantFocusability="beforeDescendants" 
       ..... /> 

從那裏你可以在你的適配器的getView方法中設置你的onClickListener按鈕。

相關問題