Listview only displaying last item of arraylist - android

I have a problem. Currently i'm using Lazyloading to load images and data into my listview using arraylist. However it only display the last item of the arraylist.
Below is the LazyLoading adapter class:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Hashtable<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<Hashtable<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 static class ViewHolder{
public TextView title;
public TextView author;
public TextView format;
public TextView call_number;
public ImageView cover_image;
public Button next;
public Button previous;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null){
vi = inflater.inflate(R.layout.search_result_display_list , null);
holder=new ViewHolder();
holder.next = (Button) vi.findViewById(R.id.next);
holder.previous = (Button) vi.findViewById(R.id.previous);
holder.title = (TextView) vi.findViewById(R.id.item_title);
holder.author = (TextView) vi.findViewById(R.id.item_author);
holder.format = (TextView) vi.findViewById(R.id.item_format);
holder.call_number = (TextView) vi.findViewById(R.id.item_call_number);
holder.cover_image=(ImageView) vi.findViewById(R.id.cover_image);
vi.setTag(holder);
}
else {
holder=(ViewHolder)vi.getTag();
vi = convertView;
}
for(int i = 0; i < getCount(); i++) {
holder.title.setText(data.get(i).get("title"));
holder.author.setText(data.get(i).get("author"));
holder.format.setText(data.get(i).get("format"));
holder.call_number.setText(data.get(i).get("callNumber"));
holder.cover_image.setTag(data.get(i).get("coverImage"));
imageLoader.DisplayImage(data.get(i).get("coverImage"), activity, holder.cover_image);
}
return vi;
}
I'm thinking that the problem might actually be with my for-loop. Where it is being generated infinitely. But I don't know where did I go wrong in typing this code. Please help.

Remove the for loop and in place of i in data.get(i) use position

Related

DIsplay first letter of string to another textview in Android

I am displaying a listview in which, the row item has two textview. The second textview is for the name of companies and the first textview is for the starting letter of the companies. How this can be achieved ? Need help !!
public class ExampleAdapter extends BaseAdapter {
Context context;
ArrayList<ExamplePojo> items = new ArrayList<>();
public ExampleAdapter(Context context, ArrayList<ExamplePojo> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return items.indexOf(items.get(position));
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflater.inflate(R.layout.example_row_item, null);
holder = new ViewHolder();
holder.txtSentence = (TextView) convertView.findViewById(R.id.txtSentence);
holder.txtInitialLetter = (TextView) convertView.findViewById(R.id.txtInitialLetter);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().charAt(0));
return convertView;
}
public class ViewHolder{
TextView txtSentence, txtInitialLetter;
}
}
Here is code for showing first character in first textview and complete name in other textview.Also put below lines in if(convertView== null) block
final ExamplePojo pojo = items.get(position);
holder.txtSentence.setText(pojo.getSentence());
holder.txtInitialLetter.setText(pojo.getSentence().substring(0, 1));

Listview Scrolling changes Button Visibility?

I am having a listview in my App. Each row has an button. I'm hiding the button for some rows with setVisibility. But the buttons visibility changes after scrolling the list. How can i stop this change ?
I already saw a question with the checkbox with Listview. But i don't know how to implement it for the buttons. So please guide me!
ADAPTER
public class published_adapter extends BaseAdapter {
Context con;
ArrayList<HashMap<String, String>> class_list;
LayoutInflater inflater;
public class ViewHolder
{
TextView title,description,class_section,date;
ImageButton download;
Button viewasgn;
}
public published_adapter(Context co, ArrayList<HashMap<String, String>> list1) {
class_list = list1;
con = co;
inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return class_list.size();
}
#Override
public Object getItem(int arg0) {
return class_list.get(arg0).get("class_name");
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
View row = arg1;
ViewHolder holder = new ViewHolder();
if(row == null)
{
row = inflater.inflate(
R.layout.assignment_adapter_layout, arg2, false);
// initialize the elements
holder.download = (ImageButton) row.findViewById(R.id.download);
holder.title = (TextView) row.findViewById(R.id.title);
holder.description = (TextView) row.findViewById(R.id.description);
holder.class_section = (TextView) row.findViewById(R.id.class_section);
holder.date = (TextView) row.findViewById(R.id.date);
holder.viewasgn = (Button) row.findViewById(R.id.attend);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
String type = class_list.get(arg0).get("ASSIGNMENT_TYPE");
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
return row;
}
}
Update your code like this
if (class_list.get(arg0).get("TOTAL_SUBMISSION").equals("0")) {
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.INVISIBLE);
holder.viewasgn.setText("");
}
else{
Log.e("TITLE", class_list.get(arg0).get("TOTAL_SUBMISSION"));
holder.viewasgn.setVisibility(View.VISIBLE);
holder.viewasgn.setText("VIEW");
}
Either add the setVisibility(View.INVISIBLE) like #Solhail suggested or hide those buttons by default in your assignment_adapter_layout.xml by adding android:visibility="invisible" in your Button's properties, and don't change a thing in your current adapter code.
In your case Views will be recreated when listView is scroll.
To prevent this, you should not use ViewHolder.
You need to user Adapter like this:
(Only if you have not a lot elements)
public class SampleAdapter extends BaseAdapter {
private ArrayList<String> arrayList;
private Activity activity;
public SampleAdapter(ArrayList<String> arrayList, Activity activity) {
this.arrayList = arrayList;
this.activity = activity;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public Object getItem(int position) {
return arrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.sample_element, parent, false);
TextView textViewTitle = (TextView) view.findViewById(R.id.text_view_title);
textViewTitle.setText(arrayList.get(position));
return view;
} }

