On scroll add more json data in listView - android

I made one ListView with image and text. I am able to parse json data in the ListView. But my requirenment is to load 10 rows of Json data, then at the bottom have a load more. to get the next 10, etc etc.
Suggest me if anyone knows the solution using json please...
Here is my code for Adapter.java
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
ImageView thumb_image1=(ImageView)vi.findViewById(R.id.imageView1);// thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.USER_NAME));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL1), thumb_image1);
return vi;
}
}
Here is the code of MainActivity.java
public class CustomizedListView extends Activity {
// All static variables
static final String URL = XXXX.json;
//private static String URL;
// XML node keys
static final String KEY_SONG = "review_full"; // parent node
static final String KEY_ID = "activity_id";
static final String USER_NAME = "user_name";
static final String KEY_ARTIST = "review_small";
static final String KEY_DURATION = "time";
static final String KEY_THUMB_URL = "thumb";
static final String KEY_THUMB_URL1 = "photo";
String type;
String user_name1;
String thumbnail;
String subscriber_name;
String review_full;
String status;
String photo;
ListView list;
String review_small;
LazyAdapter adapter;
String[] image;
ArrayList<HashMap<String, String>> songsList;
public String image1=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Intent intent1 = getIntent();
//String easyPuzzle = intent1.getExtras().getString("Id");
//URL=URL0+easyPuzzle;
songsList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URL);
try{
Log.d("Parsing JSON Data", "Before json1 try2");
JSONArray activities = json.getJSONArray("activities");
Log.d("Parsing JSON Data", "after activiti");
for (int i = 0; i < activities.length(); i++) {
JSONObject c = activities.getJSONObject(i);
image=new String[2];
Log.d("Parsing JSON Data", "inside for loop");
HashMap<String, String> map = new HashMap<String, String>();
//checking type content
type=c.getString("type");
if(type.contains("user")){
user_name1=c.getString( USER_NAME);
thumbnail=c.getString(KEY_THUMB_URL);
map.put(KEY_THUMB_URL, thumbnail);
map.put(USER_NAME, user_name1+" "+"joined etable community");
map.put(KEY_DURATION, c.getString( KEY_DURATION));
// map.put(KEY_ARTIST,null);
}else if(type.contains("checkin")){
user_name1=c.getString( USER_NAME);
subscriber_name=c.getString("subscriber_name");
thumbnail=c.getString(KEY_THUMB_URL);
photo=c.getString("photo");
if(photo.contains("False")){
image1="False";
map.put(KEY_THUMB_URL1, image1);
}else{
image = photo.split("image=");
image1=image[1];
map.put(KEY_THUMB_URL1, image1);
}
map.put(KEY_THUMB_URL, thumbnail);
map.put(USER_NAME, user_name1+" "+"was spotted at"+" "+subscriber_name);
map.put(KEY_DURATION, c.getString( KEY_DURATION));
// map.put(KEY_ARTIST,null);
}else if(type.contains("favorite")){
subscriber_name=c.getString("subscriber_name");
user_name1=c.getString( USER_NAME);
thumbnail=c.getString(KEY_THUMB_URL);
map.put(KEY_THUMB_URL, thumbnail);
map.put(USER_NAME, user_name1+" "+"favorited"+" "+subscriber_name);
map.put(KEY_DURATION, c.getString( KEY_DURATION));
//map.put(KEY_ARTIST,null);
}else if(type.contains("review")){
subscriber_name=c.getString("subscriber_name");
user_name1=c.getString( USER_NAME);
review_small=c.getString("review_small");
review_full=c.getString("review_full");
thumbnail=c.getString(KEY_THUMB_URL);
map.put(KEY_THUMB_URL, thumbnail);
map.put(USER_NAME, user_name1+" "+"wrote a review for"+" "+subscriber_name);
map.put(KEY_DURATION, c.getString( KEY_DURATION));
map.put(KEY_ARTIST,review_small);
map.put(KEY_SONG,review_full);
}else if(type.contains("status")){
user_name1=c.getString( USER_NAME);
status=c.getString("full_status");
thumbnail=c.getString(KEY_THUMB_URL);
map.put(KEY_THUMB_URL, thumbnail);
map.put(USER_NAME, user_name1+" "+"says\n"+" "+status);
map.put(KEY_DURATION, c.getString( KEY_DURATION));
//map.put(KEY_ARTIST,null);
}else{
user_name1=c.getString( USER_NAME);
map.put(USER_NAME, user_name1+" "+"some problem");
//map.put(KEY_ARTIST,null);
}
// adding HashList to ArrayList
songsList.add(map);
}}catch (JSONException e) {
e.printStackTrace();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.invalidateViews();
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
list.invalidateViews();
Log.d("Parsing JSON Data", "start click");
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent(CustomizedListView.this, LargeView.class);
in.putExtra("text", map.get(USER_NAME));
in.putExtra("image", map.get(KEY_THUMB_URL));
in.putExtra("review", map.get(KEY_SONG));
Log.d("Parsing JSON Data", "inside profile33");
startActivity(in);
Log.d("Parsing JSON Data", USER_NAME);
}
});
}
}

