【问题标题】:Android ListView with images and text in FragmentAndroid ListView 与片段中的图像和文本
【发布时间】:2016-05-11 13:45:21
【问题描述】:

我在 Android Studio 中创建了一个导航抽屉。在导航抽屉的片段中,我想添加一个带有图像和文本的 ListView。但是在我运行程序并单击片段程序崩溃并显示文本“不幸的是,SerialMania 已停止”之后。我做错了什么?

package boiko.taisiia.com.serialmania;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import android.widget.SimpleAdapter;

public class Date extends Fragment {

// Array of strings storing country names
String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
};

// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera,
        R.drawable.ic_menu_camera
};

// Array of strings to store currencies
String[] currency = new String[]{
        "Indian Rupee",
        "Pakistani Rupee",
        "Sri Lankan Rupee",
        "Renminbi",
        "Bangladeshi Taka",
        "Nepalese Rupee",
        "Afghani",
        "North Korean Won",
        "South Korean Won",
        "Japanese Yen"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().setContentView(R.layout.fragment_date);

    // Each row in the list stores country name, currency and flag
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", "Country : " + countries[i]);
        hm.put("cur","Currency : " + currency[i]);
        hm.put("flag", Integer.toString(flags[i]) );
        aList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag,R.id.txt,R.id.cur};

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);

    // Getting a reference to listview of main.xml layout file
    ListView listView = ( ListView ) getActivity().findViewById(R.id.listview);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);
}
}

【问题讨论】:

  • 需要重写Fragment类的onCreateView方法来设置当前Fragment的布局。目前通过使用getActivity().setContentView(R.layout.fragment_date);,您正在更改 Activity 的布局而不是 Fragment。请参阅Creating a Fragment 了解如何设置 Fragment 的布局。
  • 请发布您的堆栈跟踪。
  • 你没有遵循片段生命周期。

标签: java android listview android-fragments navigation-drawer


【解决方案1】:

了解fragments 更改您的代码,您可以使用 onCreateView、onViewCreated 和 onActivityCreated.onCreate 方法是使用初始化变量、对象或接收来自即将到来的活动的值或对象。

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

    tvTitle = (TextView) v.findViewById(R.id.tvTitle);
    ivLogo = (ImageView) v.findViewById(R.id.ivLogo);

    // Each row in the list stores country name, currency and flag 
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

    for(int i=0;i<10;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", "Country : " + countries[i]);
        hm.put("cur","Currency : " + currency[i]);
        hm.put("flag", Integer.toString(flags[i]) );
        aList.add(hm);
    } 

    // Keys used in Hashmap 
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout 
    int[] to = { R.id.flag,R.id.txt,R.id.cur};

    // Instantiating an adapter to store each items 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(v.getContext(), aList, R.layout.listview_layout, from, to);

    // Getting a reference to listview of main.xml layout file 
    ListView listView = ( ListView ) v.findViewById(R.id.listview);

    // Setting the adapter to the listView 
    listView.setAdapter(adapter);

    return v;
}

【讨论】:

    【解决方案2】:

    onCreate中删除此代码:

    getActivity().setContentView(R.layout.fragment_date);
    

    并添加此方法:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_date, container, false);
    }
    

    UPD

    这是您修改后的代码:

    package boiko.taisiia.com.serialmania;
    
    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    
    import android.widget.SimpleAdapter;
    
    public class Date extends Fragment {
    
        // Array of strings storing country names
        String[] countries = new String[] {
                "India",
                "Pakistan",
                "Sri Lanka",
                "China",
                "Bangladesh",
                "Nepal",
                "Afghanistan",
                "North Korea",
                "South Korea",
                "Japan"
        };
    
        // Array of integers points to images stored in /res/drawable-ldpi/
        int[] flags = new int[]{
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera,
                R.drawable.ic_menu_camera
        };
    
        // Array of strings to store currencies
        String[] currency = new String[]{
                "Indian Rupee",
                "Pakistani Rupee",
                "Sri Lankan Rupee",
                "Renminbi",
                "Bangladeshi Taka",
                "Nepalese Rupee",
                "Afghani",
                "North Korean Won",
                "South Korean Won",
                "Japanese Yen"
        };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);    
    
            // Each row in the list stores country name, currency and flag
            List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
    
            for(int i=0;i<10;i++){
                HashMap<String, String> hm = new HashMap<String,String>();
                hm.put("txt", "Country : " + countries[i]);
                hm.put("cur","Currency : " + currency[i]);
                hm.put("flag", Integer.toString(flags[i]) );
                aList.add(hm);
            }
    
            // Keys used in Hashmap
            String[] from = { "flag","txt","cur" };
    
            // Ids of views in listview_layout
            int[] to = { R.id.flag,R.id.txt,R.id.cur};
    
            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);
    
            // Getting a reference to listview of main.xml layout file
            ListView listView = ( ListView ) getActivity().findViewById(R.id.listview);
    
            // Setting the adapter to the listView
            listView.setAdapter(adapter);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_date, container, false);
        }
    }
    

    【讨论】:

    • @TaisiiaBoiko 从代码中删除该行logger.v
    • 你没有遵循片段生命周期。
    • 不幸的是,它又被压碎了
    【解决方案3】:

    解决方案

    这不是在片段中设置内容的好方法!

    代码

    public final class YourFragment extends Fragment{
    
    private TextView tvTitle;
    
    private ImageView ivLogo;
    
    
    /**
     * default and empty constructor
     * The constructor should stay empty
     */
    public YourFragment()
    {
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.your_layout, container, false);
    
        this.tvTitle = (TextView) v.findViewById(R.id.tvTitle);
        this.ivLogo = (ImageView) v.findViewById(R.id.ivLogo);
        return v;
    }
    

    // ...片段的其余部分

    它应该做的工作!

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多