list view with single contact activity in android - android

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_list);
contactList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
int itemPosition = position;
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String cost = ((TextView) view.findViewById(R.id.email))
.getText().toString();
// String add = ((TextView) view.findViewById(R.id.address))
// .getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile))
.getText().toString();
String price =((TextView) view.findViewById(R.id.price)).getText().toString();
String amount=((TextView) view.findViewById(R.id.mobile_labels)).getText().toString();
//String sweet = ((TextView) view.findViewById(R.id.home))
// .getText().toString();
// Starting single contact activity
Intent in = new Intent(SearchActivity.this,
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
//in.putExtra(TAG_PHONE_MOBILE,description);
//in.putExtra(TAG_PHONE_HOME,price);
// in.putExtra(TAG_PHONE_OFFICE,amount);
//in.putExtra(TAG_ADDRESS,add);
//in.putExtra(TAG_PHONE_MOBILE, description);
// in.putExtra(TAG_PHONE_HOME,sweet);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SearchActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
// String add = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
// contact.put(TAG_ADDRESS,add);
contact.put(TAG_PHONE_MOBILE, mobile);
contact.put(TAG_PHONE_HOME,home);
contact.put(TAG_PHONE_OFFICE,office);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, contactList,
R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE ,TAG_PHONE_HOME,TAG_PHONE_OFFICE }, new int[] { R.id.name,
R.id.email, R.id.mobile,R.id.price ,R.id.mobile_labels});
setListAdapter(adapter);
}
}
}
Here is the list. In this I am getting name,email and mobile number. In the list when I select a name in the list open a new activity in that activity I need name, email, phone, address, gender, office. How should I pass this in next activity.
Here is the next activity code.
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String email = in.getStringExtra(TAG_EMAIL);
String mobile = in.getStringExtra(TAG_PHONE_MOBILE);
String home = in.getStringExtra(TAG_PHONE_HOME);
String office= in.getStringExtra(TAG_PHONE_OFFICE);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblEmail = (TextView) findViewById(R.id.email_label);
TextView lblMobile = (TextView) findViewById(R.id.mobile_label);
TextView lblMobiles = (TextView) findViewById(R.id.mobile_labels);
TextView lblOffice = (TextView) findViewById(R.id.mobile_office);
lblName.setText(name);
lblEmail.setText(email);
lblMobile.setText(mobile);
lblMobiles.setText(home);
lblOffice.setText(office);
}
}
It's better if I have only a name in the listview. On selecting the name in the list further details should be displayed, for instance: name, phone, email, address, gender.

Pass them through the intent you used to start the new activity. When you generate the intent you can bundle some information and pass it as an extra. Then all the information should be in the bundle when your new activity starts. That's probably the easiest way.
There's lots of information in those links, and google has tutorials on how to use intents as well.
Hope that helps!

Related

listview on select the item in list display a detail activity

private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_list);
contactList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
int itemPosition = position;
String name = ((TextView) view.findViewById(R.id.name))
.getText().toString();
String cost = ((TextView) view.findViewById(R.id.email))
.getText().toString();
// String add = ((TextView) view.findViewById(R.id.address))
// .getText().toString();
// String description = ((TextView) view.findViewById(R.id.mobile))
//.getText().toString();
//String sweet = ((TextView) view.findViewById(R.id.home))
// .getText().toString();
// Starting single contact activity
Intent in = new Intent(SearchActivity.this,
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
//in.putExtra(TAG_ADDRESS,add);
//in.putExtra(TAG_PHONE_MOBILE, description);
// in.putExtra(TAG_PHONE_HOME,sweet);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SearchActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
// String add = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
// contact.put(TAG_ADDRESS,add);
contact.put(TAG_PHONE_MOBILE, mobile);
contact.put(TAG_PHONE_HOME,home);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SearchActivity.this, contactList,
R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE ,TAG_PHONE_HOME }, new int[] { R.id.name,
R.id.email, R.id.mobile,R.id.home });
setListAdapter(adapter);
}
}
}
I have a list activity in the list it display the name,email,phone number on select an item in the list open a new activity and display the same name, email,number but i need others details in nextactivity including addrees,gender,home, office
In the nextactivity i displaying this items
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String email = in.getStringExtra(TAG_EMAIL);
String mobile = in.getStringExtra(TAG_PHONE_MOBILE);
String home = in.getStringExtra(TAG_PHONE_HOME);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblEmail = (TextView) findViewById(R.id.email_label);
TextView lblMobile = (TextView) findViewById(R.id.mobile_label);
TextView lblMobiles = (TextView) findViewById(R.id.mobile_labels);
lblName.setText(name);
lblEmail.setText(email);
lblMobile.setText(mobile);
lblMobiles.setText(home);
}
}
Inclusignthsi i want to display address,home,office .
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String email = in.getStringExtra(TAG_EMAIL);
String mobile = in.getStringExtra(TAG_PHONE_MOBILE);
String home = in.getStringExtra(TAG_PHONE_HOME);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblEmail = (TextView) findViewById(R.id.email_label);
TextView lblMobile = (TextView) findViewById(R.id.mobile_label);
TextView lblMobiles = (TextView) findViewById(R.id.mobile_labels);
lblName.setText(name);
lblEmail.setText(email);
lblMobile.setText(mobile);
lblMobiles.setText(home);
}
#subhas i have updated th other activity code