Setting your adapter.setNotifysetchanged() will display the added items in your ListView Here i give you some example that how to integrate the load more with your ListView
1.example
2.Costum Load more listview

Related

hashmap working in simpleadapter but not working in custom array adapter

Hashmap for the code below is working for simpleadapter array but not for custom array adapter .Why ? and how do I fix it ? Please Help.Unable to resolve .Tried everything.
I am using lazyLoading class to load images.
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position);
Toast.makeText(CustomizedListView.this, "ID '" + o.get("KEY_TITLE") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
The entire code which is crashing at onclick shown above :
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "https://itunes.apple.com/us/rss/topalbums/limit=20/json";
// 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";
ListView list;
LazyAdapter adapter;
HashMap<String, String> map;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL(URL);
try {
JSONObject arr2 = json.getJSONObject("feed");
JSONArray arr = arr2.getJSONArray("entry");
for (int i = 0; i < arr.length(); i++) {
JSONObject e1 = arr.getJSONObject(i);
JSONArray arr3 = e1.getJSONArray("im:image");
JSONObject arr8 = e1.getJSONObject("im:name");
JSONObject arr10 = e1.getJSONObject("im:artist");
JSONObject e12 = arr3.getJSONObject(0);
// creating new HashMap
map = new HashMap<String, String>();
map.put(KEY_THUMB_URL, e12.getString("label"));
map.put(KEY_ARTIST, arr8.getString("label"));
map.put(KEY_TITLE, arr10.getString("label"));
// adding HashList to ArrayList
songsList.add(map);
}
} catch (JSONException e) {
// Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getBaseContext(),
"Network communication error!", 5).show();
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
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) {
HashMap<String, String> o= (HashMap<String, String>) list.getItemAtPosition(position);
Toast.makeText(CustomizedListView.this, "ID '" + o.get("title") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
This Code below works perfectly:
public class Main extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo");
try{
JSONArray earthquakes = json.getJSONArray("earthquakes");
for(int i=0;i<earthquakes.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "magnitude" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
LogCAT:
threadid=1: thread exiting with uncaught exception (group=0x40ab0228)
FATAL EXCEPTION: main
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.HashMap
at com.example.androidhive.CustomizedListView$1.onItemClick(CustomizedListView.java:94)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1077)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2533)
at android.widget.AbsListView$1.run(AbsListView.java:3198)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
LazyAdapter.java
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String, String>>();
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public static HashMap<String, String> song;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
In you custom Adapter you are returning position instead of Item
public Object getItem(int position) {
return position;
}
Try use this
public Object getItem(int position) {
return data.get(position);
}
This will fix your problem

ListFragment in onItemClickListener() startActivity crashes app

I have problem with calling startActivity(intent) from onItemClickListener() on ListFragment. When I click on an item from listview the application crashes.
Code is here:
public class AndroidFragment extends SherlockListFragment{
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
static final String URL = "***";
// XML node keys
static final String KEY_ITEM = "novost"; // parent node
//static final String KEY_ID = "id";
static final String KEY_NAME = "naslov";
static final String KEY_COST = "datum";
static final String KEY_DESC = "text";
static final String KEY_LINK = "link";
static final String KEY_LINK1 = "doc";
ArrayList<HashMap<String, String>> menuItems;
String[] from = { KEY_NAME, KEY_DESC, KEY_COST,KEY_LINK,KEY_LINK1};
/** Ids of views in listview_layout */
int[] to = { R.id.naslov, R.id.novost, R.id.datum,R.id.link,R.id.link1};
ListView list;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new loadListView().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.naslov)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) view.findViewById(R.id.novost)).getText().toString();
String link = ((TextView) view.findViewById(R.id.link)).getText().toString();
String link1 = ((TextView) view.findViewById(R.id.link1)).getText().toString();
//String link_asd=link;
// Starting new intent
Intent in = new Intent(getActivity().getBaseContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
if(link==null)
{
link=null;
}else{
in.putExtra(KEY_LINK, link);
in.putExtra(KEY_LINK1, link1);
}
//in.putExtra("link1", link_asd);
startActivity(in);
}
});
}
/*#Override
public void onListItemClick(ListView list, View v, int position, long id) {
/**
* Toast message will be shown when you click any list element
//Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
String name = ((TextView) v.findViewById(R.id.naslov)).getText().toString();
String cost = ((TextView) v.findViewById(R.id.datum)).getText().toString();
String description = ((TextView) v.findViewById(R.id.novost)).getText().toString();
String link = ((TextView) v.findViewById(R.id.link)).getText().toString();
String link1 = ((TextView) v.findViewById(R.id.link1)).getText().toString();
//String link_asd=link;
// Starting new intent
Intent in = new Intent(getActivity(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
if(link==null)
{
}else{
in.putExtra(KEY_LINK, link);
in.putExtra(KEY_LINK1, link1);
}
//in.putExtra("link1", link_asd);
startActivity(in);
Log.e("error",name);
super.onListItemClick(list, v, position, id);
}*/
#Override
public void onResume() {
super.onResume();
//Log.w("Aplikacija_resume","Startovana" );
//new loadListView().execute();
}
public class loadListView extends AsyncTask<Integer, String, String>
{
private final ProgressDialog dialog = new ProgressDialog(getActivity());
#Override protected void onPreExecute()
{
//Toast.makeText(getActivity(), "Ucitavanje...", Toast.LENGTH_LONG).show();
//
this.dialog.setMessage("Molimo da sačekate ...");
super.onPreExecute();
}
#Override protected String doInBackground(Integer... args)
{ // updating UI from Background Thread
menuItems = new ArrayList<HashMap<String, String>>();
final XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
#Override
protected void onPostExecute(String args)
{
//Toast.makeText(getActivity(), "Ucitano", Toast.LENGTH_LONG).show();
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), menuItems, R.layout.list_row, from, to);
setListAdapter(adapter);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
}
}
}
Add SingleMenuItemActivity to your manifest :)

