在片段android中创建列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22512833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:05:58  来源:igfitidea点击:

Create listview in fragment android

androidlistviewandroid-fragments

提问by gamo

As the title I want to create a listview with custom row in Fragment. My code below.

作为标题,我想在 Fragment 中创建一个带有自定义行的列表视图。我的代码如下。

Fragment class

片段类

public class PhotosFragment extends Fragment{

public PhotosFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_photos, container, false);

    ArrayList<ListviewContactItem> listContact = GetlistContact();
    ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);
    lv.setAdapter(new ListviewContactAdapter(getActivity(), listContact));

    return rootView;
}

private ArrayList<ListviewContactItem> GetlistContact(){
    ArrayList<ListviewContactItem> contactlist = new ArrayList<ListviewContactItem>();

    ListviewContactItem contact = new ListviewContactItem();

    contact.SetName("Topher");
    contact.SetPhone("01213113568");
    contactlist.add(contact);

    contact = new ListviewContactItem();
    contact.SetName("Jean");
    contact.SetPhone("01213869102");
    contactlist.add(contact);

    contact = new ListviewContactItem();
    contact.SetName("Andrew");
    contact.SetPhone("01213123985");
    contactlist.add(contact);

    return contactlist; 
    }   
}

Adapter class

适配器类

public class ListviewContactAdapter extends BaseAdapter{
private static ArrayList<ListviewContactItem> listContact;

private LayoutInflater mInflater;

public ListviewContactAdapter(Context photosFragment, ArrayList<ListviewContactItem> results){
    listContact = results;
    mInflater = LayoutInflater.from(photosFragment);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listContact.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return listContact.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}


public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView == null){
        convertView = mInflater.inflate(R.layout.contact_item, null);
        holder = new ViewHolder();
        holder.txtname = (TextView) convertView.findViewById(R.id.lv_contact_item_name);          
        holder.txtphone = (TextView) convertView.findViewById(R.id.lv_contact_item_phone);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtname.setText(listContact.get(position).GetName());
    holder.txtphone.setText(listContact.get(position).GetPhone());

    return convertView;
}

static class ViewHolder{
    TextView txtname, txtphone;
}
}

But when I run the app that display no thing. Could anyone tell me what wrong here and how can I fix it?

但是当我运行没有显示任何东西的应用程序时。谁能告诉我这里出了什么问题,我该如何解决?

回答by Raghunandan

I guess your app crashes because of NullPointerException.

我猜你的应用程序崩溃是因为NullPointerException.

Change this

改变这个

ListView lv = (ListView)getActivity().findViewById(R.id.lv_contact);

to

ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);

assuming listview belongs to the fragment layout.

假设列表视图属于片段布局。

The rest of the code looks alright

其余的代码看起来没问题

Edit:

编辑:

Well since you said it is not working i tried it myself

好吧,既然你说它不起作用,我自己试过了

enter image description here

在此处输入图片说明

回答by Julisch

Please use ListFragment. Otherwise, it won't work.

请使用ListFragment。否则,它不会工作。

EDIT 1:Then you'll only need setListAdapter()and getListView().

编辑1:那么你只需要setListAdapter()getListView()

回答by Julisch

you need to give:

你需要给:

public void onActivityCreated(Bundle savedInstanceState)    
{
  super.onActivityCreated(savedInstanceState);
}

inside fragment.

里面的片段。

回答by IntelliJ Amiya

The inflate() method takes three parameters:

inflate() 方法接受三个参数:

  1. The id of a layout XML file (inside R.layout),
  2. A parent ViewGroup into which the fragment's View is to be inserted,

  3. A third boolean telling whether the fragment's View as inflated from the layout XML file should be inserted into the parent ViewGroup.

  1. 布局 XML 文件的 id(在 R.layout 中),
  2. 要插入片段视图的父视图组,

  3. 第三个布尔值,指示从布局 XML 文件膨胀的片段视图是否应插入父视图组。

In this case we pass false because the View will be attached to the parent ViewGroup elsewhere, by some of the Android code we call (in other words, behind our backs). When you pass false as last parameter to inflate(), the parent ViewGroup is still used for layout calculations of the inflated View, so you cannot pass null as parent ViewGroup .

在这种情况下,我们传递 false 是因为 View 将通过我们调用的一些 Android 代码(换句话说,在我们背后)附加到其他地方的父 ViewGroup。当您将 false 作为最后一个参数传递给 inflate() 时,父 ViewGroup 仍用于膨胀 View 的布局计算,因此您不能将 null 作为父 ViewGroup 传递。

 View rootView = inflater.inflate(R.layout.fragment_photos, container, false);

So, You need to call rootViewin here

所以,你需要rootView在这里打电话

ListView lv = (ListView)rootView.findViewById(R.id.lv_contact);

回答by VictorPurMar

Instead:

反而:

public class PhotosFragment extends Fragment

You can use:

您可以使用:

public class PhotosFragment extends ListFragment

It change the methods

它改变了方法

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<ListviewContactItem> listContact = GetlistContact();
        setAdapter(new ListviewContactAdapter(getActivity(), listContact));
    }

onActivityCreated is void and you didn't need to return a view like in onCreateView

onActivityCreated 是无效的,你不需要像 onCreateView 那样返回一个视图

You can see an example here

你可以在这里看到一个例子