2017-02-27 615 views
0

你好我新的android工作室,我可能犯了一個愚蠢的錯誤,我找不到。 因此,基本上我做了一個標籤式的活動,所有的日子,我想做一個清單(我想做一個自定義的時間表應用程序,將有我所做的每日活動)。我ListView不顯示:(。什麼有我做錯了Android studio listview不顯示

EditActivity類代碼:https://hastebin.com/amajusutid.java

package com.timemanagement.restrictedpower.timemanagement; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class EditActivity extends AppCompatActivity { 
    private SectionsPagerAdapter mSectionsPagerAdapter; 
    private ViewPager mViewPager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_edit, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static String day(int id){ 
     switch (id) { 
      case 1: 
       return "Monday"; 
      case 2: 
       return "Tuesday"; 
      case 3: 
       return "Wednesday"; 
      case 4: 
       return "Thirsday"; 
      case 5: 
       return "Friday"; 
      case 6: 
       return "Saturday"; 
      case 7: 
       return "Sunday"; 
     } 
     return null; 
    } 
    public static class PlaceholderFragment extends Fragment { 
     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public PlaceholderFragment() { 
     } 
     public static PlaceholderFragment newInstance(int sectionNumber) { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.fragment_edit, container, false); 

      ListView lv = (ListView) v.findViewById(R.id.Activities); 
      List<String> fruits_list = new ArrayList<String>(); 
      fruits_list.add("Apple"); 
      fruits_list.add("Strawberry"); 
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, fruits_list); 
      lv.setAdapter(arrayAdapter); 
      lv.invalidateViews(); 
      View rootView = inflater.inflate(R.layout.fragment_edit, container, false); 
      TextView textView = (TextView) rootView.findViewById(R.id.Day); 
      int id = getArguments().getInt(ARG_SECTION_NUMBER); 
      String day = day(getArguments().getInt(ARG_SECTION_NUMBER)); 
      textView.setText(day); 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 
      return rootView; 
     } 
    } 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      return PlaceholderFragment.newInstance(position + 1); 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 7; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "SECTION 1"; 
       case 1: 
        return "SECTION 2"; 
       case 2: 
        return "SECTION 3"; 
       case 3: 
        return "SECTION 4"; 
       case 4: 
        return "SECTION 5"; 
       case 5: 
        return "SECTION 6"; 
       case 6: 
        return "SECTION 7"; 
      } 
      return null; 
     } 
    } 
} 

XML代碼:https://hastebin.com/yapepideme.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#1234"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/Activities" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     style="@android:style/Widget.DeviceDefault.ExpandableListView" /> 
</LinearLayout> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_height="60dp" 
    android:layout_width="match_parent"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textSize="5mm" 
     android:id="@+id/Day" /> 

    <TextView 
     android:id="@+id/section_label" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:textColor="#FFCC91" 
     android:textSize="6mm" 
     android:gravity="center" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</android.support.design.widget.AppBarLayout> 

</android.support.design.widget.CoordinatorLayout> 
+0

我想你列表diplaying但你的ListView項的背景顏色和文字顏色相同。檢查這個。點擊或長按列表視圖 –

+1

爲什麼你要採取查看rootView = inflater.inflate(R.layout.fragment_edit,container,false); rootView?你已經有了這個視圖v = inflater.inflate(R.layout.fragment_edit,container,false); 所以只需返回v並刪除rootView –

回答

0

我已經解決了你的問題,你必須改變線性佈局的高度,因爲它是嗨dden當應用欄佈局應用。您的列表視圖應該放置在應用欄佈局下方。我在您的線性佈局中更改了android:layout_height =「300dp」, android:layout_marginTop =「100dp」。現在您的列表視圖出現了。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#1234"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_marginTop="100dp" 
    android:layout_weight="1"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/Activities" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     style="@android:style/Widget.DeviceDefault.ExpandableListView" /> 
</LinearLayout> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_height="60dp" 
    android:layout_width="match_parent"> 

    <TextView 
     android:text="TextView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textSize="5mm" 
     android:id="@+id/Day" /> 

    <TextView 
     android:id="@+id/section_label" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:textColor="#FFCC91" 
     android:textSize="6mm" 
     android:gravity="center" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</android.support.design.widget.AppBarLayout> 

+0

嗯,它仍然沒有顯示任何內容:/。另外,我確定我的線性佈局不會隱藏在應用程序欄後面,我可以在設計頁面中看到它。 – RestrictedPower