I have sqlite database that have table(benefits) and designed like this
recipe percent name ID
1 100 Digestion 1
1 80 Lower ch 2
1 60 Immune sys 3
I want to create list that have two item.
first is name that i have no problem with
second is percent that i want to show like progress bar.
this is what i wrote to do this.
public class BenefitsFragment extends Fragment {
private JuiceDatabase db;
public String data;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_benefits, container, false);
db = new JuiceDatabase(super.getActivity());
//Ingredients Code
Cursor benefits = db.getbenefitsresult();
String[] from = new String[]
{"name","percent"};
int[] to = new int[]
{R.id.input,R.id.progressbar};
final SimpleCursorAdapter myadapter = new CustomSimpleCursorAdapter(
super.getActivity(),
R.layout.ingredients,
benefits,
from,
to);
final ListView list1 = (ListView) rootView.findViewById(R.id.list1);
list1.setAdapter(myadapter);
list1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (calculateHeight(list1))));
return rootView ;
}
private int calculateHeight(ListView list) {
int height = 0;
for (int i = 0; i < list.getCount(); i++) {
View childView = list.getAdapter().getView(i, null, list);
childView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
height+= childView.getMeasuredHeight();
}
//dividers height
height += list.getDividerHeight() * (list.getCount()+5);
return height;
}
}
list_layout.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/padding5"
android:paddingLeft="@dimen/padding5"
android:paddingRight="@dimen/padding5"
android:paddingTop="@dimen/padding5"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
ingredients.xml
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true">
<TextView
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="@dimen/list_name_height"
android:layout_alignWithParentIfMissing="true"
android:textSize="12sp"
android:textColor="@color/dark_grey"
android:gravity="center_vertical|center"
android:background="@android:color/transparent"
android:elevation="8sp"
android:elegantTextHeight="false"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:layout_below="@+id/input"
android:textSize="25sp"
android:longClickable="false"
style="@android:style/Widget.ProgressBar.Horizontal">
</ProgressBar>
CustomSimpleCursorAdabtor.class
public class CustomSimpleCursorAdapter extends SimpleCursorAdapter {
public CustomSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
}
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
updateProgressbar(view, cursor);
}
private void updateProgressbar(View view, Cursor cursor) {
ProgressBar progressBar = (ProgressBar) view
.findViewById(R.id.progressbar);
progressBar.setMax(100);
progressBar.setProgress(cursor.getInt(cursor
.getColumnIndex("percent")));
}
}
CursorAdaptor
public Cursor getbenefitsresult(){
SQLiteDatabase db = this.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String data=StoreData.data;
String sqlTables = "benefits";
qb.setTables(sqlTables);
Cursor c = db.rawQuery("SELECT 0 _id, name, percent FROM benefits WHERE recipe ="+data,null);
c.moveToFirst();
Log.d("percent",Integer.toString(c.getInt(c
.getColumnIndex("percent"))));
db.close();
return c;
}
After run the program when i click this fragment tab it go back to previous page!!!
whats the problem ?
sorry if its noobish question this is my first app.
Aucun commentaire:
Enregistrer un commentaire