i would like to switch tab without open new activity

here my code i have create three tab and i want to i am click on list item in first tab and its result show in third tab but currently its open new activity window i want to click on list item its move to third tab and view result.
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos",
getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
// setting Title and Icon for the Tab
songspec.setIndicator("Songs",
getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos",
getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}
tab1**************************************
public class PhotosActivity extends ListActivity {
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos_layout);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), VideosActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
}
}
tab3*****************************************
public class VideosActivity extends Activity {
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String name = in.getStringExtra(TAG_NAME);
String cost = in.getStringExtra(TAG_EMAIL);
String description = in.getStringExtra(TAG_PHONE_MOBILE);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
lblName.setText(name);
lblCost.setText(cost);
lblDesc.setText(description);
}
}
your code needs too much work
you can try PagerSlidingTabStrip to add tabbs and dynamic pages its ready project you can download it and import it to Eclipse from this link
https://github.com/astuetz/PagerSlidingTabStrip

After selected listitem shows the details of them on another activity through json?

I have activity in which I show the list item and in list item I only want to show the name of places and I am able to show it but when I click on the item I want related to that place name it shows the detail on another activity i.e. location detail. so please help me to resolve my problem I am new in android.
here is my both activity code:
locationList=( ListView)findViewById(R.id.list_Surroundings);
// Hashmap for ListView
ArrayList<HashMap<String, String>> locationOfList = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, locationOfList,
R.layout.list_item1,
new String[] { TAG_NAME }, new int[] {
R.id.name});
locationList.setAdapter(adapter);
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Category
location=json.getJSONArray(TAG_LOCATION);
//looping through all categories
for(int i = 0; i < location.length(); i++){
JSONObject c = location.getJSONObject(i);
JSONObject c1=c.getJSONObject(TAG_LOCATION1);
// Storing each json item in variable
String LocationID = c1.getString(TAG_LOCATION_ID);
String Name = c1.getString(TAG_NAME);
String Phone = c1.getString(TAG_PHONE);
String FormattedPhone = c1.getString(TAG_FORMATTED_PHONE);
String Address = c1.getString(TAG_ADDRESS);
String CrossStreet = c1.getString(TAG_CROSS_STREET);
String Lat = c1.getString(TAG_LAT);
String Lng = c1.getString(TAG_LNG);
String Distance = c1.getString(TAG_DISTANCE);
String PostalCode = c1.getString(TAG_POSTAL_CODE);
String City = c1.getString(TAG_CITY);
String State = c1.getString(TAG_STATE);
String Country = c1.getString(TAG_COUNTRY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_LOCATION_ID, LocationID);
map.put(TAG_NAME, Name);
map.put(TAG_PHONE, Phone);
map.put(TAG_FORMATTED_PHONE, FormattedPhone);
map.put(TAG_ADDRESS, Address);
map.put(TAG_CROSS_STREET, CrossStreet);
map.put(TAG_LAT, Lat);
map.put(TAG_LNG, Lng);
map.put(TAG_DISTANCE, Distance);
map.put(TAG_POSTAL_CODE, PostalCode);
map.put(TAG_CITY, City);
map.put(TAG_STATE, State);
map.put(TAG_COUNTRY, Country);
// adding HashList to ArrayList
locationOfList.add(map);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
locationList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// getting values from selected ListItem
String Name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String Distance = ((TextView) view.findViewById(R.id.distance_list1)).getText().toString();
String Country = ((TextView) view.findViewById(R.id.country_list1)).getText().toString();
String City = ((TextView) view.findViewById(R.id.city_list1)).getText().toString();
String Phone = ((TextView) view.findViewById(R.id.phone_list1)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), LocationDetails.class);
in.putExtra(TAG_NAME, Name);
in.putExtra(TAG_DISTANCE, Distance);
in.putExtra(TAG_COUNTRY, Country);
in.putExtra(TAG_CITY, City);
in.putExtra(TAG_PHONE, Phone);
startActivity(in);
}
});
public class LocationDetails extends Activity{
// JSON node keys
private static final String TAG_NAME = "Name";
private static final String TAG_PHONE = "Phone";
private static final String TAG_ADDRESS = "Address";
private static final String TAG_DISTANCE= "Distance";
private static final String TAG_CITY = "City";
private static final String TAG_COUNTRY= "Country";
private TextView name,phone,address,distance,city,country;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_details);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String Name = in.getStringExtra(TAG_NAME);
String Phone = in.getStringExtra(TAG_PHONE);
String Address = in.getStringExtra(TAG_ADDRESS);
String Distance = in.getStringExtra(TAG_DISTANCE);
String City = in.getStringExtra(TAG_CITY);
String Country = in.getStringExtra(TAG_COUNTRY);
// Displaying all values on the screen
name= (TextView) findViewById(R.id.TxtView_name);
phone=(TextView)findViewById(R.id.TxtView_PhoneField);
address=(TextView)findViewById(R.id.TxtView_AddressField);
distance=(TextView)findViewById(R.id.TxtView_DistanceField);
city=(TextView)findViewById(R.id.TxtView_CityField);
country=(TextView)findViewById(R.id.TxtView_CountryField);
name.setText(Name);
phone.setText(Phone);
address.setText(Address);
distance.setText(Distance);
city.setText(City);
country.setText(Country);
}
}
In sending activity, try replacing
in.putExtra(TAG_NAME, Name);
in.putExtra(TAG_DISTANCE, Distance);
in.putExtra(TAG_COUNTRY, Country);
in.putExtra(TAG_CITY, City);
in.putExtra(TAG_PHONE, Phone);
with
Bundle extras = new Bundle();
extras.putString(TAG_NAME, Name);
extras.putString(TAG_DISTANCE, Distance);
extras.putString(TAG_COUNTRY, Country);
extras.putString(TAG_CITY, City);
extras.putString(TAG_PHONE, Phone);
extras.putExtras(extras);
In receiving activity, try replacing
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String Name = in.getStringExtra(TAG_NAME);
String Phone = in.getStringExtra(TAG_PHONE);
String Address = in.getStringExtra(TAG_ADDRESS);
String Distance = in.getStringExtra(TAG_DISTANCE);
String City = in.getStringExtra(TAG_CITY);
String Country = in.getStringExtra(TAG_COUNTRY);
with
Bundle extras = getIntent().getExtras();
if(extras != null) {
String Name = extras.getString(TAG_NAME);
String Phone = extras.getString(TAG_PHONE);
String Address = extras.getString(TAG_ADDRESS);
String Distance = extras.getString(TAG_DISTANCE);
String City = extras.getString(TAG_CITY);
String Country = extras.getString(TAG_COUNTRY);
}
You are trying to get the data from the TextViews, which is not at all right.
You need to fetch the values of the selected item from the ArrayList by using locationOfList(position) inside the onClick() of the listItem.
Something like this :
locationList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// getting values from selected ListItem
HashMap<String, String> selectedMap = locationOfList.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), LocationDetails.class);
in.putExtra(selectedMap);
startActivity(in);
}
});
And in your second Activity, use this example to retrieve the data from the HashMap.

