2017-04-16 87 views
-2

我在Android的初學者。在這裏,試圖列表視圖添加到我的項目中,片段用於導航的抽屜裏。因此,我堅持以下錯誤。下面是我的活動代碼:在片段使用上下文

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 

public class Menu1 extends Fragment { 
    private ArrayList<Property> rentalProperties = new ArrayList<>(); 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //returning our layout file 
     //change R.layout.yourlayoutfilename for each of your fragments 
     return inflater.inflate(R.layout.fragment_all, container, false); 
    } 


    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     //you can set the title for your toolbar here for different fragments different titles 
     getActivity().setTitle("All"); 
     //create property elements 

    } 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_all); 

     //create our property elements 
     rentalProperties.add(new Property(10, "Smith Street", "Sydney", "NSW", "A large 3 bedroom apartment right in the heart of Sydney! A rare find, with 3 bedrooms and a secured car park.", 450.00, "property_image_1", 3, 1, 1, false)); 
     rentalProperties.add(new Property(66, "King Street", "Sydney", "NSW", "A fully furnished studio apartment overlooking the harbour. Minutes from the CBD and next to transport, this is a perfect set-up for city living.", 320.00, "property_image_2", 1, 1, 1, false)); 
     rentalProperties.add(new Property(1, "Liverpool Road", "Liverpool", "NSW", "A standard 3 bedroom house in the suburbs. With room for several cars and right next to shops this is perfect for new families.", 360.00, "property_image_3", 3, 2, 2, true)); 
     rentalProperties.add(new Property(567, "Sunny Street", "Gold Coast", "QLD", "Come and see this amazing studio apartment in the heart of the gold coast, featuring stunning waterfront views.", 360.00, "property_image_4" , 1, 1, 1, false)); 

     //create our new array adapter 
     ArrayAdapter<Property> adapter = new propertyArrayAdapter(this, 0, rentalProperties); 

     //Find list view and bind it with the custom adapter 
     ListView listView = (ListView) findViewById(R.id.customListView); 
     listView.setAdapter(adapter); 


     //add event listener so we can handle clicks 
     AdapterView.OnItemClickListener adapterViewListener = new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Property property = rentalProperties.get(position); 

       Intent intent = new Intent(Menu1.this, DetailActivity.class); 
       intent.putExtra("streetNumber", property.getStreetNumber()); 
       intent.putExtra("streetName", property.getStreetName()); 
       intent.putExtra("suburb", property.getSuburb()); 
       intent.putExtra("state", property.getState()); 
       intent.putExtra("image", property.getImage()); 

       startActivityForResult(intent, 1000); 
      } 
     }; 
     //set the listener to the list view 
     listView.setOnItemClickListener(adapterViewListener); 


    } 


} 

沒有顯示出三個錯誤,它們分別是:

1)的setContentView(R.layout.fragment_all);

Error:(43, 9) error: cannot find symbol method setContentView(int) 

2)ListView listView =(ListView)findViewById(R.id.customListView);

Error:(55, 40) error: cannot find symbol method findViewById(int) 

3)ArrayAdapter適配器=新propertyArrayAdapter(此,0, rentalProperties); 4)Intent intent = new Intent(Menu1.this,DetailActivity.class); 4)Intent intent = new Intent(Menu1.txt,

Error:(67, 33) error: no suitable constructor found for 
    Intent(Menu1,Class<DetailActivity>) 
    constructor Intent.Intent(String,Uri) is not applicable 
    (argument mismatch; Menu1 cannot be converted to String) 
    constructor Intent.Intent(Context,Class<?>) is not applicable 
    (argument mismatch; Menu1 cannot be converted to Context) 

上面給出的問題通過下面給出的回答進行整流,標記爲回答。但是在這裏我遇到了佈局問題。佈局不適合屏幕,每邊都有一些空白區域。嘗試不同的佈局方法,但沒有改變。佈局代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="700dp" 
    android:layout_height="400dp" 
    android:orientation="vertical" 
    android:paddingTop="-10dp" 
    android:paddingBottom="10dp"> 

    <LinearLayout 
     android:id="@+id/infoSection" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:weightSum="1"> 

     <TextView 
      android:id="@+id/filename" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:text="Filename" 
      android:textSize="15dp" 
      android:layout_weight="2.35" /> 

     <ProgressBar 
      style="?android:attr/progressBarStyleHorizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/progressBar" 
      android:layout_weight="2.35" 
      android:layout_marginTop="-10dp"/> 

    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/pricingSection" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/infoSection" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <TextView 
        android:text="Status" 
        android:layout_width="172dp" 
        android:layout_height="wrap_content" 
        android:id="@+id/status" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:text="Size:" 
        android:layout_weight="1.07" /> 

       <TextView 
        android:text="sz" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/size" 
        android:layout_weight="1" /> 
      </LinearLayout> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

      </LinearLayout> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:layout_toRightOf="@id/size" 
        android:text="Progress:" 
        android:layout_weight="0.36" /> 

       <TextView 
        android:text="pg" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/progress" 
        android:layout_weight="1.65" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Speed:" 
        android:layout_marginLeft="25dp" 
        android:layout_toRightOf="@+id/progress" 
        android:layout_weight="0.87" /> 

       <TextView 
        android:text="sp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/speed" 
        android:layout_weight="1.86" /> 
      </LinearLayout> 
     </LinearLayout> 

    </RelativeLayout> 

</RelativeLayout> 
+1

http://stackoverflow.com/questions/21205732/converting-activity-into-fragment,http://stackoverflow.com/questions/35785049/converting-an-activity-to-a-fragment,http:/ /stackoverflow.com/questions/24604802/how-to-change-activity-to-fragment,http://stackoverflow.com/questions/8215308/using-context-in-a-fragment –

+0

in fragment'onCreateView' return view必須在'Fragment'中顯示,不要在'Fragment'中使用'setContentView' –

+0

請閱讀[在什麼情況下我可以在我的問題中添加「urgent」或其他類似的短語,以獲得更快的答案?]( https://meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者的理想方式,並且可能會對獲得答案起反作用。請不要將這添加到您的問題。 – halfer

回答

0

1)的setContentView(R.layout.fragment_all);

對於fragment,您必須對onCreateView()中的視圖進行充氣。不要使用setContentView(R.layout.fragment_all);fragment;

2)ListView listView =(ListView)findViewById(R.id.customListView);

