mercredi 31 décembre 2014

display informations from database by clicking listview

I want to display differents informations depending on what object is clicked i have a listfragment which display only name of objects like : KEYS,SHOES,USB and they are stored in my data base but each element has other information who are already stored but not shown for example KEYS has also a mac adress which is for example 0.0.0.1 and a password which is 123 .


So i want to display the mac adress and the password which correspond to the object who is clicked so who is in the same line in my database.



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);

}

@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;

}


and the layout :



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="match_parent">



<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@id/android:list"

android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />

<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:text="" />


<com.getbase.floatingactionbutton.AddFloatingActionButton
android:id="@+id/semi_transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_plusIconColor="@color/primaryColorDark"
fab:fab_colorNormal="@color/primaryColor"
fab:fab_colorPressed="@color/AccentColor"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />


</RelativeLayout>


I already put the method to click item in the list.


Thanks in advance for your help


Aucun commentaire:

Enregistrer un commentaire