Android:在片段

时间:2015-05-08 07:33:26

标签: android android-listview android-listfragment

我有一个现有的片段,我想在其中显示典型的地址类型的信息,如街道,城市,PIN和电话号码列表。

要显示电话号码列表,我在addressfragment.xml中添加了listview:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <TextView
            android:id="@+id/txtStreetAddr"
            style="?android:textAppearanceMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:text="default" />

        <ListView
            android:id="@+id/phones"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:paddingLeft="0dp" >
        </ListView>
        </LinearLayout>
</ScrollView>

我的片段类:

        public class AddressFragment extends Fragment {
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_screen_slide_page, container, false);

                ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                        "123, Corner Street");

                ListView phoneListView = ((ListView) rootView.findViewById(R.id.phones));

                ArrayList<String> phNumbers = new ArrayList<String>();
                phNumbers.add("4343534343");
                phNumbers.add("6767566766");

                ArrayAdapter<String> arrayAdapter =      
                new ArrayAdapter<String>(MyApp.getAppContext(),android.R.layout.simple_list_item_1, phNumbers);
                // Set The Adapter
                phoneListView.setAdapter(arrayAdapter); 
    return rootView;
        }

电话列表视图没有显示在屏幕上 - 为什么? 有没有更好的方法来解决这个问题?

3 个答案:

答案 0 :(得分:3)

我想你忘了在View

中夸大onCreateView(...)
View rootView= inflater.inflate(R.layout.your_layout, container, false);

添加您的onCreateView(...)

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

         View rootView= inflater.inflate(R.layout.your_layout, container, false);

        ((TextView) rootView.findViewById(R.id.txtStreetAddr)).setText(
                "123, Corner Street");

        ListView phoneListView =((ListView) rootView.findViewById(R.id.phones);

        ArrayList<String> phNumbers = new ArrayList<String>();
        phNumbers.add("4343534343");
        phNumbers.add("6767566766");

        ArrayAdapter<String> arrayAdapter =      
        new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, phNumbers);
        // Set The Adapter
        phoneListView.setAdapter(arrayAdapter);

     return rootView;
  }

答案 1 :(得分:1)

删除ScrollView并将LinearLayout作为父布局。

答案 2 :(得分:0)

if you've scrollview as parent view then, you've to set listview height programmatically, you can create a method, like below---

class ErgbnisAusFortran
{
public:
   ErgbnisAusFortran();
private:
   int Var_a;
   int Var_b ;  
public: 
   int getVar_a() const { return Var_a; }
   int getVar_res() const { return Var_res; }

   void setVar_res(int input)      {Var_res = input;}
   void setVar_a(int input)        {Var_a = input;}

   ErgbnisAusFortran calculateResults(EingabeWerte Ein);    
};

then pass listview to this method like below --

select table1.xyz,table2.abc from table1 join table2 on table1.pqr=table2.pqr 

Hope this will work perfectly.