How To Fix This ?? >> incompatible with FragmentStatePagerAdapter - android

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.

Related

Trouble using LazyAdapter and getAdapter()

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.

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;
}

Custom Listview + Xmlparser + Actionbar 4.2 Sherlock Searchview

I have a custom ListView that Get baseadapter by passing xml data ArrayList.
I want to perform a search using ActionbarSherlock 4.2 Searchview, so after user puts an text and press the button it will inflate a new layout with the results.
What is the best guideline?
Should I implement Filterable on BaseAdapter? And if yes, how I handle to get tostring() from Sherlock Searchview instead of filterText.addTextChangedListener(filterTextWatcher);?
Should I create a new BaseAdapter to download only the listview hint by the search? And If yes, Do you have any sample or tutorial to link?
Or anyother way?
Custom Listview:
public class CustomizedListView extends ListFragment {
// 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;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
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);
}
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(getActivity(), songsList);
setListAdapter(adapter);
}
}
BaseAdapter:
public class LazyAdapter extends BaseAdapter {
private FragmentActivity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(FragmentActivity fragmentActivity, ArrayList<HashMap<String, String>> d) {
activity = fragmentActivity;
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(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;
}
}
Sherlock ActionBar SearchView:
public abstract class BaseSampleActivity extends SherlockFragmentActivity {
private static final Random RANDOM = new Random();
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Procure pelo nome");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
Intent search = new Intent(getApplicationContext(), SearchableActivity.class);
search.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
search.putExtra(query, query);
startActivityForResult(search, 0);
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});
menu.add("Search")
.setIcon(R.drawable.ic_search_inverse)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
The NewActivity:
public class SearchableActivity extends SherlockListActivity{
// 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";
private String message;
private ListView list;
private LazyAdapterS adapter;
protected void onCreate(Bundle savedInstanceState, String query) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
message = getIntent().getExtras().getString(query);
setContentView(R.layout.main_list_row);
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 LazyAdapterS(this, songsList);
list.setAdapter(adapter);
((Filterable) list.getAdapter()).Filter(message);// Error The method Filter(String) is undefined for the type Filterable
}
}
UPDATE: I put the setOnQueryTextListener as Suggested by #Luksprog.
UPDATE: I´ve created a new Activity to show the results from the search, but still got a error on filter, I dont know how to handle this.
Thank you,
Should I implement Filterable on BaseAdapter?
If you want to use the standard filtering mechanism for an adapter(which any Android SDK adapter does), yes, you need to implement the Filterable interface and provide a proper implementation for it's two methods.
And if yes, how I handle to get tostring() from Sherlock Searchview
instead of filterText.addTextChangedListener(filterTextWatcher);?
Have a look at OnQueryTextListener:
public boolean onQueryTextSubmit(String query) {
((Filterable) listview.getAdapter()).filter(query);
return true;
}
Should I create a new BaseAdapter to download only the listview hint
by the search? And If yes, Do you have any sample or tutorial to link?
If you mean, parsing again the xml(using only what matches the query string) and create a new adapter to show the results, no because you'll be doing a lot of unnecessary work(especially all the network calls which you should really try move on a background thread).
If you want to show the results in a new activity then put the query String in the Intent that starts the results activity and do/redo the filtering there.
Or anyother way?
See point one.

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