我正在尝试从我的自定义列表视图和按钮单击时从数据库中删除项目。我到目前为止,但从下面的代码你可能会看到我的错误。删除第一个项目时,位置会发生变化,但不会更新idarray信息。请帮忙!
历史适配器:
package uk.co.rascagneres.clocking_app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Jacques on 08/08/2016.
*/
public class HistoryAdapter extends BaseAdapter implements ListAdapter{
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public ArrayList<String> idArray = new ArrayList<String>();
DatabaseHandler db;
public HistoryAdapter (ArrayList<String> list, Context context, ArrayList<String> idArray, DatabaseHandler db){
this.list = list;
this.context = context;
this.idArray = idArray;
this.db = db;
}
@Override
public int getCount(){
return list.size();
}
@Override
public Object getItem(int pos){
return list.get(pos);
}
@Override
public long getItemId(int pos){
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.history_list, null);
}
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
System.out.println(list.get(position));
System.out.println(idArray.get(position));
System.out.print(idArray);
db.deleteRowID(Integer.parseInt(idArray.get(position)));
list.remove(position);
notifyDataSetChanged();
}
});
return view;
}
}
HistoryFragment:
package uk.co.rascagneres.clocking_app;
import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Jacques on 03/08/2016.
*/
public class HistoryFragment extends Fragment {
DatabaseHandler db;
View myView;
ArrayList<String> historyArray = new ArrayList<String>();
ArrayList<String> idArray = new ArrayList<String>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.history_layout, container, false);
db = new DatabaseHandler(getActivity());
ArrayList<String> list = insertData();
HistoryAdapter adapter = new HistoryAdapter(list, getActivity(), idArray, db);
ListView lView = (ListView)myView.findViewById(R.id.listView);
lView.setAdapter(adapter);
return myView;
}
public ArrayList<String> insertData(){
Cursor c = db.getAllData();
ArrayList<String> list = new ArrayList<String>();
while(c.moveToNext()) {
int id = c.getInt(0);
long in = c.getLong(1);
long out = c.getLong(2);
String outTime;
if(out == 0){
outTime = "N/A";
}else{
outTime = ClockingFragment.getDate(out, "HH:mm");
}
String date = ClockingFragment.getDate(in, "EEE, MMM d");
String inTime = ClockingFragment.getDate(in, "HH:mm");
historyArray.add(date + "\n Clocked In: " + inTime + "\n Clocked Out: " + outTime + "\n");
idArray.add(String.valueOf(id));
}
if (historyArray.isEmpty()){
historyArray.add("No History Available!");
}
return historyArray;
}
public ArrayList<String> getIdArray(){
System.out.println(idArray);
return idArray;
}
}
数据库处理程序部分:
public void deleteRowID(int ID)
{
SQLiteDatabase db = this.getWritableDatabase();
String IDString = String.valueOf(ID);
try
{
db.delete(TABLE_NAME, "ID = ?", new String[] { IDString });
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
db.close();
}
}