如何在自定义适配器中分配SQLiteOpenHelper?

时间:2017-03-31 19:24:22

标签: java database sqlite android-studio custom-adapter

我已经搜索了这个问题的答案很长时间了,但尚未找到答案。 我正在使用SQLiteDatabase进行项目。一切正常,除了将SQLiteOpenHelper分配给我的CustomAdapter。我在Android工作室大约两个月和所有Java的东西,所以我只是搜索互联网上的几乎所有东西。这是我项目的片段。

SQLiteOpenHelper:



public class ActivitiesDataBase extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "database.db";
    public static final String ACTIVITIES_COLUMN_ID = "id";
    public static final String ACTIVITIES_TABLE_NAME = "create_activity";
    public static final String ACTIVITIES_COLUMN_NAME = "activity";
    public static final String ACTIVITIES_COLUMN_NUMBER = "number";
    public static final String ACTIVITIES_COLUMN_DESCRIPTION = "description";
    
    
    
    //Created command to create items
    public void addactivity(String activity, String description, String number){

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv =new ContentValues();
        cv.put(ACTIVITIES_COLUMN_NAME, activity);
        cv.put(ACTIVITIES_COLUMN_NUMBER, number);
        cv.put(ACTIVITIES_COLUMN_DESCRIPTION, description);

        db.insert(ACTIVITIES_TABLE_NAME, null, cv);
        db.close();
    }
    
    
    
    
    //Constructor
        public ActivitiesDataBase(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE create_activity" +
                "(id INTEGER PRIMARY KEY, " +
                "activity TEXT, " +
                "number TEXT, " +
                "description TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS create_activity");

    }
}




ActivityMain的一小部分:



public class MainActivity extends AppCompatActivity implements DrawerLocker {

//Assigning SQLiteOpenHelper, no errors, result is fine.
    final ActivitiesDataBase db = new ActivitiesDataBase(this);
    
    private SQLiteDatabase sql;
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//Here I call command addactivity and it works fine, because I
//assigned SQLiteOpenHelper well.
    db.addactivity(activitysubject, activitydesc, activitynum);
    
    }
}




这是我的自定义适配器:



public class CustomAdapter extends ArrayAdapter {

    private List sarasas = new ArrayList();

    public CustomAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        sarasas.add(object);
    }

    @Override
    public int getCount() {
        return this.sarasas.size();
    }


    @Override
    public Object getItem(int position) {
        return this.sarasas.get(position);
    }


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

        View v = convertView;

        final Duomenys pavadinimas;



        if (v == null){
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(R.layout.listview_layout, parent, false);

            pavadinimas = new Duomenys();
//I have missed some unnecessary text here, hope it doesn't change
//contex a lot



//So, crap starts here. Don't mind that pavadinimas after dots.
//Basically it's written here similarly to MainActivity, but it
//shows errors because assigning is different :(

//           (SQLiteOpenHelper assigning is below)
pavadinimas.plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pavadinimas.db.addactivity(name, description, number);
                }
            });



            v.setTag(pavadinimas);
        }else {
            pavadinimas = (Duomenys) v.getTag();
        }


        DataActivity perdavimas = (DataActivity) this.getItem(position);
        pavadinimas.txt1.setText(perdavimas.getNamee());

        return v;

    }

    static class Duomenys{

        SQLiteDatabase sql;
        
        //Here i don't know how to assign my SQLiteOpenHelper,
        // so actually, I NEED HELP HERE
        ActivitiesDataBase db;
        
    }
}




所以,我真的想知道,如何在Custom Adapter中分配(如果可能的话)SQLiteOpenHelper。任何帮助都是相关的。

  

所以,这是我编辑过的版本。静态类Duomenys的一切都很好,但在那一部分有一些下划线:



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

        View v = convertView;

//Here it underlines mContext and i really don't know from where to get it
        final Duomenys pavadinimas = new Duomenys(mContext);

        if (v == null){
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(R.layout.listview_layout, parent, false);

//Then it underlines () after Duomenys
            pavadinimas = new Duomenys();
pavadinimas.plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                
pavadinimas.db.assactivity(name, description, number);

                }
            });

           v.setTag(pavadinimas);
        }else {
        
//And it underlines pavadinimas, saying 
//"Cannot assign a value to final variable 'pavadinimas'  "
            pavadinimas = (Duomenys) v.getTag();
        }




2 个答案:

答案 0 :(得分:0)

将构造函数添加到Duomenys类

static class Duomenys{

        SQLiteDatabase sql;

        //Here i don't know how to assign my SQLiteOpenHelper,
        // so actually, I NEED HELP HERE
        ActivitiesDataBase db;
        public Duomenys(Context context){
            this.db = new ActivitiesDataBase(context);
        }

    }

然后在创建Duomenys对象时传递上下文final Duomenys pavadinimas = new Duomenys(mContext)

答案 1 :(得分:0)

所以,在@AgiMaulana的帮助下,我解决了这个问题。代码仅在CustomAdapter中更改,因此它是:



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

        View v = convertView;

        final Duomenys pavadinimas;

        if (v == null){
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            v = inflater.inflate(R.layout.listview_layout, parent, false);

            pavadinimas = new Duomenys(getContext());
            pavadinimas.plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                
                    pavadinimas.db.modifyNumber(name, newNumber);
                }
            });

        v.setTag(pavadinimas);
        }else {
            pavadinimas = (Duomenys) v.getTag();
        }




非常感谢你们!