Endless listview not loading items

I am using listview to display items obtained from json response. Currently 10 items are displaying, as i scroll down more items must load but it is not happening. Following is my code can anyone help me? Answers will be appreciated.
public class MainActivity extends ListActivity {
ListView list;
LazyAdapter adapter;
JSONArray posts;
// All static variables
static final String URL = "http://site.org/";
static final String KEY_POSTS = "posts";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
Runnable runable = new Runnable() {
#Override
public void run() {
//call the function
LoadData();
//also call the same runnable
handler.postDelayed(this, 40000);
}
};
handler.postDelayed(runable, 10);
}public void LoadData(){
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
JSONArray posts = json.getJSONArray(KEY_POSTS);
// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");
//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);
String url = null;
String slug = null;
try {
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);
slug = d.getString(KEY_SLUG);
JSONObject images = d.getJSONObject(KEY_IMAGES);
JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
url = thumbnail.getString(KEY_URL);
}
} catch (Exception e) {
e.printStackTrace();
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);
// adding HashList to ArrayList
songsList.add(map);
}
}catch (JSONException e) {
e.printStackTrace();
}
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
adapter.notifyDataSetChanged();
ListView list=(ListView)findViewById(android.R.id.list);
list.setAdapter(adapter);
// Launching new screen on Selecting Single ListItem
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String date = ((TextView) view.findViewById(R.id.date)).getText().toString();
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String content = ((TextView) view.findViewById(R.id.content)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),SampleDesp.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_DATE, date);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_CONTENT, content);
startActivity(in);
}
});
list.setOnScrollListener(new OnScrollListener(){
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// TODO Auto-generated method stub
}
});
}
}

