无法在ListView中看到TextView

时间:2013-11-27 23:51:02

标签: android listview android-listview android-fragments

我所拥有的一切都能正确显示我的列表视图,但它不会显示我的第二个TextView,它应该是listview的一部分。这可能是一个非常简单的错误,需要经验丰富的Android用户30秒才能发现。

这是我的代码:这是listView的一个项目

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>

    <TextView
        android:id="@+id/Title"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Choice"
        android:layout_width="50dp"
        android:layout_height="50dp" />

</TableRow>

这是包含listview的片段xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/listView1"
        android:layout_marginLeft="28dp"
        android:text="Handbells" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="Towerbells" />
</RelativeLayout>

这是我的适配器代码:

public class Top3CustomAdapter<String> extends ArrayAdapter<String> {

private ArrayList<String> entries;

public Top3CustomAdapter(Context context, int textViewResourceId){
    super(context, textViewResourceId);
}

public Top3CustomAdapter(Activity a, int textViewResourceId, ArrayList<String> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {

        LayoutInflater vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.listview_top_3, null);

    }

    TextView title = (TextView) v.findViewById(R.id.Title);
    TextView choice = (TextView) v.findViewById(R.id.Choice);

    title.setText(entries.get(0) + "");
    choice.setText(entries.get(1) + "");

    return v;
}

}

这是我的片段代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    // Inflate the layout for this fragment
    //This layout contains your list view 
        View view = inflater.inflate(R.layout.fragment_activity_start, parent, false);

       //now you must initialize your list view
       ListView listview =(ListView) view.findViewById(R.id.listView1);

       ArrayList<String> items = new ArrayList<String>();
       items.add(0,"Method");
       items.add(1,"Aberafan");

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 
       listview.setAdapter(adapter);  

        //To have custom list view use this : you must define CustomeAdapter class
        // listview.setadapter(new CustomeAdapter(getActivity()));
       //getActivty is used instead of Context
     return view;
}

1 个答案:

答案 0 :(得分:4)

在片段的onCreateView函数中,您有以下两行:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items); 
listview.setAdapter(adapter);  

这意味着列表视图使用标准ArrayAdapter,每行布置android.R.layout.simple_list_item_1。而不是您认为正在使用的自定义适配器和行布局。