使用适配器的ListView没有显示

时间:2018-01-02 04:17:06

标签: android listview

我正在尝试创建一个列表视图(List For Menu)。 因为我是新手,所以我正在尝试这个教程http://www.androidbegin.com/tutorial/android-custom-listview-texts-tutorial/。 ,这就是我做的事情

SettingFragment.java

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

        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        list = (ListView) view.findViewById(R.id.setting_menulist);

        String[] MenuName       = new String[] {
                                            "Profil" ,
                                            "Change Log",
                                            "Check For Update",
                                            "Sign Out"
                                            };
        String[] ActivityName   = new String[] {
                                            "ProfilActivity",
                                            "ChangelogActivity",
                                            "",
                                            ""};

        adapter = new SettingMenuAdapter( getActivity(),  MenuName , ActivityName );
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                alertDialog.setTitle("View To Load");
                alertDialog.setMessage("Here Is view to Load" + view);
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
        });
        return view;
    }

SettingMenuAdapter

public class SettingMenuAdapter extends BaseAdapter {

    Context context;
    String[] MenuName;
    String[] ActibityName;
    ListView list;
    LayoutInflater inflater;

    public SettingMenuAdapter(Context context, String[] MenuName, String[] ActivityName) {
        this.context = context;
        this.MenuName = MenuName;
    }

    @Override
    public int getCount() {
        return MenuName.length;
    }
    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.fragment_setting, parent, false);
        list = (ListView) itemView.findViewById(R.id.setting_menulist);
        return itemView;
    }
}

当我运行它时,我没有看到任何错误,但我的列表视图是空的。我错过了什么吗?

我已经修好了。创建名为settingitem的新xml,然后将我的适配器更改为此

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

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.settingitem, parent, false);

        MenuListName = (TextView) itemView.findViewById(R.id.menuname);
        MenuListName.setText(MenuName[position]);

        return itemView;
    }

1 个答案:

答案 0 :(得分:4)

您的适配器错误

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

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.fragment_setting, parent, false);
        list = (ListView) itemView.findViewById(R.id.setting_menulist);
        return itemView;
    }

这里你要替换listview.it必须是listview项目

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

        // Declare Variables
        TextView txtrank;
        TextView txtcountry;
        TextView txtpopulation;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);

        // Locate the TextViews in listview_item.xml
        txtrank = (TextView) itemView.findViewById(R.id.rank);
        txtcountry = (TextView) itemView.findViewById(R.id.country);
        txtpopulation = (TextView) itemView.findViewById(R.id.population);

        // Capture position and set to the TextViews
        txtrank.setText(rank[position]);
        txtcountry.setText(country[position]);
        txtpopulation.setText(population[position]);

        return itemView;
    }

你在片段onCreate和适配器getView

中正在膨胀相同的东西