I have created teh 2 xml files, and my custom view of 3 rows, 2 firsts are texts, and the last a button to delete data:
xml of the activity:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView" />
listpoids.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textViewDate"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textViewPoids" />
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supprimer"
android:id="@+id/buttonSupprimer" />
</LinearLayout>
I did my adaper:
public class PoidsAdapter extends ArrayAdapter<String> {
private Context mContext;
private List<String> mListPoids;
public PoidsAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
this.mContext = context;
this.mListPoids = objects;
}
@Override
public int getCount() {
return mListPoids.size();
}
@Override
public String 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;
}
}
And the database handler to get all of the rows:
public List<Poids> getAllPoids() {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<Poids> poidsList = new ArrayList<Poids>();
Cursor cursor = db.rawQuery("SELECT * from " + TABLE_POIDS,
new String[] {});
/*
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.setPoids(Integer.parseInt(cursor.getString(1)));
poid.setDate_enr(cursor.getString(2));
// Adding contact to list
poidsList.add(poid);
} while (cursor.moveToNext());
}
// return contact list
return poidsList;
}
To finish how I call it:
db.getAllPoids();
List<Poids> poids = db.getAllPoids();
ListView listView=(ListView) view.findViewById(R.id.listView);
newData = new ArrayList<String>();
for (Poids val : poids) {
newData.add(val.getDate_enr());
newData.add(String.valueOf(val.getPoids()));
//String log = "Id: " + val.getId() + " ,Date: " + val.getDate_enr() + " ,Poids: " + val.getPoids() + " ,Evolution: " + val.getEvolution() ;
// Writing Contacts to log
//Log.d("Name: ", log);
}
PoidsAdapter mAdapter = new PoidsAdapter(getActivity(), R.layout.listpoids, newData);
listView.setAdapter(mAdapter);
But it doesn't work, I'm in a fragment.
Aucun commentaire:
Enregistrer un commentaire