Footer view is not displayed at the end of the listview .
Pleas tell me where I am going wrong .The code for the footerview is :
footerView = ((LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.listfooter, null, false);
list.addFooterView(footerView);
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Entire Code :
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";
static ListView list;
static LazyAdapter adapter;
HashMap<String, String> map;
public static String message_receiver_str;
public static int deleteposition;
public static ArrayList<HashMap<String, String>> songsList;
View footerView ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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) {
#SuppressWarnings("unchecked")
HashMap<String, String> o= (HashMap<String, String>) list.getItemAtPosition(position);
// Toast.makeText(CustomizedListView.this, "ID '" + o.get("title") + "' was clicked.", Toast.LENGTH_SHORT).show();
deleteposition=position;
message_receiver_str= o.get("title");
Intent ii= new Intent(getBaseContext(),openedmsg.class);
startActivity(ii);
}
});
footerView = ((LayoutInflater)getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.listfooter, null, false);
list.addFooterView(footerView);
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
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 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
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;
}
}
listfooter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:padding="13dp"
android:layout_height="fill_parent">
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="15dp"
android:text="Tap to Load"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
You must call addFooterView() before you call setAdapter() on the ListView. Same applies for addHeaderView().
Related
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.
I have a ListView which displays a list with two TextViews. On one of them, I want to apply a specific color. All the data (and colors) are provided by a Json file from web service.
I use a simple adapter which I want to use. Could someone indicate how to put the specific color for each item?
Here is my class:
public class NetworkTimeTableFragment extends Fragment {
private ProgressDialog pDialog;
private static String mylat;
private static String mylng;
// JSON Node names
private static final String TAG_NAME = "shop_name";
private static final String TAG_SHORT = "shop_address";
private static final String TAG_ID = "shop_url";
private static final String TAG_COLOR = "feuilletez";
TextView shop_address;
TextView shop_name;
TextView shop_url;
ImageButton feuilletez;
private ListView list;
// Hashmap for ListView
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
public NetworkTimeTableFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_timetable, container, false);
// Calling async task to get json
new GetJson().execute();
return rootView;
}
/**
* Async task class to get json by making HTTP call
*/
private class GetJson extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
String myurl = "routes.json";
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(myurl, ServiceHandler.GET);
JSONArray array = null;
try {
array = new JSONArray(jsonStr);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String id = jsonObject.getString("id");
System.out.println("id --->" + id);
String url = jsonObject.getString("url");
System.out.println("url --->" + url);
String created_at = jsonObject.getString("created_at");
System.out.println("created_at --->" + created_at);
String updated_at = jsonObject.getString("updated_at");
System.out.println("updated_at --->" + updated_at);
String short_name = jsonObject.getString("short_name");
System.out.println("short_name --->" + short_name);
String long_name = jsonObject.getString("long_name");
System.out.println("long_name --->" + long_name);
String desc = jsonObject.getString("desc");
System.out.println("desc --->" + desc);
String type = jsonObject.getString("type");
System.out.println("type --->" + type);
String type_name = jsonObject.getString("type_name");
System.out.println("type_name --->" + type_name);
String color = jsonObject.getString("color");
System.out.println("color --->" + color);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, long_name);
map.put(TAG_SHORT, short_name);
map.put(TAG_COLOR, color);
map.put(TAG_ID, id);
oslist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list = (ListView) getActivity().findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(
getActivity(),
oslist,
R.layout.listview_routes_row,
new String[]{TAG_NAME, TAG_SHORT, TAG_COLOR},
new int[]{R.id.long_name, R.id.short_name});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Intent intent = new Intent(NewsActivity.this, NewsActivity.class);
//intent.putExtra("shopurl",oslist.get(+position).get(TAG_URL));
// overridePendingTransition(R.anim.animationin, R.anim.animationout);
// startActivity(intent);
}
});
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
The better way to achieve what you want is to create custom adapter. Here I am giving you an example to achieve your goal...
Activity Layout---> activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
List Item Layout---> listview_routes_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/long_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip" />
<TextView
android:id="#+id/short_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dip" />
</LinearLayout>
Custom SimpleAdapte---> CustomSimpleAdapter.java
public class CustomSimpleAdapter extends SimpleAdapter {
private List<Map<String, Object>> itemList;
private Context mContext;
private static final String TAG_COLOR = "color";
private static final String TAG_NAME = "shop_name";
private static final String TAG_SHORT = "shop_address";
public CustomSimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.itemList = (List<Map<String, Object>>) data;
this.mContext = context;
}
/* A Static class for holding the elements of each List View Item
* This is created as per Google UI Guideline for faster performance */
class ViewHolder {
TextView textLong;
TextView textShort;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_routes_row, null);
holder = new ViewHolder();
// get the textview's from the convertView
holder.textLong = (TextView) convertView.findViewById(R.id.long_name);
holder.textShort = (TextView) convertView.findViewById(R.id.short_name);
// store it in a Tag as its the first time this view is generated
convertView.setTag(holder);
} else {
/* get the View from the existing Tag */
holder = (ViewHolder) convertView.getTag();
}
/* update the textView's text and color of list item */
holder.textLong.setText((CharSequence) itemList.get(position).get(TAG_NAME));
holder.textShort.setText((CharSequence) itemList.get(position).get(TAG_SHORT));
holder.textShort.setTextColor((Integer) itemList.get(position).get(TAG_COLOR));
return convertView;
}
}
Activity Class---> MainActivity.java
public class MainActivity extends Activity {
private static final String TAG_NAME = "shop_name";
private static final String TAG_SHORT = "shop_address";
private static final String TAG_COLOR = "color";
private List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
private Map<String, Object> map;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view);
//Sample data insertion into the list
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview11");
map.put(TAG_SHORT, "textview12");
map.put(TAG_COLOR, Color.BLUE);
itemList.add(map);
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview21");
map.put(TAG_SHORT, "textview22");
map.put(TAG_COLOR, Color.GREEN);
itemList.add(map);
map = new LinkedHashMap<String, Object>();
map.put(TAG_NAME, "textview31");
map.put(TAG_SHORT, "textview32");
map.put(TAG_COLOR, Color.RED);
itemList.add(map);
/* create an adapter for listview*/
SimpleAdapter adapter = new CustomSimpleAdapter(this, itemList,
R.layout.listview_routes_row, new String[] { TAG_NAME, TAG_SHORT }, new
int[] { R.id.long_name, R.id.short_name });
listView.setAdapter(adapter);
}
}
I think it will help you. If you have any problem then let me know.
For creating custom view for each item you'll have to create your own adapter instead of using the defaults one, but it's pretty easy:
public class CustomAdapter extends SimpleAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Object item = getItem(position);
TextView text = v.findViewById(//your text view id);
ColorStateList color = //get color for item;
text.setTextColor(color);
return v;
}
}
you can try this:textview.setTextColor(color);
I am new to android development. I have implemented an activity which is Loading Json data to a listview. I have two activities now. First one is login Activity.Second one is loading list view activity.the json data is loading perfectly. but my problem is, when i pressed back button, the list view data is removing one by one. i have no idea where the problem is.
here is my main activity
public class MainActivity extends Activity {
ListView list;
TextView ver;
TextView name;
TextView api;
ImageButton login;
public static ArrayList<HashMap<String, String>> oslist;
//URL to get JSON Array
private static String url = "http://api.learn2crack.com/android/jsonos/";
//JSON Node Names
static final String TAG_OS = "android";
static final String TAG_VER = "ver";
static final String TAG_NAME = "name";
static final String TAG_API = "api";
JSONArray android = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loging);
oslist = new ArrayList<HashMap<String, String>>();
login = (ImageButton)findViewById(R.id.imageButton1);
new ArrayList<HashMap<String, String>>();
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
ver = (TextView)findViewById(R.id.title);
name = (TextView)findViewById(R.id.artist);
api = (TextView)findViewById(R.id.time);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
JsonParser jParser = new JsonParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
// TODO Auto-generated method stub
pDialog.dismiss();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String ver = c.getString(TAG_VER);
String name = c.getString(TAG_NAME);
String api = c.getString(TAG_API);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_VER, ver);
map.put(TAG_NAME, name);
map.put(TAG_API, api);
oslist.add(map);
Intent reult = new Intent(MainActivity.this,ViewList.class);
//reult.putStringArrayListExtra("map", oslist);
//startActivity(reult);
reult.putExtra("arraylist", oslist);
startActivityForResult(reult, 500);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
Here is my Listview Activity
public class ViewList extends Activity {
private static final String TAG_VER = "ver";
private static final String TAG_NAME = "name";
private static final String TAG_API = "api";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Intent getres = getIntent();
//HashMap<String, String> hashMap = (HashMap<String, String>)getres.getSerializableExtra("map");
final ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
ListView list=(ListView)findViewById(R.id.list);
/* ListAdapter adapter = new SimpleAdapter(ViewList.this, arl,
R.layout.row,
new String[] { TAG_VER,TAG_NAME, TAG_API }, new int[] {
R.id.title,R.id.artist, R.id.time});*/
NewsRowAdapter adapter = new NewsRowAdapter(ViewList.this, R.layout.row, arl);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(ViewList.this, "You Clicked at "+arl.get(+position).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
}
this is my Adapter Class
public class NewsRowAdapter extends BaseAdapter {
/*//ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
//ArrayList<HashMap<String, String>> data;
private List<Item> items;
//private Item objBean;
private int row;*/
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/*public NewsRowAdapter(Activity act, int resource, HashMap<String, List<String>> listChildData)) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.data = arrayList;
}*/
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
TextView title = (TextView) vi.findViewById(R.id.title);
TextView artist = (TextView) vi.findViewById(R.id.artist);
TextView time = (TextView) vi.findViewById(R.id.time);
ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
title.setText(song.get(MainActivity.TAG_VER));
artist.setText(song.get(MainActivity.TAG_NAME));
time.setText(song.get(MainActivity.TAG_API));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
Button accept = (Button) vi.findViewById(R.id.btnaccept);
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//CustomizedListView getlist = new CustomizedListView();
final int x = (int) getItemId(position);
Toast.makeText(parent.getContext(),"you clicked "+ x , Toast.LENGTH_SHORT).show();
}
});
vi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
}
});
return vi;
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
#Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
please some one help me...
Yes it will do so, When you press back button your activity get finished , So the view inside the activity will get deleted , Either you must keep the data in a seperate class, outside activity or keep the activity alive.
In my ListView there are 2 TextViews and an ImageView.
I am using a LazyAdapter to load images from JSON file according to this link.
I am successfully loading data but i am facing the problem that each list item shows in both TextViews the link of the image while the image itself is shown correctly.
Here is the adapter:
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());
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView name = (TextView)vi.findViewById(R.id.name);
TextView price = (TextView)vi.findViewById(R.id.price);
ImageView image=(ImageView)vi.findViewById(R.id.image);
HashMap<String, String> smart = new HashMap<String, String>();
smart = data.get(position);
id.setText(smart.get(SmActivity.KEY_ID));
name.setText(smart.get(SmActivity.KEY_NAME));
price.setText(smart.get(SmActivity.KEY_PRICE));
imageLoader.DisplayImage(smart.get(SmActivity.KEY_THUMB), image);
return vi;
}
}
And here is the activity:
public class SmActivity extends ListActivity {
static String url = "url-php to get json";
public static String KEY_PRICE, KEY_NAME, KEY_THUMB;
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> smList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJson(url);
try {
JSONArray smJArray = json.getJSONArray("sm");
for(int i = 0; i < smJArray.length(); i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonObj = smJArray.getJSONObject(i);
map.put(KEY_NAME, jsonObj.getString("product_name"));
map.put(KEY_PRICE, jsonObj.getString("price")+"€");
map.put(KEY_THUMB, jsonObj.getString("image_main"));
smList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
}
list = (ListView)getListView().findViewById(android.R.id.list);
adapter=new LazyAdapter(this, smList);
list.setAdapter(adapter);
}
}
I can understand that ListView shows at all items the value of the last map.put(...) i am using.
For example, if the last put in HashMap is map.put(KEY_PRICE, jsonObj.getString("price")+"€");
i get the price value not only on price, but on name TextView as well. And of course i get no image.
I would be grateful in any help.
Thanks in advance.
Ok. i got it. i replaced:
public static String KEY_PRICE, KEY_NAME, KEY_THUMB;
by
static final String KEY_NAME = "product_name";
static final String KEY_PRICE = "price";
and
map.put("product_name", jsonObj.getString(KEY_NAME));
map.put("price", jsonObj.getString(KEY_PRICE));
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);