2010-08-12 3 views
20

방금 ​​Android를 시작했으며 몇 가지 문제가 있습니다.
데이터베이스에서 채워진 ListView을 만들었습니다.
각 행에는 목록과 데이터베이스에서 항목을 삭제하는 버튼이 있습니다.목록 뷰 행의 버튼에 onclick 리스너에 매개 변수를 전달합니다.

단추에 이벤트 수신기를 연결할 수는 있지만 삭제할 일치하는 데이터베이스 레코드를 확인할 수 없었습니다.

내 클래스는 그래서 처리기에 데이터베이스 레코드의 ID를 전달 할 수있는 방법 그래서 그것을 삭제하는 데이터베이스 어댑터를 사용할 수

public class FeedArrayAdapter extends ArrayAdapter<Feed> implements OnClickListener 
{ 
    private ARssEReaderDBAdapter dba; 
    private String TAG = "FeedArrayAdapter"; 

    public FeedArrayAdapter(Context context, int textViewResourceId, List<Feed> items)   { 
    super(context, textViewResourceId, items); 
    Log.w(TAG, "List"); 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     Log.w(TAG, "getView"); 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.feedlistrow, null); 
    } 
    Feed feed = getItem(position); 
    if (feed != null) { 
     TextView title = (TextView)v.findViewById(R.id.TextView01); 
     if (title != null) { 
      title.setText(feed.getTitle()); 
     } 
     Button btnDelete = (Button)v.findViewById(R.id.btnDelete); 
     btnDelete.setOnClickListener(this); //btnDeleteFeedListener 
    } 
    return v; 
} 

public void onClick(View v) { 
    Log.w(TAG, "something got clicked: "); 
} 

아래와 같습니다?

답변

39

온 클릭 귀하의 답변 getTag()

22

OnClickListener를 구현하는 내부 클래스를 만든 다음 생성자에서 position 변수를 전달하십시오.

private Class MyClickListener implements OnClickListener { 

    private int position; 

    public MyClickListener(int position) { 
     this.position = position; 
    } 

    public void onClick(View v) { 
     System.out.println("position " + getPosition() + " clicked."); 
    } 

    public int getPosition() { 
     return position; 
    } 

} 
당신은 태그에서 레코드의 ID를 저장하는보기에 setTag()를 호출에서 읽을해야
+0

감사 전화, 태그 옵션을 구현하는 가장 쉬운 듯 그리고 내가 원하는 모든 것을 수행합니다. Thanks again – Richard

+1

내 setTag()가 이미 다른 용도로 사용 되었기 때문에이 답변을 좋아합니다. –

+0

클릭 리스너에 둘 이상의 값을 전달해야 할 때 유용했습니다. Thanks – speedynomads

관련 문제