2 qq 28150513 qq_28150513 于 2016.04.25 17:22 提问

database为空 求大神解决下呗

package com.wlh.app.notepad.adapter;

import java.util.ArrayList;

import com.wlh.app.notepad.modle.NotepdaBean;
import com.wlh.app.notepad.util.db.MyDataBaseHelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wlh.app.notepad.act.NarrateAct;
import com.wlh.app.notepad.act.R;

public class ListToItemAdapter extends BaseAdapter{

private NotepdaBean bean;

private ArrayList<NotepdaBean> arrayList = new ArrayList<NotepdaBean>();
public static SQLiteDatabase database;

private LayoutInflater mInflater;

private float downX;
private float upX;
private TextView mListjmp;
private Animation animation;

private Context context;
private View view;

public ListToItemAdapter(Context context){

    mInflater = LayoutInflater.from(context);
    this.context = context;
    animation=AnimationUtils.loadAnimation(context, R.anim.push_out);

}

public void addDataSource(ArrayList<NotepdaBean> arrayList){

    this.arrayList = arrayList;
    notifyDataSetChanged();

}

public void insertDataSource(NotepdaBean bean){

    arrayList.add(bean);
    notifyDataSetChanged();

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return arrayList.size() > 0 ? arrayList.size() : 0;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return arrayList.size() > 0 ? arrayList.get(position) : "";
}

@Override
public long getItemId(int id) {
    // TODO Auto-generated method stub
    return id;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.activity_list, parent, false);

        holder = new ViewHolder();
        holder.mListImg = (ImageView) convertView.findViewById(R.id.list_img_title);
        holder.mListTitle = (TextView) convertView.findViewById(R.id.list_tv_title);
        holder.mListContent = (TextView) convertView.findViewById(R.id.list_tv_content);
        holder.mListData = (TextView) convertView.findViewById(R.id.list_tv_data);
        holder.mListjmp = (TextView) convertView.findViewById(R.id.list_tv_jmp);

        convertView.setTag(holder);

    }else{

        holder = (ViewHolder) convertView.getTag();

    }

    convertView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            final ViewHolder holder = (ViewHolder) v.getTag();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                downX = event.getX();
                if (mListjmp != null) {   
                    mListjmp.setVisibility(View.GONE);
                }

                break;

            case MotionEvent.ACTION_UP:
                upX = event.getX();
                break;

            }

            if (holder.mListjmp != null) {
                if (Math.abs(downX - upX) > 35) {
                    holder.mListjmp.setVisibility(View.VISIBLE);
                    mListjmp = holder.mListjmp;
                    view=v;
                    return true;
                }
                return false;
            }
            return false;
        }
    });

    holder.mListjmp.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mListjmp != null) {
                mListjmp.setVisibility(View.GONE);
                database = NarrateAct.dataBaseHelper.getWritableDatabase();
                String deletesql = "DELETE FROM "+MyDataBaseHelper.NOTEPAD_TABLE_NAME+" WHERE "+MyDataBaseHelper.NOTEPAD_ID +"= '?'";
                Log.i("my", deletesql);
                database.execSQL(deletesql, new String[]{bean.getID()+""});
                deleteItem(view, position);
            }

        }
    });

    bean = (NotepdaBean) getItem(position);

    holder.mListImg.setBackgroundResource(bean.getImg());
    holder.mListTitle.setText(bean.getTitlie());
    holder.mListContent.setText(bean.getContent());
    holder.mListData.setText(bean.getDate());
    holder.mListjmp.setVisibility(View.GONE);
    holder.mListjmp.setBackgroundResource(R.drawable.ic_list_delete);

    return convertView;
}

public void deleteItem(View view,final int position){

    view.startAnimation(animation);

    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            arrayList.remove(position);
            notifyDataSetChanged(); 

        }
    });

}

static class ViewHolder{

    ImageView mListImg;
    TextView mListTitle;
    TextView mListContent;
    TextView mListData;
    TextView mListjmp;

}

}

2个回答

erichk2008
erichk2008   2016.04.25 17:45

database = NarrateAct.dataBaseHelper.getWritableDatabase();

需要看一下NarrateAct的代码,dataBaseHelper这个变量是不是没有初始化,或者是DataBaseHelper这个类里面的建表语句有问题?

另外,MyDataBaseHelper和DataBaseHelper的关系是?

qq_28150513
qq_28150513   2016.04.28 18:39

谢谢了 已经解决了 MyDataBaseHelper就是SQLite的辅助类

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!