Reputation: 1491
Everything I have displays my listview properly but it will not show my second TextView which should be part of the listview. This will probably be a really simple mistake and take an experienced android user 30 seconds to spot.
Here is my code: This is one item of a 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>
This is my fragment xml containing the listview:
<?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>
This is my adapter code:
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;
}
}
This is my fragment code:
@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;
}
Upvotes: 1
Views: 735
Reputation: 3248
In your onCreateView
function for your fragment, you have these two lines:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
this means that the list view is using a standard ArrayAdapter
with a android.R.layout.simple_list_item_1
layout for each row. Instead of the custom adapter and row layout that you thought you were using.
Upvotes: 4