问题描述
- android listview添加出现问题
-
我是一枚新手,在写添加listview并对数据库进行增删改查时遇到了问题,添加listview时我想添加一个item可是一添加就添加了好多行,还有插入数据后不能listview没有显示之前插入的值,求大神给看看!这是我的代码:public class DLXMainActivity extends AppCompatActivity { private Toolbar mToolbar; public static EditText mDlxInput1, mDlxInput2;//输入框 public TextView mTitle, mContent, mTime;//显示内容 DatabaseHelper mDatabaseHelper = new DatabaseHelper(this); static List<Comment> mList = new ArrayList<>(); static Comment mComment = new Comment(); MyAdapter mAdapter; ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dlx_task3_dlxmain); initViewValue(); } public void initViewValue() { findViews(); setSupportActionBar(mToolbar); setOnclicklistener(); } //数据库的初始化和按下删除的监听事件 public void setOnclicklistener() { mDatabaseHelper = new DatabaseHelper(this); mAdapter = new MyAdapter(this, mList); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mComment = mList.get(position); int positioncurrent = mComment.getId(); mDatabaseHelper.databaseDelete(positioncurrent); mList.remove(mComment); mAdapter.notifyDataSetChanged(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.dlx_task3_menu, menu); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: showDialog_Layout(DLXMainActivity.this); break; } return super.onOptionsItemSelected(item); } //对话框 private void showDialog_Layout(final Context context) { LayoutInflater inflater = LayoutInflater.from(this); final View textEntryView = inflater.inflate(R.layout.dlx_task3_dialoglayout, null); final AlertDialog.Builder builder = new AlertDialog.Builder(context); mDlxInput1 = (EditText) textEntryView.findViewById(R.id.dlx_Input1); mDlxInput2 = (EditText) textEntryView.findViewById(R.id.dlx_Input2); builder.setCancelable(false); builder.setTitle(R.string.dlx_add); builder.setView(textEntryView); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(""); } }); builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { mDatabaseHelper.databaseInsert(); mDatabaseHelper.databaseFind(context); mAdapter.notifyDataSetChanged(); } }); builder.show(); } public void findViews() { mToolbar = (Toolbar) findViewById(R.id.toolbar); mTitle = (TextView) findViewById(R.id.dlx_maintitle); mContent = (TextView) findViewById(R.id.dlx_maincontent); mTime = (TextView) findViewById(R.id.dlx_maintime); mListView = (ListView) findViewById(R.id.dlx_lv); } } public class MyAdapter extends BaseAdapter { public List<Comment> list; public LayoutInflater mInflater = null; Context context; DatabaseHelper mDatabaseHelper = new DatabaseHelper(context); public MyAdapter(Context context, List<Comment> list) { this.mInflater = LayoutInflater.from(context); this.context = context; this.list = list; } //ViewHolder静态类 static class ViewHolder { public TextView title; public TextView content; public TextView time; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } //获取一个在数据集中指定索引的视图来显示数据 public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; //如果缓存convertView为空,则需要创建View if (convertView == null) { holder = new ViewHolder(); //根据自定义的Item布局加载布局 convertView = mInflater.inflate(R.layout.dlx_task3_commentlayout, null); holder.title = (TextView) convertView.findViewById(R.id.dlx_maintitle); holder.content = (TextView) convertView.findViewById(R.id.dlx_maincontent); holder.time = (TextView) convertView.findViewById(R.id.dlx_maintime); //将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.title.setText(list.get(position).getTitle()); holder.content.setText(list.get(position).getContent()); holder.time.setText(mDatabaseHelper.getTimeShow(context, (list.get(position).getTime()))); return convertView; } } public class DatabaseHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "Comment.db"; public static final String TABLE_NAME = " comment"; public long mDatetimecurrent; Context context; public long mDatetime; // TODO: 2016/5/18 public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + Comment.Commentinter.EVENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Comment.Commentinter.EVENT_TITLE + " TEXT NOT NULL, " + Comment.Commentinter.EVENT_CONTENT + " TEXT NOT NULL, " + Comment.Commentinter.EVENT_TIME + " INTEGER NOT NULL);"); } public void databaseInsert() { SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); mDatetime = System.currentTimeMillis(); //开始组装第一条数据 values.put("title", String.valueOf(DLXMainActivity.mDlxInput1.getText())); values.put("content", String.valueOf(DLXMainActivity.mDlxInput2.getText())); values.put("time", mDatetime); db.insert(TABLE_NAME, null, values);//插入第一条数据 db.close(); } /** * 删除数据库元素 * * @param */ public void databaseDelete(int positioncurrent) { SQLiteDatabase db = getWritableDatabase(); String[] args = {String.valueOf(positioncurrent)}; db.delete("comment", Comment.Commentinter.EVENT_ID + "=?", args); db.close(); } /** * 从数据库中取值并放到listview中 * @param context */ public void databaseFind(Context context) { this.context = context; SQLiteDatabase db = getReadableDatabase(); Cursor cursor = db.query(DatabaseHelper.TABLE_NAME,new String[] {Comment.Commentinter.EVENT_ID, Comment.Commentinter.EVENT_TITLE, Comment.Commentinter.EVENT_CONTENT,Comment.Commentinter.EVENT_TIME}, null, null, null, null, null, null); //DLXMainActivity.mList.clear(); while (cursor.moveToNext()) { String title = cursor.getString(cursor.getColumnIndex(Comment.Commentinter.EVENT_TITLE)); String content = cursor.getString(cursor.getColumnIndex(Comment.Commentinter.EVENT_CONTENT)); long time = cursor.getLong(cursor.getColumnIndex(Comment.Commentinter.EVENT_TIME)); int id = cursor.getInt(cursor.getColumnIndex(Comment.Commentinter.EVENT_ID)); DLXMainActivity.mComment.setTitle(title); DLXMainActivity.mComment.setContent(content); DLXMainActivity.mComment.setTime(time); DLXMainActivity.mComment.setId(id); DLXMainActivity.mList.add(DLXMainActivity.mComment); } cursor.close(); db.close(); } public String getTimeShow(Context context, long datetime) { mDatetimecurrent = System.currentTimeMillis(); long time = mDatetimecurrent - datetime; if (time / 1000 != 0) { if (time / 60000 != 0) { if (time / 3600000 != 0) { if (time / 86400000 != 0) { return time / 86400000 + context.getString(R.string.dlx_day1); } else { return time / 3600000 + context.getString(R.string.dlx_hour); } } else { return time / 60000 + context.getString(R.string.dlx_minute); } } else { return time / 1000 + context.getString(R.string.dlx_second); } } else { return context.getString(R.string.dlx_gang); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { onCreate(db); } @Override public void onOpen(SQLiteDatabase db) { super.onOpen(db); }
解决方案
Android中添加字串资源出现问题
[Android] AndroidManifest.xml出现问题
时间: 2025-01-21 18:20:29