2016-03-15 36 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