2014-02-22 2 views
0

내 Android 응용 프로그램에서 문제가 발생했습니다. 문제는 ListView 또는 데이터베이스에서 항목을 삭제할 수 없다는 것입니다. Google, S0 및 YouTube 전체에서 검색했지만 해결책을 찾을 수 없습니다. 다음은 내 ListView 어댑터입니다.ListView 및 데이터베이스에서 항목을 삭제하는 방법

listContent.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(final AdapterView<?> parent, View view, int position, long id) { 
      int j = 0; 
      for (j = 0; j < parent.getChildCount(); j++) 
       parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT); 

      // change the background color of the selected element 
      view.setBackgroundColor(Color.LTGRAY); 

      final long hello = id; 
      Button button = (Button) getView().findViewById(R.id.clrselected); 

      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        dbAdapter.delete123456(hello); 
        adapter.notifyDataSetChanged(); 
        listContent.refreshDrawableState(); 
        listContent.invalidate(); 
       } 
      }); 

     } 
    }); 

내 단추 OnClickListener에서 데이터베이스 및 ListView 값을 삭제하려고합니다. 다음은 delete123456 코드입니다.

public boolean delete123456(long rowId) { 
    return mDb.delete(FTS_VIRTUAL_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

버튼을 클릭해도 아무런 변화가 없습니다. 내가 뭘 잘못하고 있는지 모르겠다. 이 문제와 관련된 도움을 주시면 감사하겠습니다.

package com.example.foodsaver2; 

import android.content.Context; 
import android.database.Cursor; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CursorAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CustomCursorAdapter extends CursorAdapter { 

public CustomCursorAdapter(Context context, Cursor c) { 
    super(context, c); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    // when the view will be created for first time, 
    // we need to tell the adapters, how each item will look 
    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
    View retView = inflater.inflate(R.layout.show_row_item, parent, false); 

    return retView; 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    // here we are setting our data 
    // that means, take the data from the cursor and put it in views 

    TextView textViewPersonName = (TextView) view.findViewById(R.id.tv_person_name); 
    textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))); 

    TextView textViewPersonPIN = (TextView) view.findViewById(R.id.tv_person_pin); 
    textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)))); 
} 

} 
+0

테이블 선언을 게시 할 수 있습니까? – bluewhile

+0

맞춤 어댑터를 사용하는 경우이 함수를 게시 할 수 있습니까? public long getItemId (int position) – Merlevede

+0

커서 어댑터를 사용하여 'ListView' 태그에있는'view'의 ID와 함께 삭제하면 목표를 달성 할 수 있습니다. –

답변

0

나는 때문에 제한된 코멘트 공간의 대답으로이를 게시합니다 :


편집

여기 내 어댑터 클래스 코드입니다.

어댑터는 목록보기의 각 행에 대해 Idposition입니다. 귀하의 코드에서 다음을 사용하고 있습니다 :

final long hello = id; 
dbAdapter.delete123456(hello); 

일반적으로 위치는 정확히 테이블의 위치와 같습니다. ID는 어댑터의 사용자에 의해 설정됩니다. 배열을 사용할 때 일반적으로 위치와 ID는 같지만 데이터베이스를 사용할 때 ID는 테이블의 내부 참조입니다.

내 생각에, 사용중인 ID가 잘못되었거나 설정되지 않았습니다. 하지만 내가 게시 한 코드는 알 수 없습니다. 어쩌면 더 도움이 될 어댑터 코드를 게시해야합니다.

+0

Merlevede, 내 CursorAdapter 코드를 게시했습니다. – Rohodude

+0

'hello'를 축배로 표시하면 ListView에서 첫 번째 항목을 클릭하면 0이되고, ListView의 두 번째 항목을 클릭하면 1이됩니다. – Rohodude

관련 문제