2016-03-15 84 views
-4

你好,我是在填充上片段 以下列表视图有问题是我的fragment.class移植ListView在片段

package com.example.fred.p; 


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


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class MainFragment extends Fragment { 


    public MainFragment() { 
     // Required empty public constructor 
    } 



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

     String[] game = { "Android", "iOS", "window" }; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView(), getContext() , android.R.layout.p_g, game); 
     getListView(), setAdapter(adapter); 

     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_main, container, false); 
    } 

} 

碎片的xml文件如下。我想填充只有三个 项目的视图可以有人请帮助我这是我写的代码 谢谢。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.fred.p.MainFragment"> 




    <GridLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:rowCount="2" 
     android:columnCount="1" 
     android:orientation="vertical" 
     android:id="@+id/home" 
     android:paddingBottom="8dp"> 


     <ImageView 
      android:id="@+id/pd" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/p_adds" 
      android:scaleType="centerCrop"/> 


    </GridLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="8dp" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 

    <ListView 
     android:id="@+id/p_g" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary"> 



    </ListView> 


    </LinearLayout> 



</LinearLayout> 
+0

发布堆栈跟踪或有任何编译错误 – DJphy

+0

错误是它无法找到getListView和p_g(这是xml文件中的列表视图的ID)@DJphy – user3551487

+0

创建方法getListView() – DJphy

回答

0

尝试这个...

@Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View view=inflater.inflate(R.layout.fragment_main, container, false); 
      ListView lv=(ListView)view.findViewById(R.id.p_g); 
      String[] game = { "Android", "iOS", "window" }; 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, game); 
      lv.setAdapter(adapter); 


      return view 
     } 
+0

我得到错误,它无法找到getListView()和p_g(这是列表视图的ID)。 – user3551487

+0

检查我编辑的代码 –

+0

谢谢,但什么是[email protected] Rawal – user3551487

0

第一件事改变阵列适配器声明如下:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.p_g, game); 

第二件事,你有扩展片段,因此你不能用getListView ()方法, 至于你需要扩展ListActivity。

所以最好拿一个ListView对象并在这里指定它。

声明,如:

ListView lstData; 

然后在viewCreated方法初始化像lstData:

lstData = (ListView) view.findViewById(R.id.p_g); 

再经过阵列适配器声明一套适配器如下:

lstData.setAdapter(adapter); 

希望这将有助于您。

+0

你好@Vickyexpert它返回一个空 – user3551487

+0

声明ArrayAdapter正确如下 – Vickyexpert

+0

ArrayAdapter adapter = new ArrayAdapter (getActivity(), android.R.layout.simple_list_item_1,android.R.id.text1,game); – Vickyexpert

0

将这个列表视图放在你想要显示的地方;例如:你想显示列表视图activity_main_layout;我会在这里展示;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ListView 
    android:id="@+id/myListView" 
    android:layout_width="150dp" //u can have ur own width 
    android:layout_height="wrap_content"> 
</ListView> 
</RelativeLayout> 

然后在列表视图中为您的行定义布局;我将展示如何将列表视图排在texview(文件名:row_layout.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="48dp"> 
    <TextView 
    android:id="@+id/rowTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="saggsg" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:textColor="@color/colorAccent" /> 
</RelativeLayout> 

在您的主要活动中的OnCreate做到这一点:

String[] game = new String[] { "Android", "iOS", "window" };  
    ArrayList<String> gameList = new ArrayList<String>(); 
    gameList.addAll(Arrays.asList(game)); 
    ArrayAdapter myAdapter = new ArrayAdapter<String>(this,R.layout.row_layout,R.id.rowTextView, gameList); 
ListView myListView = (ListView) findViewById(R.id.myListView); 
     myListView .setAdapter(myAdapter); 

//你甚至可以添加一个onItemClickListener()到这个listview;享受

+0

谢谢,但listItem没有定义@Djphy – user3551487

+0

你的列表项目是android,ios和窗口 – DJphy

+0

现在检查它必须是listview ..做什么,我们没有IDE在这里检查我的变量.. haha​​haa – DJphy