单选按钮滚动时弄乱了

时间:2019-06-18 16:19:53

标签: java android sqlite

我在列表视图中有57个单选按钮,我想将那些单选按钮分为19组,所以每个组有3个单选按钮。但是当我滚动它时,视图被弄乱了,并且自动添加了单选按钮,变成了两倍。 数据是从sqlite数据库获取的。

Klasifikasi活动

public class KlasifikasiActivity extends AppCompatActivity {
    private ListView lvKlasifikasi;
    private ListKlasifikasiAdapter adapter;
    private List<Klasifikasi> mKlasifikasiList;
    private DatabaseHelper mDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_klasifikasi);
        lvKlasifikasi = (ListView)findViewById(R.id.stepSwitcher);
        mDBHelper = new DatabaseHelper(this);

        File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
        if (false == database.exists()){
            mDBHelper.getReadableDatabase();
            if (copyDatabase(this)){
                Toast.makeText(this,"COPY SUCCESS",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"COPY ERROR",Toast.LENGTH_SHORT).show();
                return;
            }
        }
        //get db metode
        mKlasifikasiList = mDBHelper.getListKlasifikasiByGroup();
        adapter = new ListKlasifikasiAdapter(this,mKlasifikasiList);
        lvKlasifikasi.setAdapter(adapter);
    }

    private boolean copyDatabase(Context context) {
        try {
            InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
            String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
            OutputStream outputStream = new FileOutputStream(outFileName);
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buff)) > 0){
                outputStream.write(buff,0,length);
            }
            outputStream.flush();
            outputStream.close();
            Log.w("MainActivity","DB copied");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

DatabaseHelper和函数方法

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "database.db";
    public static final String DBLOCATION = "/data/data/com.example.damar.finalproject/databases/";
    private Context mContext;
    private SQLiteDatabase mDatabase;

    public DatabaseHelper(Context context){
        super(context,DBNAME,null,1);
        this.mContext = context;
    }
    @Override
    public void onCreate(SQLiteDatabase db){

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

    }

    public void openDatabase(){
        String dbPath = mContext.getDatabasePath(DBNAME).getPath();
        if (mDatabase != null && mDatabase.isOpen()){
            return;
        }
        mDatabase = SQLiteDatabase.openDatabase(dbPath, null,SQLiteDatabase.OPEN_READWRITE);
    }
    public void closeDatabase(){
        if (mDatabase!=null){
            mDatabase.close();
        }
    }
public List<Klasifikasi> getListKlasifikasiByGroup(){
        Klasifikasi klasifikasi = null;
        List<Klasifikasi> klasifikasiList = new ArrayList<>();
        openDatabase();
        Cursor cursor = mDatabase.rawQuery("SELECT * FROM klasifikasi group by group_index", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()){
            klasifikasi = new Klasifikasi(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getInt(4));
            klasifikasiList.add(klasifikasi);
            cursor.moveToNext();
        }
        cursor.close();
        closeDatabase();
        return klasifikasiList;
    }
}

1 个答案:

答案 0 :(得分:0)

您是否刚从db中获取列表并设置为适配器之前检查过列表