如何在片段中实现无限滚动视图

时间:2013-08-12 12:21:35

标签: android android-view

我的服务以

的方式获取数据有一点问题
         jsonParser = new JSONParser();

     String userid = data.get(0);
    String GroupID = data.get(1);
    // String FromDate = fromDate;
    String FromDate = data.get(2);
    String UpToDate = data.get(3);
    String Category = data.get(4);
    String Location = data.get(5);
    String PageIndex = "1";
    String PageSize = "10";
    String body = String
            .format("{\"UserID\": \"%s\", \"GroupID\": \"%s\", \"FromDate\": \"%s\", \"UpToDate\": \"%s\", \"Category\": \"%s\", \"Location\": \"%s\", \"PageIndex\": \"%s\", \"PageSize\": \"%s\"}",
                    userid, GroupID, FromDate, UpToDate, Category,
                    Location, PageIndex, PageSize);
    JSONArray json1 = new JSONArray();
    json1 = jsonParser.getJSONFromUrl("http://###.com/GetEvents", body);

如果页面索引为2,则获取11-20,依此类推。 我的问题是如何能够设置数据,如果用户向下滚动它可以有更多的数据。我的意思是页面索引是2和前10个数据存储在地图或列表或数据库中。什么是最好的选择。我正在使用Fragment,我的java类扩展。

提前感谢。

EDITED: 谢谢你的好评, 这是我的片段类

                public class ArtFragment extends Fragment implements OnClickListener {

List<String> data = new ArrayList<String>();
User user;// = new User();
String userid = "";
String groupid = "";
String fromDate = "";
String toDate = "";
String category = "";
String location = "";

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


    View v = inflater.inflate(R.layout.music_layout, container, false);
    LinearLayout mLayout = (LinearLayout) v
            .findViewById(R.id.catagory_linearlayout);

    mLayout.setBackgroundResource(R.drawable.music_full);
    TableLayout tableLayout = (TableLayout) v.findViewById(R.id.maintable);
    tableLayout.removeAllViews();

    user = new User();
    WebServiceCaller webServiceCaller = new WebServiceCaller();
    DatabaseHandler db = new DatabaseHandler(getActivity());
    user = db.getUser();
    userid = user.getUserId();

    data.add(userid);// UserId
    data.add("6");// Group Id
    data.add("");// FromDate
    data.add("");// TODate
    data.add("Art");// Category
    data.add("");// Location
    db.close();
    JSONArray testresult = null;
    testresult = webServiceCaller.getServiceData(data);
    TableRow tableRow = new TableRow(v.getContext());
    if (testresult.length() > 0) {
        for (int i = 0; i < testresult.length(); i++) {

            String myname = "";
            String eventName = "";
            String fromDate = "";
            String companyName = "";
            String location = "";
            try {
                final JSONObject jo = (JSONObject) testresult.get(i);
                myname = jo.getString("DisplayName");
                eventName = jo.getString("EventName");
                fromDate = jo.getString("FromDate");
                companyName = jo.getString("CompanyName");
                location = jo.getString("Location");

                tableRow = new TableRow(v.getContext());
                tableRow.setPadding(10, 10, 10, 10);
                tableRow.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));

                TextView tv = new TextView(v.getContext());
                tv.setTextColor(getResources().getColor(
                        R.color.black_overlay));
                tableRow.addView(tv);
                tv.setText(Html.fromHtml(myname + eventName + fromDate
                        + companyName + location));
                // tv.setOnClickListener(this);

                tv.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {// Intent intent = new Intent();
                        Intent intent = new Intent(getActivity(),
                                TextActivity.class);
                        intent.putExtra("data", jo.toString());
                        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                });
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            tableLayout.addView(tableRow, new TableLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        }
        return v;
    } 

    return v;
}

public void onClick(View arg0) {
    startActivity(new Intent(getActivity(), TextActivity.class));
}

}

我只想加载前10个数据,如果用户向下滚动它再加载10个意味着11-20。等等......并在列表或地图或数据库中附加数据,无论最佳选择。如果用户进入第1页并向下拉,则清除整个列表并从头开始调用服务。与android 4.x中的Gmail一样。

我是android的新手,不知道如何实现这个?

0 个答案:

没有答案