001.
package
com.myapp;
002.
003.
import
java.util.ArrayList;
004.
import
java.util.HashMap;
005.
006.
import
org.json.JSONArray;
007.
import
org.json.JSONException;
008.
import
org.json.JSONObject;
009.
010.
import
android.os.Bundle;
011.
import
android.app.Activity;
012.
import
android.view.Menu;
013.
import
android.view.View;
014.
import
android.widget.Button;
015.
import
android.widget.ListView;
016.
import
android.widget.SimpleAdapter;
017.
018.
public
class
MainActivity
extends
Activity {
019.
020.
public
String strJSON;
021.
public
int
currentPage =
1
;
022.
public
ListView lisView1;
023.
024.
public
Button btnNext;
025.
public
Button btnPre;
026.
027.
@Override
028.
public
void
onCreate(Bundle savedInstanceState) {
029.
super
.onCreate(savedInstanceState);
030.
setContentView(R.layout.activity_main);
031.
032.
033.
lisView1 = (ListView)findViewById(R.id.listView1);
034.
035.
036.
btnNext = (Button) findViewById(R.id.btnNext);
037.
038.
btnNext.setOnClickListener(
new
View.OnClickListener() {
039.
public
void
onClick(View v) {
040.
currentPage = currentPage +
1
;
041.
ShowData();
042.
}
043.
});
044.
045.
046.
btnPre = (Button) findViewById(R.id.btnPre);
047.
048.
btnPre.setOnClickListener(
new
View.OnClickListener() {
049.
public
void
onClick(View v) {
050.
currentPage = currentPage -
1
;
051.
ShowData();
052.
}
053.
});
054.
055.
056.
strJSON =
"[{\"ItemID\":\"Items 01\",\"Name\":\"Data Row 01\",\"Description\":\"Desc 01\"}"
+
057.
",{\"ItemID\":\"Items 02\",\"Name\":\"Data Row 02\",\"Description\":\"Desc 02\"}"
+
058.
",{\"ItemID\":\"Items 03\",\"Name\":\"Data Row 03\",\"Description\":\"Desc 03\"}"
+
059.
",{\"ItemID\":\"Items 04\",\"Name\":\"Data Row 04\",\"Description\":\"Desc 04\"}"
+
060.
",{\"ItemID\":\"Items 05\",\"Name\":\"Data Row 05\",\"Description\":\"Desc 05\"}"
+
061.
",{\"ItemID\":\"Items 06\",\"Name\":\"Data Row 06\",\"Description\":\"Desc 06\"}"
+
062.
",{\"ItemID\":\"Items 07\",\"Name\":\"Data Row 07\",\"Description\":\"Desc 07\"}"
+
063.
",{\"ItemID\":\"Items 08\",\"Name\":\"Data Row 08\",\"Description\":\"Desc 08\"}"
+
064.
",{\"ItemID\":\"Items 09\",\"Name\":\"Data Row 09\",\"Description\":\"Desc 09\"}"
+
065.
",{\"ItemID\":\"Items 10\",\"Name\":\"Data Row 10\",\"Description\":\"Desc 10\"}"
+
066.
",{\"ItemID\":\"Items 11\",\"Name\":\"Data Row 11\",\"Description\":\"Desc 11\"}"
+
067.
",{\"ItemID\":\"Items 12\",\"Name\":\"Data Row 12\",\"Description\":\"Desc 12\"}"
+
068.
",{\"ItemID\":\"Items 13\",\"Name\":\"Data Row 13\",\"Description\":\"Desc 13\"}"
+
069.
",{\"ItemID\":\"Items 14\",\"Name\":\"Data Row 14\",\"Description\":\"Desc 14\"}"
+
070.
",{\"ItemID\":\"Items 15\",\"Name\":\"Data Row 15\",\"Description\":\"Desc 15\"}"
+
071.
",{\"ItemID\":\"Items 16\",\"Name\":\"Data Row 16\",\"Description\":\"Desc 16\"}"
+
072.
",{\"ItemID\":\"Items 17\",\"Name\":\"Data Row 17\",\"Description\":\"Desc 17\"}"
+
073.
",{\"ItemID\":\"Items 18\",\"Name\":\"Data Row 18\",\"Description\":\"Desc 18\"}"
+
074.
",{\"ItemID\":\"Items 19\",\"Name\":\"Data Row 19\",\"Description\":\"Desc 19\"}"
+
075.
",{\"ItemID\":\"Items 20\",\"Name\":\"Data Row 20\",\"Description\":\"Desc 20\"}"
+
076.
",{\"ItemID\":\"Items 21\",\"Name\":\"Data Row 21\",\"Description\":\"Desc 21\"}"
+
077.
",{\"ItemID\":\"Items 22\",\"Name\":\"Data Row 22\",\"Description\":\"Desc 22\"}"
+
078.
",{\"ItemID\":\"Items 23\",\"Name\":\"Data Row 23\",\"Description\":\"Desc 23\"}"
+
079.
",{\"ItemID\":\"Items 24\",\"Name\":\"Data Row 24\",\"Description\":\"Desc 24\"}"
+
080.
",{\"ItemID\":\"Items 25\",\"Name\":\"Data Row 25\",\"Description\":\"Desc 25\"}"
+
081.
",{\"ItemID\":\"Items 26\",\"Name\":\"Data Row 26\",\"Description\":\"Desc 26\"}"
+
082.
",{\"ItemID\":\"Items 27\",\"Name\":\"Data Row 27\",\"Description\":\"Desc 27\"}"
+
083.
",{\"ItemID\":\"Items 28\",\"Name\":\"Data Row 28\",\"Description\":\"Desc 28\"}"
+
084.
",{\"ItemID\":\"Items 29\",\"Name\":\"Data Row 29\",\"Description\":\"Desc 29\"}"
+
085.
",{\"ItemID\":\"Items 30\",\"Name\":\"Data Row 30\",\"Description\":\"Desc 29\"}"
+
086.
",{\"ItemID\":\"Items 31\",\"Name\":\"Data Row 31\",\"Description\":\"Desc 31\"}]"
;
087.
088.
ShowData();
089.
}
090.
091.
public
void
ShowData()
092.
{
093.
try
{
094.
JSONArray data =
new
JSONArray(strJSON);
095.
096.
int
displayPerPage =
5
;
097.
int
TotalRows = data.length();
098.
int
indexRowStart = ((displayPerPage*currentPage)-displayPerPage);
099.
int
TotalPage =
0
;
100.
if
(TotalRows<=displayPerPage)
101.
{
102.
TotalPage =
1
;
103.
}
104.
else
if
((TotalRows % displayPerPage)==
0
)
105.
{
106.
TotalPage =(TotalRows/displayPerPage) ;
107.
}
108.
else
109.
{
110.
TotalPage =(TotalRows/displayPerPage)+
1
;
111.
TotalPage = (
int
)TotalPage;
112.
}
113.
int
indexRowEnd = displayPerPage * currentPage;
114.
if
(indexRowEnd > TotalRows)
115.
{
116.
indexRowEnd = TotalRows;
117.
}
118.
119.
120.
if
(currentPage >= TotalPage)
121.
{
122.
btnNext.setEnabled(
false
);
123.
}
124.
else
125.
{
126.
btnNext.setEnabled(
true
);
127.
}
128.
129.
130.
if
(currentPage <=
1
)
131.
{
132.
btnPre.setEnabled(
false
);
133.
}
134.
else
135.
{
136.
btnPre.setEnabled(
true
);
137.
}
138.
139.
140.
int
RowID =
1
;
141.
ArrayList<HashMap<String, String>> MyArrList =
new
ArrayList<HashMap<String, String>>();
142.
HashMap<String, String> map;
143.
144.
145.
if
(currentPage >
1
)
146.
{
147.
RowID = (displayPerPage * (currentPage-
1
)) +
1
;
148.
}
149.
150.
for
(
int
i = indexRowStart; i < indexRowEnd; i++){
151.
JSONObject c = data.getJSONObject(i);
152.
map =
new
HashMap<String, String>();
153.
map.put(
"RowID"
, String.valueOf(RowID));
154.
map.put(
"ItemID"
, c.getString(
"ItemID"
));
155.
map.put(
"Name"
, c.getString(
"Name"
));
156.
map.put(
"Description"
, c.getString(
"Description"
));
157.
MyArrList.add(map);
158.
RowID = RowID +
1
;
159.
160.
}
161.
162.
SimpleAdapter sAdap;
163.
sAdap =
new
SimpleAdapter(MainActivity.
this
, MyArrList, R.layout.activity_column,
164.
new
String[] {
"RowID"
,
"ItemID"
,
"Name"
,
"Description"
},
new
int
[] {R.id.ColRowID, R.id.ColItemID, R.id.ColName, R.id.ColDescription});
165.
lisView1.setAdapter(sAdap);
166.
167.
}
catch
(JSONException e) {
168.
169.
e.printStackTrace();
170.
}
171.
}
172.
173.
174.
@Override
175.
public
boolean
onCreateOptionsMenu(Menu menu) {
176.
getMenuInflater().inflate(R.menu.activity_main, menu);
177.
return
true
;
178.
}
179.
180.
}