2011-06-24 20 views
0

我必須在ListView上應用分頁概念我的列表視圖包含從Web服務分析的數據。下面的代碼給出了我如何在列表視圖中顯示數據,如下所示。分頁ListView中的Android

try { 
    ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist"); 

     System.out.println("...serialized data.."+arl); 
lv1 = (ListView) findViewById(R.id.lstlodgingresult); 
    adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view, 
        new String[] { "Srno", "Names", "URL", "Address1", "Address2", "Telephone", "Category", "PetH", 
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1, R.id.txtaddress2, R.id.txtphone, R.id.txtcategory, 
R.id.txtpetpolicyH, R.id.txtpetpolicyC } 
); 
lv1.setScrollbarFadingEnabled(false); 
lv1.refreshDrawableState(); 
lv1.setAdapter(adapter); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
+0

你可以給更多的明確的想法,你所面對現在在上面的代碼是什麼問題呢? – Android

+0

什麼是分頁? – Nallath

+0

你好Nallath,分頁是你可以通過頁面顯示你的結果的概念。以上代碼正在工作。但它填充ListView的所有值開始我想要的是ListView默認應顯示10條記錄,當用戶向下滾動到最後一條記錄時,它將獲取下10條記錄。它應該以類似的方式工作。 – sandy

回答

2

您只需要在創建的列表中添加頁腳視圖。然後對於頁腳視圖(可能是按鈕/圖像/文本),爲其設置ClickListener,並在Listener中將這些項目添加到列表中,然後再次刷新活動。我正在添加一個小教程來幫助你。

我用下面的方法分頁:

List類:

public class customListView extends Activity implements OnClickListener{ 

    private static class EfficientAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 
    Context context; 
    public EfficientAdapter(Context context) { 
     this.context = context; 
     mInflater = LayoutInflater.from(context); 

    } 

    public int getCount() { 
     return add_Names.size(); 
    } 

    public Object getItem(int position) { 
    return position; 
    } 

    public long getItemId(int position) { 
    return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
    convertView = mInflater.inflate(R.layout.listcontent, null); 
    holder = new ViewHolder(); 
    holder.text = (TextView) convertView 
    .findViewById(R.id.txt1); 
    holder.text2 = (TextView) convertView 
    .findViewById(R.id.txt2); 
    holder.text3 = (TextView) convertView 
    .findViewById(R.id.txt3); 



    convertView.setTag(holder); 
    } else { 
    holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.text.setText(add_Names.get(position).toString()); 
    holder.text2.setText(location.get(position).toString()); 
    holder.text3.setText(details.get(position).toString()); 

    return convertView; 
    } 

    static class ViewHolder { 
    TextView text; 
    TextView text2; 
    TextView text3; 
    } 
    }//end of efficient Adapter Class 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
     adapter = new EfficientAdapter(this); 

    l1 = (ListView) findViewById(R.id.ListView01); 
    View footerView = 
     ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false); 


    l1.addFooterView(footerView); 



    l1.setAdapter(adapter); 
    mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout); 
    more = (Button) footerView.findViewById(R.id.moreButton); 
    more.setOnClickListener(this); 

    l1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
     Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show(); 

     } 
    });    
    } 

@Override 
    public void onClick(View v) { 
     switch(v.getId()) 
     { 
     case R.id.moreButton: 
      //Your code to add some more data into list and then call the following to refresh your lits 
     adapter.notifyDataSetChanged(); 
      break; 
     }//end of switch 
}//end of onClick 


}//end of Custom List view class 

layout_footerview.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="wrap_content" 
android:paddingTop="7dip" 
android:paddingBottom="7dip" 
android:orientation="horizontal" 
android:gravity="center"> 
<LinearLayout 
android:id="@+id/footer_layout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:gravity="center" 
android:layout_gravity="center"> 

<Button 
    android:text="Get more.." 
    android:id="@+id/moreButton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="14dip" 
    android:textStyle="bold"> 
</Button> 

</LinearLayout> 
</LinearLayout> 

listview.xml:

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

<ListView android:id="@+id/ListView01" android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
</ListView> 
</RelativeLayout> 

列表的content.xml:(修改爲u想成爲你的列表行)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView android:id="@+id/image1" android:layout_width="wrap_content"   android:layout_height="wrap_content" 
android:src="@drawable/icon"></ImageView> 

    <TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:id="@+id/txt1"  android:layout_toRightOf="@+id/image1" 
    android:text="Test Description" android:textSize="15dip" android:textStyle="bold">  
</TextView> 

<TextView android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:id="@+id/txt2" android:layout_below="@+id/txt1" android:layout_toRightOf="@+id/image1" 
android:text="Address" android:textSize="10dip"></TextView> 

<TextView android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:id="@+id/txt3" android:layout_below="@+id/txt2" android:layout_toRightOf="@+id/image1" 
android:text="Details" android:textSize="10dip" ></TextView> 

    </RelativeLayout>  

我跳上這將definetly幫助你!

將此標記爲true和UpVote;如果這對你有幫助。

感謝 沙阿..

+1

hello sHaH,當我試圖運行這段代碼時,它只顯示ListView中的GetMore按鈕。 – sandy

+0

你還在做錯誤?對不起,晚了 – Shah

+0

謝謝sHaH我已經完成了分頁。 – sandy