package com.example.myapplication; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListAdapter listAdapter; private String month11; private DBHelper helper; private ListView listView; private List<costList>list; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = getIntent(); month11 = intent.getStringExtra("date"); initView(); initData(); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { new AlertDialog.Builder(MainActivity.this).setTitle("注意") .setMessage("是否删除要此项记录?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String view1= String.valueOf(listAdapter.getItemId(position)); SQLiteDatabase db = helper.getWritableDatabase(); db.delete("account2","_id= ?",new String[]{view1}); Toast.makeText(getBaseContext(), "已删除", Toast.LENGTH_SHORT).show(); initData(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "已取消删除", Toast.LENGTH_SHORT).show(); } }).create().show(); return false; } }); } @SuppressLint("Range") private void initData() { list=new ArrayList<>(); SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.query("account2",null,"Date like '"+month11+"%'",null,null, null,null); BigDecimal sum=new BigDecimal(0); while (cursor.moveToNext()){ costList clist=new costList();//构造实例 BigDecimal temp=new BigDecimal(cursor.getString(cursor.getColumnIndex("Money"))); sum=sum.add(temp); clist.setSe(cursor.getString(cursor.getColumnIndex("Se"))); clist.set_id(cursor.getString(cursor.getColumnIndex("_id"))); clist.setTitle(cursor.getString(cursor.getColumnIndex("Title"))); clist.setDate(cursor.getString(cursor.getColumnIndex("Date"))); clist.setMoney(temp.toString()); list.add(clist); } //绑定适配器 listAdapter=new ListAdapter(this,list); listView.setAdapter(listAdapter); textView.setText(sum.toString()); db.close(); } private void initView() { helper=new DBHelper(MainActivity.this); listView = findViewById(R.id.list_view); textView =findViewById(R.id.t_money); } }标签:private,cursor,clist,new,import,android,2.13 From: https://www.cnblogs.com/luoqingci/p/18040466