Not able to display the image on listview - java

I am trying to display a image downloading from the url and displaying it in the listview.
This is my source code. The output has a blank spot.. that's all...
This is my java from which I am calling.
ABC s = ABC.getSingletonObject();
String[][] full_data = s.getString();
private ListView listView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
//second one is always the type of data...
Weather weather_data[] = new Weather[]
{ // full_data[1][0] contains the URL String..
new Weather(full_data[1][0],full_data[1][2],full_data[1][3]),
new Weather(full_data[2][0],full_data[2][2],full_data[2][3]),
};
WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_header_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
This is my adapter ..
public class WeatherAdapter extends ArrayAdapter<Weather> {
Context context;
int layoutResourceId;
Weather data[] = null;
public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeatherHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeatherHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView)row.findViewById(R.id.txt1);
holder.txtRating=(TextView)row.findViewById(R.id.rtxt1);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.wtitle);
holder.imgIcon.setImageDrawable(weather.wicon);
holder.txtRating.setText(weather.wrating);
//holder.imgIcon.setImageResource(R.drawable.ic_launcher);
//holder.imgIcon.setImageBitmap(weather.wicon);
return row;
}
static class WeatherHolder
{
//Bitmap imgIcon;
ImageView imgIcon;
TextView txtTitle;
TextView txtRating;
}
}
this is the other java file
public class Weather {
public Drawable publicdraw;
public Drawable wicon;
public String wtitle;
public String wrating;
public Weather(){
super();
}
public Weather(String icon, String title,String rating) {
super();
LoadImageFromWebOperations(icon);
this.wtitle = title;
this.wrating=rating;
this.wicon=publicdraw;
}
public void LoadImageFromWebOperations(String url_string) {
try {
grabImage(url_string);
} catch (Exception e) {
}
}
public void grabImage(String url) {
new GrabURL1().execute(url);
}
private class GrabURL1 extends AsyncTask<String, Void, Void> {
public Drawable d;
InputStream is;
protected Void doInBackground(String... urls) {
try {
is = (InputStream) new URL(urls[0]).getContent();
d = Drawable.createFromStream(is, "src name");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void unused) {
publicdraw=d;
}
}
}

I recommend you to go through this tutorial. Straight forward, not difficult and very efficient. I am using it for my projects where I load images on listview.If you get stucked somewhere please ask.

Try adding following condition before assigning image to your list.
if (holder.imgIcon != null && position < weather_data.size()) {
Weather weather = data[position];
holder.imgIcon.setImageDrawable(weather.wicon);
}
I have the same problem and this trick worked like a charm for me.
Thanks.

Related

GridView is not showing retrieved data from Json

I have a gridview which should show emojies that are retrieved from the server as urls, I was able to retrieve the urls and put it inside an arraylist, however using the gridview adapter, no images show at all, I've tried debugging by handcoding the url outside the for loop and it showed the image, which means that nothing is wrong with my adapter, also when I try to hardcode the url inside the for loop like arraylist.add("the url") no image appears, here's my code, please advise why the images are not showing, appreciate your assistance
BottomSheetDialog_Smiles.java
Communicator.getInstance().on("subscribe start", new Emitter.Listener() {
#Override
public void call(Object... args) {
try {
JSONDictionary response = (JSONDictionary) args[0];
String str = response.get("emojiPack").toString();
JSONArray emojies = new JSONArray(str);
for (int i = 0; i < emojies.length(); i++) {
JSONObject response2 = (JSONObject)
emojies.getJSONObject(i);
emojiModel = new EmojiModel((String)
response2.get("urlFile"));
emojiUrl = emojiModel.getEmojiFile();
JSONDictionary t =
JSONDictionary.fromString(response2.toString());
emojiModel.init(t);
emojieModels.add(emojiModel);
}
ImageAdapter2 emojiAdapter = new
ImageAdapter2(getApplicationContext(), emojieModels);
gridView2.setAdapter(emojiAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
ImageAdapter2.java
public class ImageAdapter2 extends BaseAdapter {
private Context context;
private ArrayList<String> emojieImages = new ArrayList<String>();
// Constructor
public ImageAdapter2(Context context, ArrayList<String>
emojieImagesList) {
this.context = context;
this.emojieImages = emojieImagesList;
}
#Override
public int getCount() {
return emojieImages.size();
}
#Override
public Object getItem(int position) {
return emojieImages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView imageView;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smiles_items_layout, null);
holder.imageView = (ImageView)
grid.findViewById(R.id.smile_image_view);
Picasso.with(getApplicationContext())
.load(emojieImages.get(position))
.fit()
.centerInside()
.into(holder.imageView);
return grid;
}
}
EmojiModel.java
public class EmojiModel {
private int id;
private int price;
public String urlFile;
public EmojiModel(String urlFile) {
this.id=id;
this.price=price;
this.urlFile=urlFile;
}
public String getEmojiFile() {
return urlFile;
}
public void init(JSONDictionary data){
try{
urlFile = (String) data.get("urlFile");
id = Integer.parseInt((String) data.get("id"));
price = Integer.parseInt((String) data.get("price"));
}catch(Exception e){
e.printStackTrace();
}
}
}
Try this
Picasso.with(this)
.load(imageUrl)
.fit()
.centerInside()
.into(imageViewFromUrl, new Callback() {
#Override
public void onSuccess() {
Log.i(TAG, "succcess");
}
#Override
public void onError() {
Log.i(TAG, "error");
}
}
);

Parse Data from XML into List with Custom Adapter

I am getting data from an XML uploaded on a website. I want to display the text from the XML in a custom ListView, which has two TextViews. The 'title' should go into the upper TextView and the 'guid' into the lower TextView. I'm not sure how I should go about doing this. I've written the following Custom Adapter.
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private ArrayList list;
private LayoutInflater layoutInflater;
public CustomAdapter(Context context, ArrayList list) {
this.list= list;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.layout_list_row, null);
holder = new ViewHolder();
holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
holder.topText = (TextView) convertView.findViewById(R.id.topText);
holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DiscourseItem discourseItem = (DiscourseItem) discourseList.get(position);
holder.topText.setText(discourseItem.getTopText());
holder.bottomText.setText(discourseItem.getBottomText());
return convertView;
}
static class ViewHolder {
ImageView backgroundImage;
TextView topText; //This should display the text in the 'title' field
TextView bottomText; //This should display the text in the 'guid' field
}
}
I'm currently able to display the two separately in separate ListViews with normal ArrayAdapters. Here's the code I've written.
XMLParser.java
public class XMLParser extends AsyncTask {
private URL url;
public ArrayList<String> title = new ArrayList<>();
public ArrayList<String> guid = new ArrayList<>();
#Override
protected Object doInBackground(Object[] objects) {
try {
url = new URL(removed);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), null);
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")){
if (insideItem)
title.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("guid")){
if (insideItem)
guid.add(xpp.nextText());
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem = false;
}
eventType = xpp.next();
}
} catch (MalformedURLException e){
e.printStackTrace();
} catch (XmlPullParserException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
public InputStream getInputStream(URL url){
try {
return url.openConnection().getInputStream();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
public ArrayList<String> titles(){
return title;
}
public ArrayList<String> guids(){
return guid;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parser = new XMLParser();
parser.execute();
title = parser.titles();
guid = parser.guids();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
discourseList = getDiscourseList();
}
}, 2000);
}
});
ListView listView = (ListView) findViewById(R.id.discourseList);
listView.setAdapter(new CustomAdapter(this, discourseList));
}
private ArrayList<DiscourseItem> getDiscourseList() {
ArrayList<DiscourseItem> listData = new ArrayList<DiscourseItem>();
String[] topText = new String[title.size()];
topText = title.toArray(topText);
String[] bottomText = new String[guid.size()];
bottomText = guid.toArray(bottomText);
for (int i = 0; i <= title.size(); i++) {
try {
DiscourseItem discourseItem = new DiscourseItem();
discourseItem.setTopText(topText[i]);
discourseItem.setBottomText(bottomText[i]);
listData.add(discourseItem);
} catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
return listData;
}
}
Edit :
I've made the above changes. Now when the parser runs and calls getDiscourseList(), it throws the following error at discourseItem.setTopText(topText[i]); :
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
You can create wrapper class for your upper and bottom values ( something like this, consider another name as I don't know the context of your data):
public class TitleGuidPair {
private final String title;
private final String guid;
public TitleGuidPair(String title, String guid) {
this.title = title;
this.guid = guid;
}
//getters
}
And then parse your result to ArrayList of TitleGuidPair. If you want to keep your parsing algorithm you can make some post-processing and build your TitleGuidPairs afterwards from the two lists that you have.
Having that step behind just pass to your adapter List of TitleGuidPairs and in the getView method set top and bottom text like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.layout_list_row, null);
holder = new ViewHolder();
holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
holder.topText = (TextView) convertView.findViewById(R.id.topText);
holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
TitleGuidPair titleGuidPair = list.get(position);
holder.topText.setText(titleGuidPair.getTitle());
holder.bottomText.setText(titleGuidPair.getGuid());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}

