내 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))));
}
}
테이블 선언을 게시 할 수 있습니까? – bluewhile
맞춤 어댑터를 사용하는 경우이 함수를 게시 할 수 있습니까? public long getItemId (int position) – Merlevede
커서 어댑터를 사용하여 'ListView' 태그에있는'view'의 ID와 함께 삭제하면 목표를 달성 할 수 있습니다. –