我只需单击一下,即可在sqlite数据库中插入2460行,这是我的问题:是否有任何方法可以改进此代码以使其更有效并获得更好的性能?

在这种情况下,保持DBHelper是件好事吗,是否需要在具有很多查询的Listview中显示数据?

你怎么看 ?

谢谢,对不起我的英语

我的课:

public class MainActivity <ButtonListener> extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnSimple = (Button) findViewById(R.id.buttonviewrec1);
    btnSimple.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intent = new Intent (v.getContext(),ViewTeam.class);
            startActivityForResult(intent, 0);

        }
    });

    Button btnasearch = (Button) findViewById(R.id.searchbtn1);
    btnasearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String tmname1= "spurs";
            String tmopponent1= "lal";
            String tmdate1= "26/12/2014 12:00";

            String tmname2= "Maavs";
            String tmopponent2= "Raps";
            String tmdate2= "22/12/2014 03:30";

            String tmname3= "Clips";
            String tmopponent3= "Bulls";
            String tmdate3= "19/12/2014 01:30";

        TeamModel pm;
        DBHelper db;

                db = new DBHelper(getApplicationContext());
                db.getWritableDatabase();
                pm = new TeamModel();

            pm.teamname=tmname1;
            pm.teamopponent=tmopponent1;
            pm.teamdate=tmdate1;

                Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
                        + pm.teamopponent + "" + pm.teamdate);
                db.addTeam(pm);

            pm.teamname=tmname2;
            pm.teamopponent=tmopponent2;
            pm.teamdate=tmdate2;

                Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
                        + pm.teamopponent + "" + pm.teamdate);
                db.addTeam(pm);

            pm.teamname=tmname3;
            pm.teamopponent=tmopponent3;
            pm.teamdate=tmdate3;


                 Log.i("teamname,teamopponent,teamdate", "" + pm.teamname + ""
                            + pm.teamopponent + "" + pm.teamdate);


        db.addTeam(pm);

        }
    });


}


我的DBHelper

public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
    super(context, DATABASENAME, null, 33);
    c = context;
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE if not exists teamstable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "teamname"
            + " TEXT,"
            + "teamopponent"
            + " TEXT,"
            + "teamdate" + " TEXT)");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + teamstable);
    onCreate(db);
}

//Add record
public void addTeam(TeamModel teamitem) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("teamname", teamitem.teamname);
    contentValues.put("teamopponent", teamitem.teamopponent);
    contentValues.put("teamdate", teamitem.teamdate);
    db.insert("teamstable", null, contentValues);
    db.close();
}


我的TeamModel班

public class TeamModel {

public String  teamname="", teamopponent="", teamdate="";


public String getTeamname() {
    return teamname;
}

public void setTeamname(String teamname) {
    this.teamname = teamname;
}

public String getTeamopponent() {
    return teamopponent;
}

public void setTeamopponent(String teamopponent) {
    this.teamopponent = teamopponent;
}

public String getTeamdate() {
    return teamdate;
}

public void setTeamdate(String teamdate) {
    this.teamdate = teamdate;
} }

最佳答案

好吧,您应该使用事务,它们将大大提高性能。另外,我建议您重构代码。首先,我将为您的实体创建DAO:

public interface TeamDAOProtocol {
    void addTeam(ArrayList<TeamModel> entities);
}

public class TeamDAO implements TeamDAOProtocol {
    void addTeam(ArrayList<TeamModel> entities) {
        if (entities == null) return;

        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        for (TeamModel entity : entities)
            try {
                ContentValues contentValues = new ContentValues();
                contentValues.put("teamname", entity.getTeamname());
                contentValues.put("teamopponent", entity.getTeamopponent());
                contentValues.put("teamdate", entity.getTeamdate());
                db.insert("teamstable", null, contentValues);
                db.setTransactionSuccessful();
            } catch(Exception e) {
            //Error in between database transaction
                e.printStackTrace();
        }
        db.endTransaction();
        db.close();
    }

}


还要通过getters / setters使用TeamModel的属性,因为您的“ pm.teamopponent”会破坏封装。

10-06 07:01