我在我的代码中配置了一个按钮来删除ListView中的一行,我认为它会起作用,但在离开页面后,数据才会重新出现。我已经看过一些关于如何做的帖子,但它很混乱,并想知道是否有人可以展示如何在setOnClickListener()
ListScoutInfoAdapter.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListScoutInfoAdapter extends ArrayAdapter {
List list = new ArrayList();
DatabaseHelper databaseHelper;
SQLiteDatabase db = databaseHelper.getWritableDatabase();
public ListScoutInfoAdapter (Context context, int resource) {
super(context, resource);
} //End of ListDataScoutInfoAdapter
static class LayoutHandler {
TextView TEAM_NUMBER, PORTCULLIS, CHEVAL_FRISE, MOAT, RAMPARTS, DRAWBRIDGE, SALLY_PORT, ROCK_WALL, ROCK_TERRAIN, LOW_BAR;
} //End of LayoutHandler
@Override
public void add(Object object) {
super.add(object);
list.add(object);
} //End of add
@Override
public int getCount() {
return list.size();
} //End of getCount
@Override
public Object getItem(int position) {
return list.get(position);
} //End of getItem
public void deleteRow(){
db.delete(DatabaseContract.NewDataInfo.TABLE_NAME, "id = '"+ DatabaseContract.NewDataInfo.COL_NUMBER +"'", null);
} //End of deleteRow
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutHandler layoutHandler;
if (row == null){
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout, parent, false);
layoutHandler = new LayoutHandler();
layoutHandler.TEAM_NUMBER = (TextView) row.findViewById(R.id.resultTeamNumber);
layoutHandler.PORTCULLIS = (TextView) row.findViewById(R.id.resultPortcullis);
layoutHandler.CHEVAL_FRISE = (TextView) row.findViewById(R.id.resultChevalFrise);
layoutHandler.MOAT = (TextView) row.findViewById(R.id.resultMoat);
layoutHandler.RAMPARTS = (TextView) row.findViewById(R.id.resultRamparts);
layoutHandler.DRAWBRIDGE = (TextView) row.findViewById(R.id.resultDrawbridge);
layoutHandler.SALLY_PORT = (TextView) row.findViewById(R.id.resultSallyPort);
layoutHandler.ROCK_WALL = (TextView) row.findViewById(R.id.resultRockWall);
layoutHandler.ROCK_TERRAIN = (TextView) row.findViewById(R.id.resultRockTerrain);
layoutHandler.LOW_BAR = (TextView) row.findViewById(R.id.resultLowBar);
row.setTag(layoutHandler);
}
else {
layoutHandler = (LayoutHandler) row.getTag();
} //End of if statement
final DatabaseProvider databaseProvider = (DatabaseProvider) this.getItem(position);
layoutHandler.TEAM_NUMBER.setText(databaseProvider.getTeamNumber());
layoutHandler.PORTCULLIS.setText(databaseProvider.getPortcullis());
layoutHandler.CHEVAL_FRISE.setText(databaseProvider.getChevalFrise());
layoutHandler.MOAT.setText(databaseProvider.getMoat());
layoutHandler.RAMPARTS.setText(databaseProvider.getRamparts());
layoutHandler.DRAWBRIDGE.setText(databaseProvider.getDrawbridge());
layoutHandler.SALLY_PORT.setText(databaseProvider.getSallyPort());
layoutHandler.ROCK_WALL.setText(databaseProvider.getRockWall());
layoutHandler.ROCK_TERRAIN.setText(databaseProvider.getRockTerrain());
layoutHandler.LOW_BAR.setText(databaseProvider.getLowBar());
//Deletes ListView row
Button deleteBtn = (Button) row.findViewById(R.id.buttonDelete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
} //End of onClick
}); //End of setOnClickListener
return row;
} //End of getView
} //End of class
logcat的
03-17 13:57:33.715 4135-4135/com.compscitutorials.basigarcia.ramfernoscout E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.compscitutorials.basigarcia.ramfernoscout, PID: 4135
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.compscitutorials.basigarcia.ramfernoscout.DatabaseHelper.getWritableDatabase()' on a null object reference
at com.compscitutorials.basigarcia.ramfernoscout.ListScoutInfoAdapter.<init>(ListScoutInfoAdapter.java:19)
at com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment.onCreateView(ScoutFragment.java:37)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
如果您的ID被指定为“TEAM_NUMBER”,那么您需要将其用于删除操作:
public void deleteRow(String rowId){
db.delete(DatabaseContract.NewDataInfo.TABLE_NAME, DatabaseContract.NewDataInfo.COL_NUMBER + "=" + rowId, null);
}
您需要将行ID传递给您的函数。
更好的方法是这样的:
public void deleteRow(String rowId){
db.delete(DatabaseContract.NewDataInfo.TABLE_NAME,
DatabaseContract.NewDataInfo.COL_NUMBER + "=?",
new String[] {rowId});
}
编辑:如何确定要删除的行
Button deleteBtn = (Button) row.findViewById(R.id.buttonDelete);
deleteBtn.setTag(databaseProvider.getTeamNumber());
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRow((String) v.getTag());
list.remove(position);
notifyDataSetChanged();
}
});