2010-12-01 2 views
2

내가하려는 것은 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> 

답변

2

그러나, 대신 활동의 버튼에 OnClickListener를 적용, 당신은 아씨 수 있습니다 android : onClick 속성을 사용하여 XML 레이아웃에서 버튼에 메서드를 추가합니다. 예 :

이제 사용자가 버튼을 클릭하면 Android 시스템에서 활동의 selfDestruct (View) 메소드를 호출합니다. 이 작업을 수행하려면 메서드가 public이어야하며 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

0

항목 레이아웃 xml 파일에 다음 줄을 추가하십시오. 이것은 루트 레이아웃에 추가되어야합니다.

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

거기에서 어댑터의 getView 메소드에있는 버튼의 onClickListener를 설정할 수 있습니다.

관련 문제