Android列表视图与分页

时间:2014-08-24 07:27:40

标签: android android-fragments android-listview pagination android-linearlayout

我正尝试从ListView Fragment的{​​{3}}添加分页到我的应用ListViewJSONListView检索数据。除了Layout之外,一切都很好,Button的{​​{1}}未显示。即使LogCat也没有显示任何错误。这是我的代码......

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    this.mainContainer = container;
    rootView = inflater.inflate(R.layout.product_list_pagination, container,
            false);

    dmProductOptionsArray = new ArrayList<HashMap<String, String>>();
    dmProductList = (ListView) rootView.findViewById(R.id.dm_product_list);
    title    = (TextView)rootView.findViewById(R.id.dm_page_title);
    title.setVisibility(View.GONE);
    new ProductListAsyncTask(getActivity()).execute(siteURL);
    return rootView;
}

class ProductListAsyncTask extends
        AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {


    public ProductListAsyncTask(Activity parentActivity) {
        if (parentActivity == null) throw new IllegalArgumentException("parentActivity");          
        ..........
    }

    @Override
    protected void onPreExecute() {
        .....
    }

    protected ArrayList<HashMap<String, String>> doInBackground(
            String... urls) {
        ...     }

    protected void onProgressUpdate(String... progress) {
    }

    @Override
    protected void onPostExecute(               
            ArrayList<HashMap<String, String>> dmProductOptions) {

        if (this.progressDialog.isShowing()) {
            this.progressDialog.dismiss();
        } 
        title.setVisibility(View.VISIBLE);
        ProductListFragment.this.addPaginationButtons();
        ProductListFragment.this.loadList(0);
        ProductListFragment.this.CheckBtnBackGroud(0);
    }
}

private void addPaginationButtons() {
    int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
    val = val==0?0:1;
    noOfBtns=TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE+val;

    LinearLayout ll = (LinearLayout)rootView.findViewById(R.id.btnLay);

    paginationBtns    =new Button[noOfBtns];
    Log.d("DM APP VAL", String.valueOf(noOfBtns));

    for(int i=0;i<noOfBtns;i++)
    {
        paginationBtns[i] =   new Button(getActivity());
        paginationBtns[i].setBackgroundColor(getResources().getColor(android.R.color.transparent));
        paginationBtns[i].setText(""+(i+1));

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll.addView(paginationBtns[i], lp);

        final int j = i;
        paginationBtns[j].setOnClickListener(new OnClickListener() {

            public void onClick(View v)
            {
                loadList(j);
                CheckBtnBackGroud(j);
            }
        });
    }
}

public void CheckBtnBackGroud(int index)
{
    title.setText("Page "+(index+1)+" of "+noOfBtns);
    .........

}

public void loadList(int number)
{
    ArrayList<HashMap<String, String>> dmProductOptionsTempArray = new ArrayList<HashMap<String, String>>();;

    int start = number * NUM_ITEMS_PAGE;
    for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
    {
        if(i<dmProductOptionsArray.size())
        {
            HashMap<String, String> packageTempProduct = new HashMap<String, String>();
            packageTempProduct.put(TAG_PACKAGE_TITLE, dmProductOptionsArray.get(i).get(TAG_PACKAGE_TITLE));
            packageTempProduct.put(TAG_PACKAGE_DOWNLOAD, dmProductOptionsArray.get(i).get(TAG_PACKAGE_DOWNLOAD));
            dmProductOptionsTempArray.add(packageTempProduct);
        }
        else
        {
            break;
        }
    }
    dmProductAdapter = new ProductListAdapter(getActivity(),
            dmProductOptionsTempArray);
    dmProductList.setAdapter(dmProductAdapter);
    dmProductList.setOnTouchListener(new OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside
        // ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // Disallow the touch request for parent scroll on touch of
            // child view
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });
}

Xml代码......

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/dm_page_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/black"
    android:gravity="center"
    android:textSize="16sp"
    android:background="@android:color/darker_gray"
    android:padding="10dp"/>

<ListView
    android:id="@+id/dm_product_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

</ListView>

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/btnLay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    </LinearLayout>
</HorizontalScrollView>

它显示以下输出没有分页按钮。执行时可能有问题  private void addPaginationButtons() {方法。提前致谢

更新日志

Log.d("DM APP VAL", String.valueOf(noOfBtns));

在LogCat中

08-24 04:59:29.045: D/DM APP VAL(1858): 3

enter image description here

0 个答案:

没有答案