带填充的 ListView OnClick

ListView OnClick with filling

我在从 List 中填充 ListView 时不知所措。我可以用一个元素填充它,但我想从 object 中获取三个元素,因此当我触摸每个元素时,应用程序会转到它包含的 link。在我的实际代码中,我只能显示标题

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

arraylistdatelink中,我保存了正确的数据,但我只能使用其中一个。我的意图是将日期放在标题下方,当您触摸每个元素时,浏览器将打开并选中 link。

非常感谢。

编辑:我已经完成了自定义适配器,但它给了我一个错误。

 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();

我的 Logcat 说:

Java NullPointerException: storage == null

解决方案是在 public CustomAdapter 方法中将 "whatsnew[] data" 更改为 "List data",因为我将 ArrayList 中的所有数据都命名为 data.

不要使用字符串列表,更好的解决方案是使用独立的 class 来设置和获取这三个变量,我们将其命名为 class(例如)数据:

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

然后在列表中使用数据类型的对象:

List <Data> data = new ArrayList<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;

}


}

然后你创建这个:

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

并且您将列表视图设置为此适配器:

dataList.setAdapter(adapter1);

关于空指针异常: -首先你必须在你的 customAdapter 构造函数中初始化变量 -第二件事是你必须在 listview 布局和 listItems 之间分开,所以创建新的 xml 包含 listItems->tvTitle,tvLink,tvDate - 然后你必须像我在上面的代码中所做的那样设置一个条件,即 getView 不 return View=null 希望这对你有用。 祝你好运