Trouble using LazyAdapter and getAdapter() - android

i cannot send data to second activity.
mainactivity must send img url and img title to second activity but doesnt.
in gridview i see both of them.
but gives me null data, i dont know why
main activity: (clicklistener)
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
startActivity(in);
}
});
LazyAdapter :
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 data.get(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.grid_row, null);
TextView title = (TextView)vi.findViewById(R.id.title2); // title
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image2); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
full main activity :
public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://mysite .com/images/rss.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "item";
static final String KEY_THUMB_URL = "thumb_url";
GridView gridView;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
gridView= (GridView) this.findViewById(R.id.list2);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
gridView.setAdapter(adapter);
// Click event for single list row
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map2 = (HashMap<String, String>) gridView.getAdapter().getItem(position);
Intent in = new Intent(getApplicationContext(), FullSize.class);
in.putExtra(KEY_THUMB_URL, map2.get(KEY_THUMB_URL));
in.putExtra(KEY_TITLE, map2.get(KEY_TITLE));
startActivity(in);
}
});
}
}

You are accessing your extra wrong in your 2nd Activity. The key is not "KEY_TITLE", it is "item". Remove the quotes and access the variable KEY_TITLE.

Related

How To Fix This ?? >> incompatible with FragmentStatePagerAdapter

I have a program using fragment, but i have a problem, when i including list view in my pager when one of item on sliding menu selected.
these the error :
on tabs adapter
public class TabsPagerAdapter extends FragmentStatePagerAdapter {
public TabsPagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public FragmentActivity getItem(int index) {
switch (index) {
case 0:
return new SoloAttractionFragment();
case 1:
return new KaranganyarAttractionFragment();
case 2:
return new SukoharjoAttractionFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
the error in line :
public FragmentActivity getItem(int index) {
Multiple markers at this line
- implements
android.support.v4.app.FragmentStatePagerAdapter.getItem
- The return type is incompatible with
FragmentStatePagerAdapter.getItem(int)
- The return type is incompatible with
FragmentStatePagerAdapter.getItem(int)
please help me, thanks
the other class related :
public class SoloAttractionFragment extends FragmentActivity {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
public static final String KEY_SONG = "song"; // parent node
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_ARTIST = "artist";
public static final String KEY_DURATION = "duration";
public static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_attraction_solo);
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() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
});
}
This is adapter for list view :
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); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(SoloAttractionFragment.KEY_TITLE));
artist.setText(song.get(SoloAttractionFragment.KEY_ARTIST));
duration.setText(song.get(SoloAttractionFragment.KEY_DURATION));
imageLoader.DisplayImage(song.get(SoloAttractionFragment.KEY_THUMB_URL), position, thumb_image);
return vi;
}
}
public class SoloAttractionFragment extends android.support.v4.app.Fragment
You cannot return FragmentActivity from getItem(), because FragmentActivity is not a Fragment, but an Activity that can contain Fragments. getItem() must return a Fragment or ListFragment.

ArrayList in Fragment