The method get(String) is undefined for the type String error in adding the codes to the filter class of adapter in android

For Finding the key items fromt he listview i had not used filter in the adapter, but now by following this link aded the filter but in this line String playerName=songsList.get(i).get("title").toString(); I am getting the error
The method get(String) is undefined for the type String
and also in searchResults.add(songsList.get(i)); as
The method add(HashMap<String,String>) in the type ArrayList<HashMap<String,String>> is not applicable for the arguments (String)
Here's is my entire code
public class Home extends ListActivity {
//how many to load on reaching the bottom
int itemsPerPage = 15;
boolean loadingMore = false;
//For test data :-)
Calendar d = Calendar.getInstance();
ArrayList<String> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;
//ArrayList thats going to hold the search results
ArrayList<HashMap<String, String>> searchResults;
LayoutInflater inflater;
// All static variables
static final String URL = "http://india.abc.net/ads/?json=get_recent_posts";
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.main);
final EditText searchBox=(EditText) findViewById(R.id.search);
final ListView list=(ListView)findViewById(android.R.id.list);
//get the LayoutInflater for inflating the customomView
//this will be used in the custom adapter
inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
final JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
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();
}
//searchResults=OriginalValues initially
searchResults=new ArrayList<HashMap<String, String>>(songsList);
// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
// Launching new screen on Selecting Single ListItem
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsList.get(position);
Intent in = new Intent(Home.this, Singlemenuitem.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_DATE, map.get(KEY_DATE));
in.putExtra(KEY_NAME, map.get(KEY_NAME));
in.putExtra(KEY_CONTENT, map.get(KEY_CONTENT));
in.putExtra(KEY_URL, map.get(KEY_URL));
startActivity(in);
}
});
and the adapter class is
public class LazyAdapter extends BaseAdapter implements Filterable{
TextView title;
private Activity activity;
// private TextWatcher textWatcher;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
final EditText searchBox=(EditText) findViewById(R.id.search);
ArrayList<HashMap<String, String>> searchResults;
ArrayList<String> songsList;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
private EditText findViewById(int search) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_home, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date = (TextView)vi.findViewById(R.id.date); // artist name
TextView content = (TextView)vi.findViewById(R.id.content); // duration
TextView name = (TextView)vi.findViewById(R.id.name);
// duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(Home.KEY_TITLE));
date.setText(song.get(Home.KEY_DATE));
content.setText(song.get(Home.KEY_CONTENT));
name.setText(song.get(Home.KEY_NAME));
imageLoader.DisplayImage(song.get(Home.KEY_URL), thumb_image);
return vi;
}
public void add(String string) {
// TODO Auto-generated method stub
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#SuppressWarnings("unchecked")
#Override
protected void publishResults(CharSequence data,FilterResults searchResults) {
songsList = (ArrayList<String>) searchResults.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
#Override
protected FilterResults performFiltering(CharSequence playerName) {
// TODO Auto-generated method stub
return null;
}
};
String searchString=searchBox.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<songsList.size();i++)
{
String playerName=songsList.get(i).get("title").toString();
if(textLength<=playerName.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
searchResults.add(songsList.get(i));
}
}
return filter;
}
}
ArrayList<String> songsList;
so songsList is an ArrayList of Strings... if you do songsList.get(i) you'll have a String. The String object has not a "get" method, sou you cannot go further with
songsList.get(i).get("title").toString().
The same thing happens here:
searchResults=new ArrayList<HashMap<String, String>>
so if you call searchResults.add you have to pass a hashmap, not a String.
Please check your type definitions again and it will work.
songsList is a list of strings so you cannot do a get() on its item

