列表视图中的进度条

时间:2015-06-27 12:00:56

标签: android sqlite listview progress-bar

我有sqlite数据库,它有表(好处)并且设计得像这样

recipe    percent    name         ID
1         100        Digestion    1
1         80         Lower ch     2
1         60         Immune sys   3

我想创建包含两个项目的列表。

首先是我没有问题的名字

第二个是我希望显示为进度条的百分比。

这就是我写的内容。

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

fragment_benefits.xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:scrollbars="none">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/group"
        android:layout_marginRight="15sp"
        android:layout_marginLeft="15sp"
        android:layout_marginTop="15sp"
        android:layout_marginBottom="15sp"
        android:elevation="15dp">
        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingRight="5sp"
            android:paddingLeft="5sp"
            android:paddingTop="5sp"
            android:paddingBottom="10sp"
            android:text="benefits"
            android:textSize="22sp"
            android:textColor="@color/dark_grey"
            android:gravity="center_horizontal" />
        <View style="@style/divider1"/>

        <ListView
            android:id="@+id/list1"
            android:layout_width="fill_parent"
            android:layout_height="0sp"
            android:scrollbars="none"
            android:paddingTop="5sp"
            android:paddingLeft="5sp"
            android:paddingRight="5sp"
            android:layout_marginBottom="5sp"
            android:layout_weight="1"
            android:clickable="false">
        </ListView>
    </LinearLayout>
</LinearLayout>

ingredients.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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;

}

单击此片段选项卡后运行程序后,返回上一页!

问题是什么?

对不起,如果这是我的第一个应用程序的noobish问题。

编辑1:

    public class MainRecipe extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "مواد لازم", "ارزش غذایی", "فواید" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_recipe);

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

1 个答案:

答案 0 :(得分:0)

我自己找到了问题。 那是在CustomSimpleCursorAdapter

我必须使用setProgressbar代替updateprogressbar