I am novice with fragments and Im trying to do a custom list from xml file in a fragment and I have an issue with
final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
my code is below, somebody can help me? Thank you
public class InformesFragment extends Fragment {
static final String URL = "http://www.myurl.com";
// XML node keys
static final String KEY_SONG = "songs"; // parent node
static final String KEY_ID = "id_song";
static final String KEY_TITLE = "name";
static final String KEY_THUMB_URL = "picture";
static final String KEY_ARTIST = "duration";
ListView list;
LazyAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.songs, container, false);
final 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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView) getView().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 id =songsList.get(+position).get("id_song");
String nombre =songsList.get(+position).get("name");
String caratula =songsList.get(+position).get("picture");
String duracion =songsList.get(+position).get("duration");
Intent intent = new Intent(getActivity(), Myclass.class);
intent.putExtra("id", id);
intent.putExtra("izena", nombre);
intent.putExtra("karatula", caratula);
intent.putExtra("gureop", duración);
startActivity(intent);
}
});
}
}
My LazyAdapter is:
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 LazyAdapter(InformesFragment informesFragment,
ArrayList<HashMap<String, String>> songsList) {
// TODO Auto-generated constructor stub
}
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
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(InformesFragment.KEY_TITLE));
imageLoader.DisplayImage(song.get(InformesFragment.KEY_THUMB_URL), thumb_image);
return vi;
}
You have
return inflater.inflate(R.layout.songs, container, false);
that is the problem
Should be
View view = inflater.inflate(R.layout.songs, container, false);
final 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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView) view.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 id =songsList.get(+position).get("id_song");
String nombre =songsList.get(+position).get("name");
String caratula =songsList.get(+position).get("picture");
String duracion =songsList.get(+position).get("duration");
Intent intent = new Intent(getActivity(), Myclass.class);
intent.putExtra("id", id);
intent.putExtra("izena", nombre);
intent.putExtra("karatula", caratula);
intent.putExtra("gureop", duración);
startActivity(intent);
}
});
return view;
Also you have
String xml = parser.getXmlFromUrl(URL); // geting xml from url should be from a thread.
Also need to change
list=(ListView) getView().findViewById(R.id.list);
to
list=(ListView) view.findViewById(R.id.list);
I also suggest parsing the data in onCreate() or onResume() rather than onCreateView().
Ex.
Parse in onCreate() attach/setAdapter in onCreateView()
Parse in onResume() attach/setAdapter in onResume()
Even better use an AsyncTask and parse the data in the background then update your list.
Please write your code as :
public class InformesFragment extends Fragment {
static final String URL = "http://www.myurl.com";
// XML node keys
static final String KEY_SONG = "songs"; // parent node
static final String KEY_ID = "id_song";
static final String KEY_TITLE = "name";
static final String KEY_THUMB_URL = "picture";
static final String KEY_ARTIST = "duration";
ListView list;
LazyAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.songs, container, false);
final 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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView) getView().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 id =songsList.get(+position).get("id_song");
String nombre =songsList.get(+position).get("name");
String caratula =songsList.get(+position).get("picture");
String duracion =songsList.get(+position).get("duration");
Intent intent = new Intent(getActivity(), Myclass.class);
intent.putExtra("id", id);
intent.putExtra("izena", nombre);
intent.putExtra("karatula", caratula);
intent.putExtra("gureop", duración);
startActivity(intent);
}
});
}
return v;
}

The constructor Adapter(HomeFragment, ArrayList<HashMap<String,String>>) is undefined

I'm trying to populate a listview with remote data, I'm basing this part of my application off a tutorials code. In the tutorial they simply use classes, however in mine I'd like to use fragments as I'm also integrating a sliding navigation bar to the app. This is more than likely a stupid error on my part but nonetheless I could use some direction, thanks.
This is the error I'm getting:
The constructor LazyAdapter(HomeFragment, ArrayList<HashMap<String,String>>) is undefined
This happens at the following line in HomeFragment:
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(HomeFragment.this, songsList);
list.setAdapter(adapter);
HomeFragment code:
public class HomeFragment extends Fragment {
// All static variables
static final String URL = "http://api.androidhive.info/music/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;
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.blog, container, false);
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)getView().findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(HomeFragment.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) {
}
});
}
}
And the ListAdapters code:
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); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(HomeFragment.KEY_TITLE));
artist.setText(song.get(HomeFragment.KEY_ARTIST));
duration.setText(song.get(HomeFragment.KEY_DURATION));
imageLoader.DisplayImage(song.get(HomeFragment.KEY_THUMB_URL), thumb_image);
return vi;
}
}
Change
adapter=new LazyAdapter(HomeFragment.this, songsList);
to
adapter=new LazyAdapter(getActivity(), songsList);
public final Activity getActivity ()
Return the Activity this fragment is currently associated with.
Also change
list=(ListView)getView().findViewById(R.id.list);
to
list=(ListView)rootView.findViewById(R.id.list);
Also make sure you are doing network related operation on a bakgroud thread.
String xml = parser.getXmlFromUrl(URL);
Edit : Missing return statement in onCreateView
});
return rootView;
}

How to sort in a listView with a Custom Adapter