How should the BaseAdapter coding programmatically without a layout file?

There is a sample baseadapter as follows and it uses a layout xml file names adapter_layout. But I do not want to use the adapter_layout.xml. I want to identify with all components(TextView, EditText ...) in the class. How should the BaseAdapter coding programmatically without adapter_layout.xml?
public class LayoutAdapter extends BaseAdapter {
private List<String> objects = new ArrayList<String>();
private Context context;
private LayoutInflater layoutInflater;
public LayoutAdapter(Context context) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return objects.size();
}
#Override
public String getItem(int position) {
return objects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.adapter_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
viewHolder.editText1 = (EditText) convertView.findViewById(R.id.editText1);
convertView.setTag(viewHolder);
}
return convertView;
}
protected class ViewHolder {
private TextView textView2;
private TextView textView1;
private EditText editText1;
}
}

content of listview does not appear

my code is running without error but the custom Listview does not display the values .
my code is running without error but the custom Listview does not display the values .
this is my code:
the Adapter
public class Adapter extends BaseAdapter{
ArrayList<mealItems> itemList;
public Activity context;
public LayoutInflater inflater;
public DiseasesAdapter(Activity context,int x,ArrayList<mealItems> itemList) {
super();
this.context = context;
this.itemList = itemList;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
ImageView img;
TextView Title;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.disease_row, null);
holder.Title = (TextView) convertView.findViewById(R.id.Name);
holder.img = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
Items bean = (Items) itemList.get(position);
holder.Title.setText(bean.getTitle());
holder.img.setImageResource(bean.getImage());
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
Your getCount() method returns a 0, this should return the size of the itemList instead.
i.e return itemList.size()
Try this :
this.itemList=new ArrayList<mealItems>();
this.itemList.addAll(itemList);
And of course, the getCount() method to return itemList.size()

How to access ArrayList data to ListView which is having multiple textview and image icon in Android?

I had created an ArrayList which contains data responsed by server. Now Arraylist having all data which I preffered. But I want to display these data to list view.
Ex: ['videoName','VideoCategory','Thumbnail path']
This is arraylist:
mydata=['a1','science','/playback/2012/09_im.jpg','a2','maths','/playback/2012/01.jpg'.....]
Now I want to display 'videoName' in textView1 and 'VideoCategory' in textView2 and 'Thumbnail Path' to image icon.
I am trying ArrayList Adapter. But there are several problem occured.
Please Help me...
You'll have to use a custom ListView and you have to use BaseAdapter for this. This is the link of a example of custom ListView:
http://android-codes-examples.blogspot.in/2011/03/multiple-listview-and-custom-listview.html
set list.setAdapter(new EfficientAdapter(getApplicationContext()));
public class EfficientAdapter extends BaseAdapter {
ViewHolder holder;
private LayoutInflater mInflater;
Activity context = null;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return one.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
holder.text1 = (TextView) convertView.findViewById(R.id.TextView02);
holder.image = (ImageView) convertView.findViewById(R.id.ImageView01);
holder.btn = (Button) convertView.findViewById(R.id.B);
holder.btn.setFocusable(false);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(one.get(position));
holder.text1.setText("Stall No: "+two.get(position));
return convertView;
}
class ViewHolder {
TextView text;
TextView text1;
ImageView image;
Button btn;
}
Now use whatever you want is your ListView.

Categories

Resources