Hello I am new to android..I am creating one application..in my application I am getting some data via json..it looks like this..
{
"interestsent":
[
{
"interestsent_user_id":369,
"name":"Biggy Bro",
"profile_id":"nj686317",
"image":"",
"cast":"Not Willing to specify",
"age":"34",
},
{
"interestsent_user_id":370,
"name":"Adam rose",
"profile_id":"nj686318",
"image":"",
"cast":"Not Willing to specify",
"age":"34",
},
{
"interestsent_user_id":371,
"name":"Bunny",
"profile_id":"nj686319",
"image":"",
"cast":"Not Willing to specify",
"age":"34",
},
{
"interestsent_user_id":372,
"name":"Santino",
"profile_id":"nj686320",
"image":"",
"cast":"Not Willing to specify",
"age":"34",
},
{
"interestsent_user_id":372,
"name":"Roseline",
"profile_id":"GM686321",
"image":"",
"cast":"Not Willing to specify",
"age":"34",
},
]
}
the above is my json response..I am able to display all the records in my listview,now what if I need to display three records only and then want to put "Load More" button and and after click on that remaining records will print..can any one help me?
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view_sent);
nointsent=(TextView)findViewById(R.id.no_intsent);
String strtexts = getIntent().getStringExtra("id");
System.out.println("<<<<<<<< id : " + strtexts);
INTEREST_SENT_URL = "my url";
// listview=(ListView)findViewById(R.id.list);
//ListView listview = this.getListView();
ListView listview = (ListView)findViewById(android.R.id.list);
// Making a request to url and getting response
data = new ArrayList<HashMap<String, String>>();
jsonStr = sh.makeServiceCall(INTEREST_SENT_URL, ServiceHandler.GET);
Log.d("Response first: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
interestsent = jsonObj.getJSONArray(INTEREST_SENT);
// looping through All Contacts
for (int i = 0; i < interestsent.length(); i++) {
JSONObject c = interestsent.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(INTEREST_USER_ID, c.getString(INTEREST_USER_ID));
map.put(INTEREST_SENT_NAME,c.getString(INTEREST_SENT_NAME));
map.put(INTEREST_SENT_PROFILE, c.getString(INTEREST_SENT_PROFILE));
map.put(INTEREST_SENT_IMAGE, c.getString(INTEREST_SENT_IMAGE));
map.put(INTEREST_SENT_CAST, c.getString(INTEREST_SENT_CAST));
map.put(INTEREST_SENT_AGE, c.getString(INTEREST_SENT_AGE)+" years");
map.put(INTEREST_SENT_LOCATION, c.getString(INTEREST_SENT_LOCATION));
// adding HashList to ArrayList
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
// LoadMore button
Button btnLoadMore = new Button(this);
btnLoadMore.setText("Load More");
// Adding Load More button to lisview at bottom
listview.addFooterView(btnLoadMore);
// Getting adapter
adapter = new CustomAdapterSent(getBaseContext(), data);
listview.setAdapter(adapter);
new LoadAlbums().execute();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getApplicationContext(), InterestDetails.class);
intent.putExtra("interestsent_user_id", aList.get(position).get(INTEREST_USER_ID));
startActivity(intent);
}
class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(InterestSent.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(false);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
sh = new ServiceHandler();
// Making a request to url and getting response
data = new ArrayList<HashMap<String, String>>();
jsonStr = sh.makeServiceCall(INTEREST_SENT_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
interestsent = jsonObj.getJSONArray(INTEREST_SENT);
// looping through All Contacts
for (int i = 0; i < interestsent.length(); i++) {
JSONObject c = interestsent.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(INTEREST_USER_ID, c.getString(INTEREST_USER_ID));
map.put(INTEREST_SENT_NAME,c.getString(INTEREST_SENT_NAME));
map.put(INTEREST_SENT_PROFILE, c.getString(INTEREST_SENT_PROFILE));
map.put(INTEREST_SENT_IMAGE, c.getString(INTEREST_SENT_IMAGE));
map.put(INTEREST_SENT_CAST, c.getString(INTEREST_SENT_CAST));
map.put(INTEREST_SENT_AGE, c.getString(INTEREST_SENT_AGE)+" years");
map.put(INTEREST_SENT_LOCATION, c.getString(INTEREST_SENT_LOCATION));
// adding HashList to ArrayList
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
if(interestsent == null || interestsent.length() == 0){
// Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();
nointsent.setText(" No Interest Sent ");
}
else
{
nointsent.setVisibility(View.INVISIBLE);
}
// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
if(aList == null){
aList = new ArrayList<HashMap<String, String>>();
aList.addAll(result);
adapter = new CustomAdapterSent(getBaseContext(), result);
setListAdapter(adapter);
}else{
aList.addAll(result);
adapter.notifyDataSetChanged();
}
}
}
Aucun commentaire:
Enregistrer un commentaire