Android different rows in listview

i have news application that download data from web and displays it in listview ,but how can i make that first row is different ,bigger height ,different layout and stuff ?
this is main activity source
public class Home extends Activity {
// All static variables
static final String URL = "http://dmb.site50.net/application.php?app_access_key=c4ca4238a0b923820dcc509a6f75849b";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
public static final String TAG_NEWS = "news";
public static final String TAG_ID = "id";
public static final String TAG_TITLE = "title";
public static final String TAG_STORY = "story";
public static final String TAG_SH_STORY = "shorten";
public static final String TAG_DATETIME = "datetime";
public static final String TAG_AUTHOR = "author";
public static final String TAG_IMG = "img";
ListView list;
LazyAdapter adapter;
Button mBtnNaslovnica;
private ViewSwitcher viewSwitcher;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mBtnNaslovnica = (Button) findViewById(R.id.mBtnNaslovnica);
mBtnNaslovnica.setSelected(true);
TextView txtView=(TextView) findViewById(R.id.scroller);
txtView.setSelected(true);
ImageButton mBtnRefresh = (ImageButton) findViewById(R.id.btnRefresh);
mBtnRefresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new LoadingTask().execute(URL);
}
});
ArrayList<HashMap<String, String>> homeList = new ArrayList<HashMap<String, String>>();
JSONObject jsonobj;
try {
jsonobj = new JSONObject(getIntent().getStringExtra("json"));
JSONArray news = jsonobj.getJSONArray(TAG_NEWS);
for(int i = 0; i < news.length(); i++){
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String story = c.getString(TAG_STORY);
String shorten = c.getString(TAG_SH_STORY);
String author = c.getString(TAG_AUTHOR);
String datetime = c.getString(TAG_DATETIME);
String img = c.getString(TAG_IMG);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_TITLE, title);
map.put(TAG_STORY, story);
map.put(TAG_IMG, img);
map.put(TAG_DATETIME, datetime);
map.put(TAG_AUTHOR, author);
// adding HashList to ArrayList
homeList.add(map);}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, homeList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String cur_title = ((TextView) view.findViewById(R.id.title)).getText().toString();
String cur_story = ((TextView) view.findViewById(R.id.einfo2)).getText().toString();
String cur_author = ((TextView) view.findViewById(R.id.einfo1)).getText().toString();
String cur_datetime = ((TextView) view.findViewById(R.id.einfo)).getText().toString();
ImageView cur_img = (ImageView) view.findViewById(R.id.list_image);
String cur_img_url = (String) cur_img.getTag();
Intent i = new Intent("com.example.androidhive.CURENTNEWS");
i.putExtra("CUR_TITLE", cur_title);
i.putExtra("CUR_STORY", cur_story);
i.putExtra("CUR_AUTHOR", cur_author);
i.putExtra("CUR_DATETIME", cur_datetime);
i.putExtra("CUR_IMG_URL", cur_img_url);
startActivity(i);
}
});
}
public void loadPage(){
}
public void startNewActivity(){
}
public class LoadingTask extends AsyncTask<String, Object, Object>{
XMLParser parser = new XMLParser();
JSONParser jParser = new JSONParser();
#Override
protected Object doInBackground(String... params) {
// TODO Auto-generated method stub
String URL = params[0];
JSONObject json = jParser.getJSONFromUrl(URL);
//String xml = parser.getXmlFromUrl(URL); // getting XML from URL
// getting DOM element
return json;
}
protected void onPostExecute(Object result){
Intent startApp = new Intent("com.example.androidhive.HOME");
startApp.putExtra("json", result.toString());
startActivity(startApp);
}
}
}
You need to modify your LazyAdapter code to return right layout for each row, depending on any criteria you want. ListView simply uses view given by adapter, so it is nothing special to have each row layouted differently. Please note that to make it work correctly you need to implement getItemViewType() and getViewTypeCount() as well.

Categories

Resources