android listview添加出现问题

问题描述

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

android listview添加出现问题的相关文章

Android ListView添加头布局和脚布局实例详解_Android

Android ListView添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考: 如果我们当前的页面有多个接口.多种布局的话,我们一般的选择无非就是1.多布局:2.各种复杂滑动布局外面套一层ScrollView(好low):3.头布局脚布局.有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了:学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了: 效果图:                  正文部分布局: fragme

Android ListView添加头布局和脚布局实例详解

Android ListView添加头布局和脚布局 之前学习喜马拉雅的时候做的一个小Demo,贴出来,供大家学习参考: 如果我们当前的页面有多个接口.多种布局的话,我们一般的选择无非就是1.多布局:2.各种复杂滑动布局外面套一层ScrollView(好low):3.头布局脚布局.有的时候我们用多布局并不能很好的实现,所以头布局跟脚布局就是我们最好的选择了:学过了ListView的话原理很简单,没啥理解的东西,直接贴代码了: 效果图: 正文部分布局: fragment_classify.xml <

Android ListView里控件添加监听方法的实例详解

Android ListView里控件添加监听方法的实例详解 关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现想要添加一个ImageView监听方法,发现崩了,也许是好久没有动ListView竟然忘了不能直接在主UI的xml文件里面调用其他xml文件的控件,哪怕ListView用的是这个xml文件. [错误示范]: 直接调用ImageView这个控件是ListV

touch-android listview添加侧滑监听(不是item)

问题描述 android listview添加侧滑监听(不是item) 求一个自定义listview,重写touch事件分发的demo.现在我的需求是让listview能响应侧滑事件 ,不是listview内的item侧滑哦. 解决方案 viewpager,里面套fragment,fragment里面放listview,侧滑翻页 解决方案二: 对listview重写其ontouchlistener,然后motionevent进行判断,侧滑的时候处理某个事件就行 解决方案三: 给带侧滑的界面加sl

android开发中listview添加图片

问题描述 android开发中listview添加图片 map1.put("image",R.drawable.jiantou); 我在listview中添加图像时,引用图像的地址,如上所示,但提示错误The method put(String, String) in the type HashMap is not applicable for the arguments (String, int) 但我看别人的代码都可以直接引用图像地址的,请大神帮忙解决一下,谢啦!! 解决方案 Ma

android给listview添加下拉刷新崩溃

问题描述 android给listview添加下拉刷新崩溃 logcat: 10-09 15:17:36.549 15984-15984/com.example.jkd.fchangshi D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000 10-09 15:17:36.549 15984-15984/com.example.jkd.fchangshi I/dalvikvm﹕ Could not find method android.view.Vi

android ListView内数据的动态添加与删除实例代码_Android

main.xml 文件:  复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"       android:layout_width="fill_parent"       android:layout_height

android ListView内数据的动态添加与删除实例代码

main.xml 文件: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_pa

Android 为ListView添加分段标头的方法

效果图: 我记得github上有一个类似的效果github类似效果 说一下实现这个效果的思路:在列表项中嵌入分段标头,然后根据需要显示或者隐藏分段标头,创建一个特殊的TextView,让其叠加在列表的顶部,当列表滚动到一个新的分段时,就更新其内容 创建列表布局 创建一个xml,随列表滚动的分段标头和列表顶部的固定分段标头复用这个布局文件 header.xml <?xml version="1.0" encoding="utf-8"?> <TextV