내가하려는 것은 CustomCursorAdapter에 의해 관리되는 ListView 내부의 Button 클릭을 잡는 것입니다. 클릭하면 버튼을 보이지 않게하고 데이터베이스의 값을 업데이트해야합니다. 여기에 ListActivity 및 CursorAdapter에 사용하는 코드가 있습니다. ListView에서 CustomCursorAdapter의 Button에 클릭 수를 등록 할 때 문제가 발생했습니다.
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();
}
및 어댑터 : 이미 클릭 (사실)와 (false)를 포커스로 버튼을 설정 한
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은? 을처럼 버튼이 정확히 같은 클릭받을 수 있을까요 여기이 행 레이아웃이다. 이것은 button documentation에 설명 된대로 새 aproach 필요
<?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>
예 이미 시도했지만 문제는 목록에서 클릭 한 항목의 위치를 알 수 없다는 것입니다. 방금 게시물 http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons를 찾았습니다. 이제 어댑터의 버튼에 onClickListener를 설정하고 해당 태그를 열 행의 _id로 설정합니다. 그래서 지금 나는 그것을 일하게 할 수있다. 기본적으로 당신의 대답은 똑같은 대안입니다. Tnx. – DArkO