How to correct download multiple images(thumbnails for videos)

I write client for kaltura media platform.
I have an activity "Video". In this activity I must display list of video with thumbnails
First of all, I download list of videos using kaltura api, and this works fine. Every video entry have a field thumbnailUrl, which point out to thumbnail for video.
When I download this thumbnails for videos, it's(thumbnails) downloads in incorrect order, some thumbnails not downloaded, some thumbnails repeated for other video, and some not downloaded.
This is code of callback if downloadind videos:
private void handleFetchvideoListTask(List<VideoObject> videoObjects){
LinearLayout videoListProgressBarContainer = (LinearLayout)mActivity.findViewById(R.id.videoListProgressBarContainer);
LinearLayout videoListViewContainer = (LinearLayout)mActivity.findViewById(R.id.videoListViewContainer);
videoListProgressBarContainer.setVisibility(View.GONE);
videoListViewContainer.setVisibility(View.VISIBLE);
ListView listView = (ListView)videoListViewContainer.findViewById(R.id.videoListView);
VideoListAdapter adapter = new VideoListAdapter((LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
adapter.setList(videoObjects);
listView.setAdapter(adapter);
DownloadThumbnailsTask task = new DownloadThumbnailsTask(videoObjects, adapter);
task.execute();
}
This is code of DownloadThumbnailsTask:
public class DownloadThumbnailsTask extends AsyncTask<List<VideoObject>, VideoObject, Void>{
private List<VideoObject> videoObjectList = null;
private List<VideoObject> toProcess = null;
private VideoListAdapter adapter = null;
public DownloadThumbnailsTask(List<VideoObject> videoObjectList, VideoListAdapter adapter){
this.videoObjectList = videoObjectList;
this.toProcess = new ArrayList<VideoObject>(videoObjectList);
this.adapter = adapter;
}
#Override
protected Void doInBackground(List<VideoObject>... lists) {
for(int i=0; i<toProcess.size(); i++){
VideoObject item = videoObjectList.get(i);
String urldisplay = item.getThumbnailUrl();
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
item.setThumbnail(mIcon11);
mIcon11 = null;
publishProgress(item);
}
return null;
}
#Override
protected void onProgressUpdate(VideoObject... values) {
if(values != null && values.length > 0){
VideoObject item = values[0];
videoObjectList.remove(item);
videoObjectList.add(item);
adapter.setList(videoObjectList);
adapter.notifyDataSetChanged();
}
}
#Override
protected void onPostExecute(Void aVoid) {
this.toProcess.clear();
this.toProcess = null;
}
}
This is code of adapter:
public class VideoListAdapter extends BaseAdapter {
private List<VideoObject> list;
private LayoutInflater layoutInflater;
public VideoListAdapter(LayoutInflater layoutInflater){
this.layoutInflater = layoutInflater;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return list.get(i).getId();
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = layoutInflater.inflate(R.layout.list_item_video, viewGroup, false);
}
ViewHolder viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
VideoObject item = getVideoObjectItem(i);
viewHolder.duration.setText(item.getFormattedDuration());
viewHolder.title.setText(item.getName());
if(item.getThumbnail() != null){
viewHolder.thumbnail.setImageBitmap(item.getThumbnail());
}
return view;
}
private VideoObject getVideoObjectItem(int position){
return (VideoObject) getItem(position);
}
public List<VideoObject> getList() {
return list;
}
public void setList(List<VideoObject> list) {
this.list = list;
}
public static class ViewHolder {
public final ImageView thumbnail;
public final TextView title;
public final TextView duration;
public ViewHolder(View view) {
this.thumbnail = (ImageView)view.findViewById(R.id.imgVideoThumbnail);
this.title = (TextView)view.findViewById(R.id.txtVideoName);
this.duration = (TextView)view.findViewById(R.id.txtVideoDuration);
}
}
}
And this is result of my code:
I noticed two things:
Inside doInBackground you are waiting
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
to complete before downloading the next image.
Try this:
protected Void doInBackground(List<VideoObject>... lists) {
for(final int i=0; i<toProcess.size(); i++){
try {
new Thread(){
#Override
public void run() {
VideoObject item = videoObjectList.get(i);
String urldisplay = item.getThumbnailUrl();
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
item.setThumbnail(mIcon11);
publishProgress(item);
}
}.start();
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
}
return null;
}
#Override
protected void onProgressUpdate(VideoObject... values) {
adapter.notifyDataSetChanged();
}
I think this two lines is the problem:
videoObjectList.remove(item);
videoObjectList.add(item);
You're removing an item and adding again at the bottom. Just call notifyDataSetChanged after setting the bitmap.
Inside getView you're creating a new ViewHolder every time.
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = layoutInflater.inflate(R.layout.list_item_video, viewGroup, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = view.getTag();
}
VideoObject item = getVideoObjectItem(i);
viewHolder.duration.setText(item.getFormattedDuration());
viewHolder.title.setText(item.getName());
if(item.getThumbnail() != null){
viewHolder.thumbnail.setImageBitmap(item.getThumbnail());
}
return view;
}
Do you get anything in your logcat? Maybe opening the stream or decoding fails. Are the images large?

Listview order.runOnUiThread?

Hi guys I have a problem with my ListView...
This is my code:
private ArrayAdapter<String> adapter ;
private ProgressDialog m_ProgressDialog = null;
private ArrayList<Order> m_orders = null;
public OrderAdapter m_adapter;
private Runnable viewOrders;
public class Order {
private String orderName;
private String orderStatus;
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public String getOrderStatus() {
return orderStatus;
}
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
}
private class OrderAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> items;
public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_single, null);
}
Order o = items.get(position);
if (o != null) {
// TextView tv2 = (TextView) v.findViewById(R.id.tv2);
TextView tv1 = (TextView) v.findViewById(R.id.tv1); //codice volo
TextView tv2 = (TextView) v.findViewById(R.id.tv2); // citta
TextView tv5 = (TextView) v.findViewById(R.id.tv5); //stato volo
TextView tv6 = (TextView) v.findViewById(R.id.tv6); //ora prevista
TextView tv7 = (TextView) v.findViewById(R.id.tv7); //ora stimata if (tt != null) {
tv1.setText("VOLO: "+o.getOrderName());
}
return v;
}
}
private void getOrders(){
try{
m_orders = new ArrayList<Order>();
Order o1 = new Order();
o1.setOrderName("SF services");
o1.setOrderStatus("Pending");
Order o2 = new Order();
o2.setOrderName("SF Advertisement");
o2.setOrderStatus("Completed");
m_orders.add(o1);
m_orders.add(o2);
Thread.sleep(2000);
Log.i("ARRAY", ""+ m_orders.size());
} catch (Exception e) {
Log.e("BACKGROUND_PROC", e.getMessage());
}
runOnUiThread(returnRes);
}
public LayoutInflater getSystemService(String layoutInflaterService) {
// TODO Auto-generated method stub
return null;
}
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_ProgressDialog.dismiss();
m_adapter.notifyDataSetChanged();
}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View ios = inflater.inflate(R.layout.arrivi, container, false);
m_orders = new ArrayList<Order>();
this.m_adapter = new OrderAdapter(getActivity(), R.layout.list_single, m_orders);
setListAdapter(getActivity(),m_adapter);
viewOrders = new Runnable(){
#Override
public void run() {
getOrders();
}
};
Thread thread = new Thread(null, viewOrders, "MagentoBackground");
thread.start();
m_ProgressDialog = ProgressDialog.show(getActivity(),
"Please wait...", "Retrieving data ...", true);
// new MyTask().execute("");
return ios;
}
private void setListAdapter(FragmentActivity activity,
OrderAdapter m_adapter2) {
// TODO Auto-generated method stub
}
In eclipse I get this error:
The method runOnUiThread(Runnable) is undefined for the type arrivi
This code isn't in MainActivity but in a fragment named arrivi.
The file XML is list_single.xml and is associated at custom rows!
Can you help me please?
Another question...
How to implement AsyncTask for replacing Thread?
public class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String info;
.... When I run my app... it's in loop in "Retrieving data ..."
Thank you so much!!
runOnUiThread is a method of Activity subclasses. Inside a Fragment :
getActivity().runOnUiThread(runnable);
You need to create an instance for AsyncTask like this and execute your program on background.
new AsyncTask<Void, Integer, Long>(){
#Override
protected Long doInBackground(Void... params) {
try {
// call you method here
} catch (Exception ex) {
// handle the exception here
}
return null;
}
}.execute();
runOnUiThread is used to execute some program in main Thread. When you call runOnUiThread , the action will be executed immediately if we are already in UIThread else will be posted in Queue.
You need to use the Activity Context associated with your Fragment to `runOnUiThread'.
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
// execute your code here
}
});

How to update data into an adapter class

What I want to do is to append a list item at the bottom Just Like "Load More" functionality.
What I've performed i appended an extra items with empty values into adapter for ArrayList.
It's working fine for the first time but when i load more items it's adding Load More ItemView two times after every 15 items While i didn't make any changes into original ArrayList. The Changes has been made only into adapter class's constructor.
Here's the adapter class:
public class ContactListAdaptor extends BaseAdapter {
private Context context;
private ArrayList<DirectroyDetails> items = new ArrayList<DirectroyDetails>();
private Activity activity = null;
boolean isSearch = false;
private View currentView = null;
private String categoryName = null;
private HashMap< String, String> bannerHash = new HashMap<String, String>();
ContactList ACTIVITY;
public ContactListAdaptor(Context context, final ArrayList<DirectroyDetails> items2, Activity activity, String categoryName, HashMap< String, String> bannerHash, ContactList ACTIVITY, boolean expand) {
// super(context, textViewResourceId, items);
items = items2;
if(expand)
{
DirectroyDetails dummy = new DirectroyDetails();
dummy.setId("-1");
items.add(dummy);
}
this.context = context;
this.activity = activity;
this.categoryName = categoryName;
this.bannerHash = bannerHash;
this.ACTIVITY = ACTIVITY;
}
/*public ContactListAdaptor(Context context, Vector<Directory> items,
Activity activity, boolean isSearch) {
// TODO Auto-generated constructor stub
this.items = items;
this.context = context;
this.activity = activity;
this.isSearch = isSearch;
}*/
// #Override
public View getView(final int position, View convertView, ViewGroup parent) {
final DirectroyDetails directroyDetails = items.get(position);
System.out.println("id======="+directroyDetails.getId().toString());
try {
if(convertView == null)
{
System.out.println("##THE POSITION IS=="+position);
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.item_directory, null);
if(directroyDetails.getId().toString().equals("-1"))
convertView.setId(-1);
else
convertView.setId(1);
}else {
currentView = convertView;
}
//To fatch the values from the Event object
TextView titleText = (TextView)convertView.findViewById(R.id.title);
TextView nameText = (TextView)convertView.findViewById(R.id.name);
TextView numberText = (TextView)convertView.findViewById(R.id.number);
ImageView profilePic = (ImageView)convertView.findViewById(R.id.eventimage);
System.out.println("the position is ==== " +position);
titleText.setText(directroyDetails.getCompanyname());
nameText.setText(directroyDetails.getFirstname() + " " + directroyDetails.getSurname());
numberText.setText(directroyDetails.getMobile());
System.out.println("id at position======="+directroyDetails.getId().toString()+" at=="+position);
RelativeLayout overlay = (RelativeLayout)convertView.findViewById(R.id.overlay);
if(directroyDetails.getId().toString().equals("-1"))
{
overlay.setVisibility(View.VISIBLE);
System.out.println("at here to make the Overl;ay visible");
}else {
overlay.setVisibility(View.INVISIBLE);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("the exception occurs at:-" + e.getMessage());
}
return convertView;
}
public int getCount() {
System.out.println("the item size====="+items.size());
return items.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
}
private void onScrollUpdateList() {
// TODO Auto-generated method stub
Handler onScrollList = new Handler();
onScrollList.postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
setContactList();
}
}, 200);
}
& here is my calling code:-
if(contactDb.size()>0 && contactDb!=null)
{
System.out.println("theh responseData1 before new additions==="+responseData1.size());
responseData1 = addMoreData(contactDb,responseData1);
System.out.println("theh responseData1==="+responseData1.size());
if(responseData1 != null)
{
if(responseData1.size()>0)
{
ContactListAdaptor contactAdaptor = new ContactListAdaptor(getApplicationContext(), responseData1, this, CategoryName, bannerHash, ContactList.this, expand);
contactAdaptor.notifyDataSetChanged();
listview.setAdapter(contactAdaptor);
try {
listview.setSelection(getSelectedItems());
} catch (Exception e) {
// TODO: handle exception
System.out.println("the exception at scrolling and set :- " + e.getMessage());
}
}else
{
Toast.makeText(ContactList.this, "No Item found.", Toast.LENGTH_LONG).show();
}
}}
Please review below link code and try to resolve your issue with load more data dynamically with list view..
http://www.developerfusion.com/article/145373/android-listviews-with-dynamic-data/
If you have any query then let me know..

Categories