jeudi 1 janvier 2015

onListItemClick() on listfragment to retrieve database id

I have a listfragment which display name of objects stored in a sqlitedatabase and each object has a mac adress and a password which is also stored in the same database how can i do to display the mac adress and the password of the item who is clicked and not all the password or the mac adresses .


Here is the List Fragment with OnListItemClick



public class F1_fr extends ListFragment {
View rootview;
TextView textView1;
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
SQLiteDatabase db;
ListView listView;

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c2 = db.rawQuery("SELECT * FROM test2", null);

while(c2.moveToNext())
{

Toast.makeText(getActivity(), c2.getString(0), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), c2.getString(1), Toast.LENGTH_LONG).show();

}
}

@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.f1_lay,container,false);
textView1=(TextView)rootview.findViewById(R.id.textView1);
db = getActivity().openOrCreateDatabase("testDB2", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS test2(mac VARCHAR,mdp VARCHAR,obj VARCHAR);");

aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
setListAdapter(aa);



Cursor cursor = db.rawQuery("SELECT * FROM test2", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())

{
do {
arrayList.add(cursor.getString(2));


} while (cursor.moveToNext());
}


rootview.findViewById(R.id.semi_transparent).

setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){

Intent intent = new Intent(getActivity(), ajout.class);
startActivityForResult(intent, 2);

}

}

);
return rootview;

}

Aucun commentaire:

Enregistrer un commentaire