Xml parsing in android with singlemenuitem

Hi i wrote one xml parsing example.here i have to get the data information from mysql database and display it in android emulator successfully.
this is my code:
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://192.168.1.168/xcart432pro/orderdetails.xml";
// XML node keys
static final String KEY_SONG = "Order"; // parent node
static final String KEY_ID = "orderid";
static final String KEY_TITLE = "orderid";
static final String KEY_ARTIST = "payment_method";
static final String KEY_DURATION = "total";
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
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 Orderid= ((TextView) view.findViewById(R.id.title)).getText().toString();
String Price = ((TextView) view.findViewById(R.id.duration)).getText().toString();
String Description = ((TextView) view.findViewById(R.id.artist)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, Orderid);
in.putExtra(KEY_DURATION, Price);
in.putExtra(KEY_ARTIST, Description);
startActivity(in);
}
});
}
}
dis is my singlemenuitem.java class is:
public class SingleMenuItemActivity extends Activity {
// XML node keys
static final String KEY_TITLE = "orderid";
static final String KEY_ARTIST = "payment_method";
static final String KEY_DURATION = "total";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String product = in.getStringExtra(KEY_DURATION);
String login = in.getStringExtra(KEY_TITLE);
String description = in.getStringExtra(KEY_ARTIST);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblPName = (TextView) findViewById(R.id.cost_label);
TextView lblDesc = (TextView) findViewById(R.id.description_label);
lblName.setText(login);
lblPName.setText(product);
lblDesc.setText(description);
}
}
Here i have to successfully displayed on android emulator.
but i wish to display on first page orderid and payment_method only.then it is move to next page means have to display total for that particular id.please give me solutions.how can i to do.i wish to my output is :
if i clicked 13 means that particular order total only displayed on next activity.
How is to do.please help me.
i got the answer.if u need hide the "description" field by adding andorid:visibility="gone" to description label in your xml file. So that the description filed will be present in listview but it won't be visible.
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra("KEY_TITLE", Orderid);
in.putExtra("KEY_DURATION", Price);
in.putExtra("KEY_ARTIST", Description);
startActivity(in);
singlemenuitem.java
product = getIntent().getExtras().getString("KEY_TITLE");
login = getIntent().getExtras().getString("KEY_DURATION");
description = getIntent().getExtras().getInt("KEY_ARTIST");

