开发者

Problem registering clicks on Button on CustomCursorAdapter in a ListView

What i am trying to do is 开发者_开发知识库catch a Button click that is inside a ListView managed by a CustomCursorAdapter. when clicked i need to make the button invisible and update a value in the database. here is the code i am using for the ListActivity and the CursorAdapter.

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

}

and the Adapter:

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

i have already set the button to be clickable(true) and focusable(false).

with this code i can achieve what i want but by clicking the listView row (logs only item clicks on the LinearLayout that is holding the button. how do i make the button receive click exactly the same as LinearLayout does?

here is the row layout:

 <?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>


you need a new aproach as is this described in the button documentation.

However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

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

Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

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

The View passed into the method is a reference to the widget that was clicked. You can setTag() on the View in the adapter to recognize which button was clicked.


Try adding the following line in your item layout xml file. This should be added to the root layout.

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

From there you can set your onClickListener of the button in the getView method of your adapter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