Was ich versuche zu tun, ist ein Button klicken, der in einem ListView von einem CustomCursorAdapter verwaltet wird. Wenn ich darauf klicke, muss ich die Schaltfläche unsichtbar machen und einen Wert in der Datenbank aktualisieren. Hier ist der Code, den ich für die ListActivity und den CursorAdapter verwende.Problem beim Registrieren von Klicks auf Button auf CustomCursorAdapter in einer ListView
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();
}
und der 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 bereits den Button gesetzt haben anklickbare (true) und fokussierbar (false) zu sein.
mit diesem Code i erreichen kann, was ich will, sondern durch auf dem Linearlayout Listview-Reihe (Scheite Element klickt, klicken Sie auf den Button hält. Wie mache ich die Taste genau das gleiche erhalten klicken als Linearlayout?
tuthier ist das Zeilenlayout.
<?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>
ja ich habe das schon ausprobiert, aber das Problem war in diesem Fall, dass ich die Position des angeklickten Elements in der Liste nicht kannte. Ich habe gerade einen Beitrag http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons gefunden. Jetzt setze ich einen onClickListener auf die Schaltfläche in meinem Adapter und setze sein Tag als _id der Spaltenzeile. Jetzt kann ich es zum Laufen bringen. Im Grunde ist Ihre Antwort die genau gleiche Alternative. Tnx. – DArkO