Actually I can do a loop and log in console all rows of my table:
db.getAllPoids();
List<Poids> poids = db.getAllPoids();
for (Poids val : poids) {
String log = "Id: " + val.getId() + " ,Date: " + val.getDate_enr() + " ,Poids: " + val.getPoids() + " ,Evolution: " + val.getEvolution() ;
// Writing Contacts to log
Log.d("Name: ", log);
}
DatabaseHandler:
public List<Poids> getAllPoids() {
List<Poids> poidsList = new ArrayList<Poids>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_POIDS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Poids poid = new Poids();
poid.setId(Integer.parseInt(cursor.getString(0)));
poid.setPoids(Integer.parseInt(cursor.getString(1)));
poid.setDate_enr(cursor.getString(2));
poid.setEvolution(cursor.getString(3));
poid.setId_utilisateurs(Integer.parseInt(cursor.getString(4)));
// Adding contact to list
poidsList.add(poid);
} while (cursor.moveToNext());
}
But now Iwan't to do a better view, I need something like a table or liste, I know that they're listview exemple on google, but not with this method that I use.
And in my view I need to have3 rows: get the date in the first, the row "poids" in the second and n image view containing the id to delete the row on click. It is possible ? I don't know how to do.
PoidsAdapter:
public class PoidsAdapter extends ArrayAdapter<Poids> {
private Context mContext;
private List<Poids> mListPoids;
public PoidsAdapter(Context context, int resource, List<Poids> objects) {
super(context, resource, objects);
this.mContext = context;
this.mListPoids = objects;
}
@Override
public int getCount() {
return mListPoids.size();
}
@Override
public Poids getItem(int position) {
return mListPoids.get(position);
}
@Override
public View getView(final int position, View view, final ViewGroup parent) {
final holder holder;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listpoids, null);
holder = new holder();
holder.mTvTitle = (TextView) view.findViewById(R.id.textViewDate);
holder.mTvMediaName = (TextView) view.findViewById(R.id.textViewPoids);
holder.mImageUrl = (Button) view.findViewById(R.id.buttonSupprimer);
return view;
}
public class holder {
public Button mImageUrl;
public TextView mTvTitle;
public TextView mTvMediaName;
}
}
UPDATE 2:
I put a text view hidden to keep the id:
@Override
public View getView(final int position, View view, final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listpoids, null);
holder = new ViewHolder();
holder.mTvTitle = (TextView) view.findViewById(R.id.textViewDate);
holder.mTvMediaName = (TextView) view.findViewById(R.id.textViewPoids);
holder.poidsId = (TextView) view.findViewById(R.id.textViewPoidsId);
holder.mImageUrl = (Button) view.findViewById(R.id.buttonSupprimer);
Poids poids = mListPoids.get(position);
holder.mTvTitle.setText(poids.getDate_enr().toString());
holder.mTvMediaName.setText(String.valueOf(poids.getPoids()).toString() + "kg");
holder.poidsId.setText(String.valueOf(poids.getId()).toString());
return view;
}
Fragment:
public class MonPoidsFragment extends Fragment {
DatabaseHandler db = new DatabaseHandler(getActivity());
public MonPoidsFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//View rootView = inflater.inflate(R.layout.accueil_activity, container, false);
View view = inflater.inflate(R.layout.monpoids_activity, container, false);
final ListView listView=(ListView) view.findViewById(R.id.listView);
Button buttonAjouter = (Button)view.findViewById(R.id.buttonAjouter);
Button buttonSupprimer = (Button)view.findViewById(R.id.buttonSupprimer);
db = new DatabaseHandler(getActivity());
db.getAllPoids();
final List<Poids> poids = db.getAllPoids();
PoidsAdapter mAdapter = new PoidsAdapter(getActivity(), R.layout.listpoids, poids);
listView.setAdapter(mAdapter);
Log.d("getCount(): ", "" + mAdapter.getCount());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
poids.remove(position);
db.deletePoids(position);
}
});
buttonSupprimer.setOnClickListener(
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
poids.remove(position);
db.deletePoids(position);
}
})});
buttonAjouter.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = null;
fragment = new AjoutPoidsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
});
return view;
}
}
Database:
public void deletePoids(int rowID) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_POIDS, KEY_ID + " =? ", new String[]{String.valueOf(rowID)});
}
monpoids_activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="beforeDescendants"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:padding="10dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ajouter un poids"
android:id="@+id/buttonAjouter"
android:background="#70cbed"
android:textColor="#ffffff"
android:textAlignment="center"
android:layout_marginTop="20dp" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView" />
</LinearLayout>
</RelativeLayout>
listpoids.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#bbd3dc">
<TextView
android:id="@+id/textViewDate"
android:layout_gravity="center"
android:layout_width="0.7in"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="10dp" />
<TextView
android:layout_gravity="center"
android:layout_width="0.7in"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewPoids"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewPoidsId"
android:text="hidden"
android:visibility="gone"/>
<Button
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Supprimer"
android:id="@+id/buttonSupprimer" />
</LinearLayout>