3

我试图构建一个带有 4 个选项卡的应用程序。每个选项卡都有一个不同的片段链接到它。问题是我想为每个片段制作一个自定义 listView,但它以一些无法解决的错误结束......我已经与其他开发人员交谈过,但我仍然无法制作一个有效的!真是令人沮丧!

我有:

  • 一个有效的 MainActivity 类,它使用可滑动的选项卡
  • 在我的自定义 ListView 上具有我想要的设计的 XML。
  • 带有 ListView 的名为 fragment1 的 XML。

这些是我得到的正常错误:

  • “Fragment1UG 类型的方法 findViewById(int) 未定义”
  • “Fragment1UG 类型的方法 setContentView(int) 未定义。提供 1 个快速修复:创建方法 'setContentView()'”

我试图理解和使用的指南之一:

这是我的第一个片段:

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.ListView;

public class Fragment1test extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            //This layout contains your list view 
                View view = inflater.inflate(R.layout.fragment1, container, false);

               //now you must initialize your list view
                ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
                ListView.setAdapter(new ListAdapter());
              return view;
    }
}

我的 ListAdapter.java 代码(来自教程):

import java.util.List;

import android.content.ClipData.Item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<Item> {

public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
    // TODO Auto-generated constructor stub
}

private List<Item> items;

public ListAdapter(Context context, int resource, List<Item> items) {

    super(context, resource, items);

    this.items = items;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.list_design, null);

    }

    Item p = items.get(position);

    if (p != null) {

        TextView tt = (TextView) v.findViewById(R.id.game_txtTitle);
        TextView tt1 = (TextView) v.findViewById(R.id.game_txtRelease);
        TextView tt3 = (TextView) v.findViewById(R.id.game_txtPlatform);

        if (tt != null) {
            tt.setText(p.getId());
        }
        if (tt1 != null) {

            tt1.setText(p.getCategory().getId());
        }
        if (tt3 != null) {

            tt3.setText(p.getDescription());
        }
    }
4

3 回答 3

12
public class Fragment1test extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //This layout contains your list view 
            View view = inflater.inflate(R.layout.fragment_basic, container, false);

           //now you must initialize your list view
           ListView listview =(ListView)view.findViewById(R.id.your_listview);

           //EDITED Code 
           String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
           ArrayAdapter<String> adapter =
           new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 

            listview.setAdapter(adapter);  

            //To have custom list view use this : you must define CustomeAdapter class
            // listview.setadapter(new CustomeAdapter(getActivity()));
           //getActivty is used instead of Context
         return view;
    }
  }

请参阅此链接问题以了解如何创建自定义适配器

注意:不要使用列表片段或列表活动来创建自定义列表视图

编辑

 ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter);
于 2013-06-29T15:16:59.663 回答
1
public class fragmentpassword extends Fragment {

        String[] name={"A","B","C","D"};
        int [] image={R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer};

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

            View v = inflater.inflate(R.layout.layoutpassword, container, false);

            ListView li=(ListView)v.findViewById(R.id.listViewPassword);
            li.setAdapter(new PasswordAdapter(getActivity(),R.layout.passwordlay,name));
            return v;
        }

        class PasswordAdapter extends ArrayAdapter {

            public PasswordAdapter(Context context, int resource, String[] objects) {
                super(context, resource, objects);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v=((Activity)getContext()).getLayoutInflater().inflate(R.layout.passwordlay,null);
                TextView txt1 = (TextView) v.findViewById(R.id.textViewpasslay);
                  txt1.setText(name[position]);
                ImageView img = (ImageView) v.findViewById(R.id.imageViewpasslay);
                img.setBackgroundResource(image[position]);

                return v;
            }
        }
    }


    **XML for customlist: passwordlay.xml**

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageViewpasslay" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textViewpasslay"/>
    </LinearLayout>

    **XML for fragment:  layoutpassword.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">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listViewPassword"/>
    </LinearLayout>
于 2015-07-25T07:47:59.247 回答
0

片段不使用setContentView(int),所以你不能使用它,使用inflaterinpublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)并返回膨胀的视图。至于findViewById(int)这样做getActivity().findViewById(int)

于 2013-06-29T02:36:23.323 回答