2016-08-25 5 views
0

중복 된 질문으로 불릴 수도 있지만 내 사례가 매우 독특하다는 것을 알고 있습니다. 기본적으로 내 커서 ID를 얻는 방법이 필요합니다. 즉, 사용자 지정 어댑터로 구문 분석 된 테이블의 데이터베이스에서 가져온 ID입니다. 그래서 내가 그 목록을 클릭하면, 그것은 정확한 id를 토스트한다. 여기에 내가 지금까지 가지고있는 무엇입니까 ...커서에서 CustomAdapter로 데이터베이스 ID 가져 오기 (Android)

나는 cursoradapter가 아니라 customadapter를 사용하고 싶습니다.

//Get Categories from database 
    final Cursor cursor = dbHandler.getCategories(0); 
    if (cursor != null) { 
     if(cursor.moveToFirst()){ 
      do{ 
       Categories categories = new Categories(); 
       categories.set_id(cursor.getInt(0)); 
       categories.set_categoryname(cursor.getString(2)); 
       categories.set_categoriescaption(cursor.getString(3)); 
       //list.add(cursor.getString(cursor.getColumnIndex(dbHandler.COLUMN_CATEGORY_NAME))); 
       categoriesList.add(categories); 

      }while (cursor.moveToNext()); 
     } 
     cursor.close(); 
    } 

커서 내가 버튼 및 아니오받은 위치 ID를 클릭하면 테이블의 실제 ID가 보여 싶은 내 SQLite 데이터베이스

listView = (ListView) view.findViewById(R.id.categories); 

    adapter = new CategoryAdapter(view.getContext(), R.layout.cursor_row, categoriesList); 
    listView.setAdapter(adapter); 

    listView.setOnItemClickListener(
      new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        String cid = String.valueOf(id); 
        Toast.makeText(view.getContext(), cid , Toast.LENGTH_SHORT).show(); 

에서 데이터를 얻을. 진짜 이드는 1부터 시작해야하는데, 여기서 0을 얻고 있습니다. 도움을 받으실 수 있습니다. 고마워요

답변

0

인터넷에서 많은 숙고 끝에, 이제는 많은 의미가있는 해결책을 발견했습니다.

public void onItemClick(AdapterView<?> parent, View view, int position, long id) 

온 클릭 어댑터 뷰에서의 ID입니다. 재정의하지 않으면 기본 위치로 설정됩니다. 그래서 나는 나의 ID를 나의 customadapter에 건네주고, 나의 ID를 볼 수 있었다.

@Override 
public long getItemId(int position) { 
    return data.get(position).Id; 
}