0

我有一个小问题,我的服务以某种方式获取数据

         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 类扩展了。

提前致谢。

已编辑:感谢您的好评,这是我的 Fragment 课程

                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。等等......并将数据附加到列表或地图或数据库中,无论是最佳选择。如果用户转到第一页并下拉,则整个列表将被清除并从头开始调用服务。就像 android 4.x 中的 Gmail。

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

4

0 回答 0