你必須得到的意見,在片段父母的孩子。在下面的代碼rootView是父所以我發起ListView爲:

ListView listView = (ListView) rootView.findViewById(R.id.customListView); 

3)ArrayAdapter適配器=新propertyArrayAdapter(此,0, rentalProperties);

你必須得到的activity持有片段的背景:

ArrayAdapter adapter = new propertyArrayAdapter(getActivity(), 0, rentalProperties); 

4)意向意圖=新意圖(Menu1.this,DetailActivity.class);

替換爲這樣:

Intent intent = new Intent(getActivity(), DetailActivity.class); 

最後把所有的代碼從如下onCreate()onCreateView()

@Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //returning our layout file 
     //change R.layout.yourlayoutfilename for each of your fragments 
     View rootView = inflater.inflate(R.layout.fragment_all, container, false); 
     rentalProperties.add(new Property(10, "Smith Street", "Sydney", "NSW", "A large 3 bedroom apartment right in the heart of Sydney! A rare find, with 3 bedrooms and a secured car park.", 450.00, "property_image_1", 3, 1, 1, false)); 
     rentalProperties.add(new Property(66, "King Street", "Sydney", "NSW", "A fully furnished studio apartment overlooking the harbour. Minutes from the CBD and next to transport, this is a perfect set-up for city living.", 320.00, "property_image_2", 1, 1, 1, false)); 
     rentalProperties.add(new Property(1, "Liverpool Road", "Liverpool", "NSW", "A standard 3 bedroom house in the suburbs. With room for several cars and right next to shops this is perfect for new families.", 360.00, "property_image_3", 3, 2, 2, true)); 
     rentalProperties.add(new Property(567, "Sunny Street", "Gold Coast", "QLD", "Come and see this amazing studio apartment in the heart of the gold coast, featuring stunning waterfront views.", 360.00, "property_image_4" , 1, 1, 1, false)); 

     //create our new array adapter 
     ArrayAdapter<Property> adapter = new propertyArrayAdapter(getActivity(), 0, rentalProperties); 

     //Find list view and bind it with the custom adapter 
     ListView listView = (ListView)rootView. findViewById(R.id.customListView); 
     listView.setAdapter(adapter); 


     //add event listener so we can handle clicks 
     AdapterView.OnItemClickListener adapterViewListener = new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       Property property = rentalProperties.get(position); 

       Intent intent = new Intent(getActivity(), DetailActivity.class); 
       intent.putExtra("streetNumber", property.getStreetNumber()); 
       intent.putExtra("streetName", property.getStreetName()); 
       intent.putExtra("suburb", property.getSuburb()); 
       intent.putExtra("state", property.getState()); 
       intent.putExtra("image", property.getImage()); 

       startActivityForResult(intent, 1000); 
      } 
     }; 
     //set the listener to the list view 
     listView.setOnItemClickListener(adapterViewListener); 
     return rootView; 
    } 

希望這有助於。

+0

非常感謝你......它工作... – faheemKurikkal

-1

1)在它與返回inflater.inflate(R.layout.fragment_all,容器,假)代替所述片段setContentView(R.layout.fragment_all);;

2)ListView listView = (ListView) findViewById(R.id.customListView); 將其替換爲:ListView listView = (ListView) getActivity().findViewById(R.id.customListView);

3)ArrayAdapter adapter = new propertyArrayAdapter(this, 0, rentalProperties);ArrayAdapter adapter = new propertyArrayAdapter(getActivity(), 0, rentalProperties); // getActivity() it's ur context

4)Intent intent = new Intent(Menu1.this, DetailActivity.class); 取代它與4更換)Intent intent = new Intent(getActivity(), DetailActivity.class);

希望能夠幫助:)

相關問題