dimanche 9 octobre 2016

How to Add Two checkbox dynamically in every row of Gridview

I'm new to android i have a simple program with SQLite Connectivity and it works fine but i want to add one functionality that i have one gridview and there is some rows inserted. i want to add two check box dynamically at every row. This is output.. enter image description here

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:fastScrollAlwaysVisible="true"
    android:fastScrollEnabled="true"
    android:numColumns="4">
</GridView>

JAVA File

SQLiteDatabase db;
GridView gd;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_data);
    gd=(GridView)findViewById(R.id.grid);

    try
    {
        db=openOrCreateDatabase("demo", Context.MODE_PRIVATE,null);
    }
    catch (Exception e)
    {
        Toast.makeText(ViewData.this,"DB CREATE "+e.toString(), Toast.LENGTH_SHORT).show();
    }

    ArrayList<String> arrayList=new ArrayList<String>();

    try
    {
        Cursor res = db.rawQuery("SELECT * FROM tbl ORDER BY id", null);
        res.moveToNext();
        if(res.moveToFirst())
        {
            do
            {
                arrayList.add(res.getString(res.getColumnIndex("id")));
                arrayList.add(res.getString(res.getColumnIndex("name")));
                arrayList.add(res.getString(res.getColumnIndex("enrollment")));
                arrayList.add(res.getString(res.getColumnIndex("rollno")));

                ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
                gd.setAdapter(arrayAdapter);
            }
            while (res.moveToNext());
        }
    }
    catch (Exception e)
    {
        Toast.makeText(ViewData.this,"SELECT "+e.toString(), Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onBackPressed()
{
    finishAffinity();
    startActivity(new Intent(getApplicationContext(),MainActivity.class));
}

}




Aucun commentaire:

Enregistrer un commentaire