lundi 26 octobre 2015

Android Receive position of listview to add data in other activity using SQLite

I want to request data from a database. In the list I want to Display only the title and if you click it I want to show more detailed info. How would I get the position of the ListItem and show the data that belongs to that one list item? Here's my code:

FragmentRonde1.java

public class FragmentRonde1 extends android.support.v4.app.Fragment {

public ListView lv1;
public TextView tv1, tv2;
MyDbHandler db;
SimpleCursorAdapter dataAdapter;

public FragmentRonde1() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootview = inflater.inflate(R.layout.fragment_ronde1, container, false);
    ((MainActivity) getActivity()).setTitle("Workshops Ronde 1");

    db = new MyDbHandler(getActivity());

    db.InsertValues();
    final Cursor cursor=db.GetAllData();
    String from [] = new String[]{db.COLUMN_TITLE};
    int to [] = new int[] {R.id.workshopTitle1};
    dataAdapter = new SimpleCursorAdapter(getActivity(),R.layout.row_layout_workshop, cursor, from, to, 0);

    lv1 = (ListView)rootview.findViewById(R.id.Ronde1lv);
    lv1.setAdapter(dataAdapter);

    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent listClicked = new Intent(getActivity(), ListClicked.class);
            startActivity(listClicked);

            tv1 = (TextView)rootview.findViewById(R.id.listClickedTitle);
            tv2 = (TextView)rootview.findViewById(R.id.listClickedDescription);

        }
    });

    return rootview;
    }
}

MyDbHandler.java

public class MyDbHandler extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 6;
private static String DATABASE_NAME = "workshopDB";
private static String TABLE_WORKSHOPS = "workshops";

public static String COLUMN_ID = "_id";
public static String COLUMN_TITLE= "title";
public static String COLUMN_BESCHRIJVING = "beschrijving";
public static final String COLUMN_MGT = "mgt";
public static final String COLUMN_DOC = "doc";
public static final String COLUMN_OOP = "oop";
public static final String COLUMN_BEGELEIDER = "begeleider";
public static final String COLUMN_DEELNEMERS = "deelnemers";
public static final String COLUMN_RONDE1 = "ronde1";
public static final String COLUMN_RONDE2 = "ronde2";
public static final String COLUMN_RONDE3 = "ronde3";
public static final String COLUMN_LOKAAL1 = "lokaal1";
public static final String COLUMN_LOKAAL2 = "lokaal2";
public static final String COLUMN_LOKAAL3 = "lokaal3";

public MyDbHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS workshops(_id INT, title VARCHAR, beschrijving VARCHAR);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE workshops;");
    db.execSQL("CREATE TABLE IF NOT EXISTS workshops(_id INT, title VARCHAR, beschrijving VARCHAR);");
}


public void InsertValues()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");


    db.close();
}

public Cursor GetAllData()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM workshops;", null);
    return c;
}

}

I really hope someone can help me on this because I have no idea how to do it since I'm fairly new to this.

Aucun commentaire:

Enregistrer un commentaire