在片段中实现列表视图

implementing listview in fragment

我正在尝试在 listView 中创建一个具有该行布局的项目列表,这些项目位于片段中,但每次我 运行 我的代码在我的列表视图中什么也得不到

rowlist_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_marginBottom="1dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/listIcon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/no_image"
        android:padding="5dp"/>
    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_margin="16dp"
        android:textSize="16sp"
        android:textStyle="bold"
        />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_marginLeft="10dp"/>
        </LinearLayout>
</LinearLayout>

fragment_display_lists.xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="layout.Display_lists"
    android:layout_marginTop="10dp">





        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>



</FrameLayout>

我的ListAdapter.java

package layout;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class ListAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] itemname;
    private final Integer[] imgid;

    public ListAdapter (Activity context,String[] itemname, Integer[] imgid){
        super(context, R.layout.fragment_display_lists,itemname);
        this.context=context;
        this.itemname=itemname;
        this.imgid=imgid;

    }
    public View getView (int position,View view,ViewGroup parent)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.rowlist_layout,null,true);
        TextView title = (TextView) rowView.findViewById(R.id.list_item);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.listIcon);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView);

        title.setText(itemname[position]);
        imageView.setImageResource(imgid[position]);
        extratxt.setText("Description "+itemname[position]);
        return rowView;

    };


    }

Display_lists.java

package layout;


import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;
import android.widget.Toast;




public class Display_lists extends Fragment{

    ListView list;
    String[] itemname ={
            "Item 1",
            "item 2",
            "item 3",
            "item 4",
            "item 5",

    };
    Integer[] imgid= {
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
            R.drawable.no_image,
    };


    private OnFragmentInteractionListener mListener;

    public Display_lists() {
        // Required empty public constructor

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        ListAdapter adapter=new ListAdapter(getActivity(),itemname, imgid);
        list=(ListView) getActivity().findViewById (R.id.list);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String Slecteditem= itemname[+position];
                Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

            }
        });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_display_lists, container, false);
    }



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

我在 android 帮助中还是新手:/

尝试像这样编辑您的 onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

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

    ListAdapter adapter = new ListAdapter(getActivity(),itemname, imgid);
    list = (ListView) rootView.findViewById (R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub
            String Slecteditem= itemname[position];
            Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();

        }
    });
    return rootView;
}

并从您的 onCreate

中删除 Listview Stuff

首先你的getView方法没有正确实现 见 this

其次,你在rowlist_layout.xml中定义的TextView id是textView1而不是textView,纠正一下

第三次在您的 getView 方法中放置断点并检查您的数组是否有值?

如果您仍然卡住,请参阅 this 并逐步执行