2014-01-23 2 views
1

그래서 스크롤보기 내에 표보기가있는 활동이 있습니다. 동적으로 표보기에 행을 추가합니다. 각 행에는 2 개의 스팅과 2 개의 정수가 데이터베이스에서 쿼리됩니다. 전체 행을 삭제하는 행에 단추를 추가하고 해당 행이 포함 된 데이터베이스에서 데이터를 삭제해야합니다.Android 삭제 버튼

버튼을 설정했지만 onClickListner에 쓰지 못했습니다. 어떤 버튼을 누르는 지 감지하는 방법과 해당 버튼을 해당 행과 연관시키는 방법을 알지 못합니다.

도움을 주시면 감사하겠습니다. 감사 !

이것은 행을 추가하는 방법입니다. Location 객체는 DB에서 가져온 데이터 만 보유합니다.

public void insertRow(Location l,int index){ 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


     View newRow = inflater.inflate(R.layout.row, null); 

     TextView textview = (TextView) newRow.findViewById(R.id.textViewLocation); 
     TextView textview2= (TextView) newRow.findViewById(R.id.coordinates); 

     textview2.setText(l.getLatitude() +" | "+ l.getLongitude()); 
     textview.setText(l.getName()); 

     //Button remove = (Button) newRow.findViewById(R.id.removeButtonLocation); 
     // remove.setOnClickListener(removeLocationListener); 



     // Add the new components for the location to the TableLayout 
     a.addView(newRow, index); 


} 

그리고 레이아웃 파일

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/locations_textview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/row0" 
     android:layout_gravity="center" /> 

    <ScrollView 
     android:id="@+id/Locations_scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <TableLayout 
      android:id="@+id/stockTableScrollView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/egg_shell" > 

     </TableLayout> 

    </ScrollView> 



    </LinearLayout> 
+1

시도한 것에 대해 몇 가지 코드를 추가하면 도움이됩니다. 그렇게하십시오, 그것은 또한 당신이 무엇을하려고하는지 명확하게 할 것입니다 :) –

+0

나는 활동이 너무 많습니다, 그는 지금 집에 있습니다. 젠장. –

+0

코드를 작성하고 추가했습니다. –

답변

1

이 좋아, 그래서 당신의 문제를 한 번에 하나의 시작하자. 당신은 실제로 이미 코드를 가지고 있지만, 그것은 주석,의 그것을 살펴 보자 :

Button remove = (Button) newRow.findViewById(R.id.removeButtonLocation); 
    remove.setOnClickListener(removeLocationListener); 

이라는 개체는 removeLocationListener 당신이 작업을 수행하는 포함하지 않은이 말했다. OnClickListener는 하나의 함수, onClick (View v)을 정의합니다. 전달 된 뷰는 참조 뷰입니다. 따라서 뷰와 연관된 행을 가져 와서 삭제하려고합니다. 먼저 행을 봅시다.

public void onClick(View v) { 
    TableRow row=(TableRow) v.getParent(); 
} 

좋아요, 그럼 행을 삭제하는 방법은 무엇입니까? 위의 레이아웃에서 제거해야합니다. BTW

OnClickListener removeLocationListener= new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     TableRow row=(TableRow) v.getParent(); 
     TableLayout tl=(TableLayout) v.getParent(); 
     tl.removeView(row); 
    } 
}; 

, 당신이 더 적합하다하는 ListView보고 할 수 있습니다 :

public void onClick(View v) { 
    TableRow row=(TableRow) v.getParent(); 
    TableLayout tl=(TableLayout) v.getParent(); 
    tl.removeView(row); 
} 

그냥 어딘가에 removeLocationListener 정의 할 수 있습니다 : 그럼 그것에서의보기를 얻을 전에 수행 같은 일을하자 그런 것들. 커서를 직접 커서로 전달할 수도 있으므로 모든 것을 관리하기가 훨씬 쉬워집니다.