Android ListView image onclick parsing

My list view is parsed from a online resource, but i cant get the listview to pass the image src to the single list item
SingleMenuItemActivity
static final String KEY_SONG = "song";
static final String KEY_ARTIST = "artist";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_DURATION = "duration";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.libraryonclick);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String song = in.getStringExtra(KEY_SONG);
String artist = in.getStringExtra(KEY_ARTIST);
String thumb_url = in.getStringExtra(KEY_THUMB_URL);
String duration = in.getStringExtra(KEY_DURATION);
// Displaying all values on the screen
TextView lblSong = (TextView) findViewById(R.id.textView1);
TextView lblArtist = (TextView) findViewById(R.id.textView2);
Drawable imgThumb = ((ImageView)findViewById(R.id.onclickthumb)).getDrawable(); // thumb image
TextView lblDuration = (TextView) findViewById(R.id.textView3);
Button link3Btn = (Button)findViewById( R.id.button1 );
link3Btn.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
Uri uri = Uri.parse("http://google.com");
startActivity( new Intent( Intent.ACTION_VIEW, uri ) );
}
});
lblSong.setText(song);
lblArtist.setText(artist);
lblDuration.setText(duration);
imgThumb.setImageResource(thumb_url);
//Maybe more code here?
}
ListViewActivity
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://dl.dropbox.com/u/48258247/music.xml";
// 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";
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.library);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String song = ((TextView) view.findViewById(R.id.title)).getText().toString();
String artist = ((TextView) view.findViewById(R.id.artist)).getText().toString();
String thumb_url = ((TextView) view.findViewById(R.id.list_image)).getText().toString();
String duration = ((TextView) view.findViewById(R.id.duration)).getText().toString();
// Starting new intent
Intent in = new Intent(CustomizedListView.this, org.scouts.library.SingleMenuItem.class);
in.putExtra(KEY_SONG, song);
in.putExtra(KEY_ARTIST, artist);
in.putExtra(KEY_THUMB_URL, thumb_url);
in.putExtra(KEY_DURATION, duration);
startActivity(in);
}
});
}
i think i need to parse through the imageloader class maybe
You can simply try this one -
URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
Create one ImageView in your CustomizedListView class and oncreate() method receive url path from the calling file through intent. And paste it in below code and try it.
And, have a look this example

Categories

Resources