0

I'm little lost filling a ListView from a List. I can fill it with one element, but I want to get three elements from the object, so when I touch each element the app go to the link it contain. In my actual code I just can show the title

protected  void onPostExecute (Boolean result){

            List <String> title = new ArrayList<String>();
            List <String> link = new ArrayList<>();
            List <String> date = new ArrayList<>(); 
            for(int i=0;i<news.size();i++)
            {
                title.add(news.get(i).getTitle());
                link.add(news.get(i).getLink());
                date.add(news.get(i).getDate());
            }
            ArrayAdapter <String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,title);
            result.setAdapter(adapter);
            dialog.dismiss();
        }

In the arraylist date and link, I save the correct data, but I just can use one of them. My intention is to put date below the title and when you touch each element, the browser opens with the link selected.

Thanks a lot.

EDIT: I've done the custom adapter, but it gives me an error.

 class CustomAdapter extends ArrayAdapter<whatsnew> {

            public CustomAdapter(Context context, whatsnew[] data) {
                super(context, R.layout.listitem, data);
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = LayoutInflater.from(getContext());
                View item = inflater.inflate(R.layout.listitem, null); //This give me a warning

                TextView lblTitle = (TextView)item.findViewById(R.id.lbltitle);
                lblTitle.setText(data[position].getTitle());

                TextView lbldate = (TextView)item.findViewById(R.id.lbldate);
                lbldate.setText(data[position].getFecha());

                return(item);

            }

        }
        CustomAdapter adaptader = new CuestomAdapter(getActivity(), data);
        result.setAdapter(adaptador);
        dialog.dismiss();

My Logcat says:

Java NullPointerException: storage == null

The solution is change "whatsnew[] data" by "List data" in the public CustomAdapter method since I have all the data in ArrayList named data.

user1423168
  • 155
  • 1
  • 1
  • 13
  • possible duplicate of [Custom Adapter for List View](http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) – Rolf ツ Apr 19 '15 at 08:18
  • No, just me putting the code here, solved. Thanks for the advise. The adapter works fine and fill the ListView with the element Title. But I want to add the date and the link to the ListView and that's the problem – user1423168 Apr 19 '15 at 08:27

1 Answers1

1

Don't use a list of strings, a better solution would be using an independent class that set and get those three variables, let's name this class (for example) Data:

public class Data{
private String title;
private String link;
private String date;
//define getters and setters here
}

then use an object of type data inside your list:

List <Data> data = new ArrayList<Data>();

you also have to make a custom adapter that extends Data:

public class ImageAdapter extends ArrayAdapter<Data>{



Context context;
    int layoutResourceId;   

ArrayList<Data> data=new ArrayList<Data>();
public ImageAdapter(Context YourClass, int layoutResourceId, ArrayList<Data> data) {
    super(YourClass, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = YourClass;
    this.data = data;
}




@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ImageHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ImageHolder();
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.txtlink = (TextView)row.findViewById(R.id.txtTitle1);
        holder.txtDate = (TextView)row.findViewById(R.id.txtTitle1);
        row.setTag(holder);
    }
    else
    {
        holder = (ImageHolder)row.getTag();
    }

    Data d = data.get(position);
    holder.txtTitle.setText(d.getTitle());
    holder.txtLink.setText(d.getLink());
    holder.txtDate.setText(d.getDate());


   return row;

}

static class ImageHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    TextView txtTitle1;

}


}

then you create this:

ImageAdapter adapter1 = new TicketsImageAdapter(getActivity(),
                    R.layout.photo_list_adapter, data);

and you set your listview to this adapter:

dataList.setAdapter(adapter1);

Regarding NullPointerException: -first of all you've to initialize the vars inside your customAdapter constructor -the second thing is that you have to separate between listview layout and listItems so create new xml that contains listItems->tvTitle,tvLink,tvDate -then you have to make a condition that getView doesn't return View=null as i've did in the above code hope this will work for you. best of luck

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
  • Thanks, but I have already my external class with the data definition named whatsnew. But I can't make the custom adapter inside the OnPostExecute. Or simply I don't know, so I'ts time to read – user1423168 Apr 19 '15 at 10:07
  • if it sets title that means it is working inside onpost() but the problem is that you are just giving it title through this line: (getActivity(), android.R.layout.simple_list_item_1,title); so making a custom adapter will fix that. – Ahmad Sanie Apr 19 '15 at 10:16
  • Edited. I'm getting errors with the adapter...NullPointerException and can't see the error. Thanks – user1423168 Apr 19 '15 at 11:06
  • did you make your custom adapter as an inner class ? – Ahmad Sanie Apr 19 '15 at 11:50
  • because you have to make sure that you are setting your list view adapter inside the class that calls it and make your adapter as independent. regarding the null pointer exception check out my solution I've edited to show you why you are getting that exception – Ahmad Sanie Apr 19 '15 at 11:52
  • Yes, it's inside the OnPostExecute method from the AsyncTask. Thanks for the reply – user1423168 Apr 19 '15 at 11:53
  • WORKING! The problem is that my Array was an ArrayList and I had to put into the custom adapter List Thanks! – user1423168 Apr 19 '15 at 12:41
  • 1
    NP T.c and by the way you can kindly accept the answer so others would benefit from it. – Ahmad Sanie Apr 19 '15 at 12:44