I am making an app in which I am showing no.of people with their Status as ONLINE and OFFLINE.
In present the ListView is set According to the api. I want to sort the listView according to the Online Status of the person.
for ex. if the no. of persons are online then the Listview Shows them first.
I have implemented lazy load images in my project.
this is my MainActivity.class
public class MainActivity extends Activity {
ProgressDialog dialog;
static final String URL = "Some_URL";
// XML node keys
static final String KEY_RESPONSE = "Response"; // parent node
static final String KEY_NAME = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_ID = "ConsultantID";
static final String KEY_OFFLINE = "OnlineStatus";
static final String KEY_THUMB_URL = "ProfilePicture";
ListView lv;
LazyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView)findViewById(R.id.list);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLfunctions parser = new XMLfunctions();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_RESPONSE);
// 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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_OFFLINE, parser.getValue(e, KEY_OFFLINE));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
adapter=new LazyAdapter(this, songsList);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> Parent, View view, int Position,
long Id) {
// TODO Auto-generated method stub
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(Position);
Intent i = new Intent ("com.fedorvlasov.lazylist.SHOWLARGE");
i.putExtra("ID",o.get(KEY_ID));
i.putExtra("NAME", o.get(KEY_NAME));
i.putExtra("DESCRIPTION",o.get(KEY_DESCRIPTION));
i.putExtra("STATUS", o.get(KEY_OFFLINE));
i.putExtra("IMAGE",o.get(KEY_THUMB_URL));
startActivity(i);
//Toast.makeText(LazyAdapter2.this, "position '" + adapter.getItem(Position) + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
}
}
And this is my Adapter Class called LazyAdater
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 data.get(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 name
TextView description = (TextView)vi.findViewById(R.id.artist); // descriptiom
TextView duration = (TextView)vi.findViewById(R.id.duration); // id
TextView Status = (TextView)vi.findViewById(R.id.status); //status
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(MainActivity.KEY_NAME));
description.setText(song.get(MainActivity.KEY_DESCRIPTION));
Status.setText(song.get(MainActivity.KEY_OFFLINE));
duration.setText(song.get(MainActivity.KEY_ID));
imageLoader.DisplayImage(song.get(MainActivity.KEY_THUMB_URL), thumb_image);
return vi;
}}
I have reviewed by some COMPARETOR.. but i m not understading how to achieve it..
thanks in advance..
try this
Collections.sort(songsList, new MyCustomComparator());
and
public static class MyCustomComparator implements Comparator<HashMap<String, String>> {
#Override
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
if(lhs.get("onLineCheck") is true)
return 1;
else
return -1;
return 0;
}
}
before this line
adapter=new LazyAdapter(this, songsList);

How to send the Values associated with the position in ListView to the another Activiy in android

I am parsing the xml to the list view in my proect. I want that when somebody clicks on any position in the listView i want to send the all information which i have in the current position to the another activity. I am new to android,please help me out of this.I am providing the code below.
I am displaying the KEY_NAME , KEY_DESCRIPTION ,KEY_ID , KEY_OFFLINE and KRY_THUMB_URL in the ListView. I want to send all these information for the current position to the another activity where i want to show the full detail about the current seleced item from the list.
This is my new Modified code.. but I am getting the error in this code.. application force close.
public class MainActivity extends Activity {
// All static variables
static final String URL = "Some____ URL";
// XML node keys
static final String KEY_RESPONSE = "Response"; // parent node
static final String KEY_NAME = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_ID = "ConsultantID";
static final String KEY_OFFLINE = "OnlineStatus";
static final String KEY_THUMB_URL = "ProfilePicture";
ListView list;
LazyAdapter adapter;
//String name;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLfunctions parser = new XMLfunctions();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_RESPONSE);
// 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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_OFFLINE, parser.getValue(e, KEY_OFFLINE));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position);
Intent i = new Intent(MainActivity.this,ShowLarge.class);
i.putExtra("ID",o.get("KEY_ID"));
i.putExtra("Name", o.get("KEY_NAME"));
startActivity(i);
Toast.makeText(MainActivity.this, "ID '" + o.get("KEY_ID") + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
}}
but when i Run the application it gives the following error..
07-09 16:35:42.024: E/AndroidRuntime(618): java.lang.ClassCastException: java.lang.Integer
I donot understand this all why this is happning.. plz help thankx in advance
My second Activity code is.
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 name
TextView description = (TextView)vi.findViewById(R.id.artist); // descriptiom
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
TextView Status = (TextView)vi.findViewById(R.id.status); //status
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(MainActivity.KEY_NAME));
description.setText(song.get(MainActivity.KEY_DESCRIPTION));
Status.setText(song.get(MainActivity.KEY_OFFLINE));
duration.setText(song.get(MainActivity.KEY_ID));
imageLoader.DisplayImage(song.get(MainActivity.KEY_THUMB_URL), thumb_image);
return vi;
}
}
Change The method in Lazy Adapter class like this.. will get the desired result
public Object getItem(int position) {
return data.get(position);
}
And Change the ONItemClickListner like this...
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> Parent, View view, int Position,
long Id) {
// TODO Auto-generated method stub
#SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(Position);
Intent i = new Intent ("com.fedorvlasov.lazylist.SHOWLARGE");
i.putExtra("ID",o.get(KEY_ID));
i.putExtra("NAME", o.get(KEY_NAME));
startActivity(i);
This will pass your values associated with the Position in the listview to the another activity... :)
normally you can use view tag for this purpose.
view.setTag('KEY_NAME', key_name_obj)
view.getTag('KEY_NAME')

Categories

Resources