在片段上设置listview适配器

时间:2016-05-05 20:37:57

标签: android listview android-fragments android-arrayadapter adapter

你好我试着设置所以我有一个列表视图代表我的应用程序上的一个片段当前如果我试图运行我的应用程序将崩溃如果它在一个片段但是如果它在一个主活动它会起作用。我想知道是否有人能告诉我我必须更改为我的代码的最后一部分才能让它在片段上工作

下面的代码代表我的主要活动及其工作原理的XML

 public class MainActivity extends AppCompatActivity {

List <Wine> lstWine;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    createData();

    ListView lvWine = (ListView)this.findViewById(R.id.lvWines);

    WineAdapter adapter = new WineAdapter(this, R.layout.custom_listview, lstWine);
    lvWine.setAdapter(adapter);




}


public void createData(){

    lstWine = new ArrayList<Wine>();

    lstWine.add(new Wine(R.drawable.blush, "Blushshs", "Random", "MONEY"));
    lstWine.add(new Wine(R.drawable.blush, "Blush",  "Random", "String"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "fffffffffffffffffffffff", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "fffffffffffffffff", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine(R.drawable.blush, "Blush", "YEAR", "Random"));






}

上面的代码我试图将上面的代码转换为我可以在片段中应用它的列表视图。

  public class winelistfragment extends Fragment {

List<Wine_String> lstWine;
Inflater inflate;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    wineListData();

    ListView lvWine = (ListView)getActivity().findViewById(R.id.testlistview);

    WineAdapter adapter = new WineAdapter(getActivity(), R.layout.custom_winelist, lstWine);
    lvWine.setAdapter(adapter);



}

public void wineListData(){

    lstWine = new ArrayList<Wine_String>();

    lstWine.add(new Wine_String(R.drawable.blush, "Blushshs", "Random", "MONEY"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush",  "Random", "String"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "fffffffffffffffffffffff", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "fffffffffffffffff", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));
    lstWine.add(new Wine_String(R.drawable.blush, "Blush", "YEAR", "Random"));



}

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

你能告诉我我做错了什么以及如何改变片段代码吗?

2 个答案:

答案 0 :(得分:0)

您的代码应抛出空指针异常,因为您正尝试在Fragment的onCreate()方法中检索ListView(R.id.testlistview)的视图ID。这是因为onCreate()方法是在片段的onCreateView()方法之前调用的,这是你应该给视图充气的地方。由于视图在您创建片段时尚不存在,因此最终导致崩溃。在Activity中设置视图后,您正在正确地提取元素,但是您需要更改片段中的代码。

基本上,您需要在片段中的onCreateView()方法中为视图充气,否则您将无法查看(目前您的情况)。完成此操作后,您可以在Fragment的onViewCreated()方法中提取视图元素,然后设置值。有关Fragments生命周期的更多信息,我建议您浏览以下链接http://developer.android.com/guide/components/fragments.html。该链接向您展示如何实例化您的视图,然后提取它们。

答案 1 :(得分:0)

好的,我所做的是制作了一个列表片段,然后将这个列表片段添加到我需要的片段页面的xml中。

#include <iostream>

char map[2][10][20] =
{
    {
        "###################",
        "#@                #",
        "#                 #",
        "#                  ",
        "#                  ",
        "#                 #",
        "#                 #",
        "#                 #",
        "###################"
    }
,
    {
        "###################",
        "#  #############  #",
        "#     ##           ",
        "                   ",
        "                  #",
        "#                 #",
        "#              ####",
        "#           #######",
        "###################"
    }
};

void display_map(int level)
{
    std::cout << "level: " << (level + 1) << '\n';

    for(int y = 0; y < 9; ++y)
        std::cout << map[level][y] << '\n';
}

int main()
{
    // Display both maps:

    for(int level = 0; level < 2; ++level)
    {
        display_map(level);

        std::cout << '\n';
    }
}