Android Fragment中的ListView

5

我正在做一份以Fragment为基础的学校项目。我已经制作了XML文件,但是在nearfragment.java中出现了错误。由于我熟悉Activity而不是Fragment,请告诉我错误出在哪里。请帮助我纠正错误。

near_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="{relativePackage}.${activityClass}" >

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

</RelativeLayout>

near_layout_mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon_list"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:padding="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:padding="2dp"
            android:textColor="#33CC33" />
        <TextView
            android:id="@+id/textView1_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

CustomListNearAdapter.java

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 CustomListNearAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] itemname;
    private final Integer[] imgid;

    public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) {
        super(context, R.layout.near_layout_mylist, itemname);
        // TODO Auto-generated constructor stub

        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.near_layout_mylist, null,true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list);
        TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list);

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

    };
}

NearFragment

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;

/**
 * Created by Ratan on 7/29/2015.
 */
public class NearFragment extends Fragment {



    ListView list;
    String[] itemname ={
            "Safari",
            "Camera",
            "Global",
            "FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"
    };

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = inflater.inflate(R.layout.near_layout, container,false);
        CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);

        ListView listView = (ListView) view.findViewById(R.id.list_item);
        listView.setAdapter(new CustomListNearAdapter(getActivity()));
        return view;


    }


}

错误出现在 nearfragment,我无法编写 Fragment 的代码。因此,请给我一些指导。


阿卡什迪普·辛格:出了什么错误?请发布日志记录。 - kevz
你为什么不使用“getActivity()”,而不是这个? - Ravindra Kushwaha
请发布您的LOGCAT。 - Vivek Bhardwaj
logcat不必要,因为错误在nearfragment代码中。只需要为该片段编写它。 - Akashdeep Singh
请按照我的答案操作,它可以解决问题。 - KVISH
显示剩余3条评论
4个回答

1
在你的代码中,在设置adapter时,你没有正确地向构造函数传递值,也就是说在你的代码中的那行代码。
listView.setAdapter(new CustomListNearAdapter(getActivity()));

在上面的代码行中存在问题,因此请将代码替换为下面的代码行。
        CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);

        ListView listView = (ListView) view.findViewById(R.id.list_li);
        listView.setAdapter(adapter);

错误已经被修复,但应用程序仍然崩溃。 - Akashdeep Singh
@AkashdeepSingh,你遇到了什么崩溃,请告诉我。 - Ravindra Kushwaha
@AkashdeepSingh,您遇到异常是因为在您的XML中使用了list_li,而在代码中您通过list_item这些名称查找listview的id。因此,我已经编辑了我的答案,请检查并在有疑虑的情况下告诉我。 - Ravindra Kushwaha
@KVISH...你给我的回答点了踩吗?如果除此之外还有其他错误,请告诉我。 - Ravindra Kushwaha
@AkashdeepSingh...很高兴能帮忙,兄弟 :) 请也接受它 :P - Ravindra Kushwaha
显示剩余3条评论

1
在下面的代码行中,使用getActivity()而不是this
CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);

并将适配器设置为以下列表视图。
listView.setAdapter(adapter);

1

我建议您在活动加载后编写ListView的代码。因此,请按以下方式编写您的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.near_layout, container, false);
    return view;
}

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

    CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);
    ListView listView = (ListView) view.findViewById(R.id.list_item);
    listView.setAdapter(new CustomListNearAdapter(getActivity()));
}

getActivity()可能会在这里引起问题,因为活动尚未创建。此外,您没有正确初始化适配器。您没有提供所有值。


1
将您的代码替换为以下内容。
 View view = inflater.inflate(R.layout.near_layout, container,false);

    ListView listView = (ListView) view.findViewById(R.id.list_item);
    listView.setAdapter(new CustomListNearAdapter(getActivity(), itemname, imgid));
    return view;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接