java - 在 Fragment 中带有图像和文本的 Android ListView

标签 java android listview android-fragments navigation-drawer

我在 Android Studio 中创建了一个抽屉导航。在抽屉导航的 fragment 内我想添加一个带有图像和文本的 ListView。但是在我运行程序并单击 fragment 程序崩溃并显示文本“不幸的是,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);
}
}

最佳答案

了解 fragments改变你的代码,你可以使用 onCreateView,onViewCreated 和 onActivityCreated。onCreate 方法是使用初始化变量,对象或接收来自 Acivity 的值或对象。

@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;
}

关于java - 在 Fragment 中带有图像和文本的 Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164582/

相关文章:

android - Android 应用程序是否能够通过 USB 与桌面应用程序通信?

android - Android window.device 上的 PhoneGap 未定义

delphi - CancelEdit 是否适用于 Delphi 的 TListView 中的 TListItem?

android - ListView 项目 ListSelector 可绘制闪烁

android - 在自定义对话框中使用 Listview (android)

java - 如何标记文件中的字符串?

java - Android http 连接 OkHttp 不工作

java - RestTemplate 到 elasticsearch 6.7.0 搜索 -> 错误请求

android - 如何通过 JSON 显示从 DB 到 Android 的所有值

java - 32 位应用程序可以在 Eclipse 中运行,但部署后不能运行。 (没有错误..)