如何根据选中的项目加载另一个ListView

时间:2017-10-19 11:54:22

标签: android sqlite listview listviewitem

您好我的代码有问题,我想显示我的第二个列表但是从第一个列表中选择第二个列表依赖于列选择的问题例如我的第一个列表包含产品的类别我点击一个类别它必须显示一个列表包含所选类别的产品,我创建了2表1 ere包含categoriesie和2 eme包含产品如果可能你可以帮助我

Cateegorie等级

public class CategorieActivity extends AppCompatActivity {
    Toolbar toolbar;
    ListView listview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_categorie);
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.app_name));
        listview=(ListView)findViewById(R.id.listView);
        //DBConnections db=new DBConnections(this);

        DBConnections myDbHelper = new DBConnections(this);
        try {
            myDbHelper.createDatabase();

        } catch (IOException ioe) {

            throw new Error("Unable to create database");
        }

        try {
            myDbHelper.openDatabase();

        }catch(SQLException sqle){

            throw sqle;
        }
        ArrayList arrayliste=myDbHelper.getAllrecord();


        ArrayAdapter<String> myAdapter=new ArrayAdapter<String>(CategorieActivity.this,android.R.layout.simple_list_item_1,arrayliste);
        //<String> myAdapter=new ArrayAdapter<String>(CategorieActivity.this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.categorie));
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent myIntent=new Intent(CategorieActivity.this,ListSatutsActivity.class);
                myIntent.putExtra("Categorie",listview.getItemAtPosition(position).toString());
                startActivity(myIntent);
            }
        });
        listview.setAdapter(myAdapter);
    }
}

DataBase类

public class DBConnections extends SQLiteOpenHelper {
    public static final String DBName="LastusStauts.db";
    public static final int version=1;
    public final static String DATABASE_PATH = "/data/data/com.example.hp.latestsatuts/databases/";
    private final Context myContext;
    private SQLiteDatabase myDataBase;
    public DBConnections(Context context) {

        super(context, DBName,null, version);

        this.myContext=context;
        Log.v("Error","Appel au constructeur");
    }
    public void createDatabase() throws IOException
    {

        boolean dbExist = checkDataBase();

        if(dbExist)
        {
            Log.v("DB Exists", "db exists");
            // By calling this method here onUpgrade will be called on a
            // writeable database, but only if the version number has been
            // bumped
            //onUpgrade(myDataBase, DATABASE_VERSION_old, DATABASE_VERSION);
        }

        boolean dbExist1 = checkDataBase();
        if(!dbExist1)
        {
            this.getReadableDatabase();
            try
            {
                this.close();
                copyDataBase();
            }
            catch (IOException e)
            {
                throw new Error("Error copying database");
            }
        }

    }
    //Check database already exist or not
    private boolean checkDataBase()
    {
        boolean checkDB = false;
        try
        {
            String myPath = DATABASE_PATH + DBName;
            File dbfile = new File(myPath);
            checkDB = dbfile.exists();
        }
        catch(SQLiteException e)
        {
        }
        return checkDB;
    }
    //Copies your database from your local assets-folder to the just created empty database in the system folder
    private void copyDataBase() throws IOException
    {

        InputStream mInput = myContext.getAssets().open(DBName);
        String outFileName = DATABASE_PATH + DBName;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[2024];
        int mLength;
        while ((mLength = mInput.read(mBuffer)) > 0) {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }
    //delete database
    public void db_delete()
    {
        File file = new File(DATABASE_PATH + DBName);
        if(file.exists())
        {
            file.delete();
            System.out.println("delete database file.");
        }
    }
    //Open database
    public void openDatabase() throws SQLException
    {
        String myPath = DATABASE_PATH + DBName;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public synchronized void closeDataBase()throws SQLException
    {
        if(myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase myDataBase) {

    }

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

    }
    public ArrayList getAllrecord(){

        ArrayList arraylist=new ArrayList();
        // Pour lire a partir de base de donnees
        SQLiteDatabase db=this.getReadableDatabase();
        //Cursor notre structure pour deplacer

        Cursor res =myDataBase.rawQuery("select * from Categories",null);
        res.moveToFirst();
        while(res.isAfterLast()==false){
            arraylist.add(res.getString(res.getColumnIndex("Categorie")));
            res.moveToNext();
        }
        return arraylist;
    }
}

0 个答案:

没有答案