Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有自定义适配器的空片段列表_Java_Android_Android Layout_Android Fragments - Fatal编程技术网

Java 带有自定义适配器的空片段列表

Java 带有自定义适配器的空片段列表,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,首先,我试图用随机数据填充FrgamentList,但列表总是空的。 还尝试使用ListView切换到片段,但效果不佳。 这是我的项目代码: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_par

首先,我试图用随机数据填充
FrgamentList
,但列表总是空的。 还尝试使用
ListView
切换到片段,但效果不佳。 这是我的项目代码:

activity_main.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="vertical" >

    <fragment
        android:id="@+id/fragment1"
        android:name="as400samplecode.com.myapplication.MyListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
主要活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
MyListFragment:

    public class MyListFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        List<Location> locations = new ArrayList<>();
        locations.add(new Location("bla5", R.drawable.eiffel));
        locations.add(new Location("bla6", R.drawable.eiffel));

        CustomListAdapter adapter = new CustomListAdapter(getActivity(), locations);

        setListAdapter(adapter);
    }
}
公共类MyListFragment扩展ListFragment{
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.list\u碎片,容器,错误);
返回视图;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
列表位置=新的ArrayList();
位置。添加(新位置(“bla5”,R.drawable.eiffel));
位置。添加(新位置(“bla6”,R.drawable.eiffel));
CustomListAdapter=新的CustomListAdapter(getActivity(),位置);
setListAdapter(适配器);
}
}
CustomListAdapter:

    public class CustomListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private List<Location> locations;

    public CustomListAdapter(Activity context, List<Location> locations) {
        super(context, R.layout.fragment_item);
        this.context = context;
        this.locations = locations;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder; // to reference the child views for later actions

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.fragment_item, null);
            // cache view fields into the holder
            holder = new ViewHolder();
            holder.imageView = (ImageView) v.findViewById(R.id.placeImage);
            holder.txtTitle = (TextView) v.findViewById(R.id.placeName);

            holder.txtTitle.setText(locations.get(position).locName);
            //holder.imageView.setImageBitmap(locations.get(position).locImage);
            holder.imageView.setImageResource(locations.get(position).locImage);

            // associate the holder with the view for later lookup
            v.setTag(holder);
        }

        return v;
    }

    static class ViewHolder {
        TextView txtTitle;
        ImageView imageView;
    }
}
公共类CustomListAdapter扩展了ArrayAdapter{
私人最终活动背景;
私人名单地点;
公共CustomListAdapter(活动上下文、列表位置){
super(上下文、右布局、片段项);
this.context=上下文;
这个位置=位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
ViewHolder;//为以后的操作引用子视图
如果(v==null){
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.碎片\项目,空);
//将视图字段缓存到保持架中
holder=新的ViewHolder();
holder.imageView=(imageView)v.findViewById(R.id.placeImage);
holder.txtTitle=(TextView)v.findViewById(R.id.placeName);
holder.txtTitle.setText(locations.get(position.locName));
//holder.imageView.setImageBitmap(locations.get(position.locImage));
holder.imageView.setImageResource(locations.get(position.locImage));
//将支架与视图关联,以便以后查找
v、 setTag(支架);
}
返回v;
}
静态类视窗夹{
TextView-txtTitle;
图像视图图像视图;
}
}
知道会是什么吗


谢谢

问题在于这种方法

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder; // to reference the child views for later actions

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.fragment_item, null);
        // cache view fields into the holder
        holder = new ViewHolder();
        holder.imageView = (ImageView) v.findViewById(R.id.placeImage);
        holder.txtTitle = (TextView) v.findViewById(R.id.placeName);

        holder.txtTitle.setText(locations.get(position).locName);
        //holder.imageView.setImageBitmap(locations.get(position).locImage);
        holder.imageView.setImageResource(locations.get(position).locImage);

        // associate the holder with the view for later lookup
        v.setTag(holder);
    }
  // although you have added a ViewHolder it is not used once your view is generated
 // you are just instantiating convertView if it is null and not doing anything after that   

    return v;
}
当您的
convertView
不为空时,添加一个else条件并检索您添加的
ViewHolder
实例

else {
  holder = (ViewHolder) v.getTag();
}
 // now you would be feeding information to your `ImageView` and `TextView`

holder.txtTitle.setText(locations.get(position).locName);
holder.imageView.setImageBitmap(locations.get(position).locImage);

必须重写get
getCount()
方法,如下所示:

@Override
public int getCount(){
    return locations != null ? locations.size() : 0;
}

这没用。一些非常奇怪的事情发生了,当我调试时,我看到应用程序从未达到那个方法,这正常吗?不,这不正常,看看几个自定义listview的例子,自定义适配器是如何工作的,试着把日志放在你认为检查代码进度所必需的地方
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder; // to reference the child views for later actions

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.fragment_item, null);
        // cache view fields into the holder
        holder = new ViewHolder();
        holder.imageView = (ImageView) v.findViewById(R.id.placeImage);
        holder.txtTitle = (TextView) v.findViewById(R.id.placeName);

        holder.txtTitle.setText(locations.get(position).locName);
        //holder.imageView.setImageBitmap(locations.get(position).locImage);
        holder.imageView.setImageResource(locations.get(position).locImage);

        // associate the holder with the view for later lookup
        v.setTag(holder);
    }
  // although you have added a ViewHolder it is not used once your view is generated
 // you are just instantiating convertView if it is null and not doing anything after that   

    return v;
}
else {
  holder = (ViewHolder) v.getTag();
}
 // now you would be feeding information to your `ImageView` and `TextView`

holder.txtTitle.setText(locations.get(position).locName);
holder.imageView.setImageBitmap(locations.get(position).locImage);
@Override
public int getCount(){
    return locations != null ? locations.size() : 0;
}