在片段中实现listview

时间:2016-08-05 14:00:24

标签: listview android-listfragment

我正在尝试在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帮助中的新手:/

2 个答案:

答案 0 :(得分:3)

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

第二,你在rowlist_layout.xml中定义的TextView id是textView1而不是textView,更正

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

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

答案 1 :(得分:1)

尝试编辑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资料