so i recently started to get my hands dirty with android development and after some wrestling and failing to get this working i thought id run it by you guys and gals. i hope any of you has solved a similar problem
I have a fragment with a layout that is to display a textview and a list of items as follow:
Historical data
Item1 20
item2 30
item3 40
This includes a button on the action bar which will be implemented to allow the user to add more items and a number. I am having trouble achieving the above behavior. I was able to implement a adapter which ended up displaying as follow:
Historical data
Item1 20
Historical data
item2 30
Historical data
item3 40
Any ideas how could i do the above?
Myfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="?android:listSeparatorTextViewStyle"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/weight_history_label" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Weigh in Date" />
<TextView
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item"
android:inputType="numberDecimal"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:text="Weight Amount " />
</RelativeLayout>
</LinearLayout>
I have a class of items:
Items.java
public class NewEntry {
private String mName;
private int mNumber;
public NewEntry(){
....
}
getters/setters }
Then in my fragment.java
myFragment.java
public class MyFragment extends ListFragment {
private ArrayList<Items> mEntries;
MenuItem test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get all the entries and store them on mEntries
mEntries = EntriesLab.get(getActivity()).getWeighInEntries();
setHasOptionsMenu(true);
ItemAdapter adapter = new ItemAdapter(mEntries);
setListAdapter(adapter);
}
/interface to talk to activity
public interface onButtonClicked {
public void onWeighInButtonClicked(View v);
}
private class ItemAdapter extends ArrayAdapter<Items> {
public ItemAdapter(ArrayList<Items> items) {
super(getActivity(), 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.Myfragment, null);
}
Items c = getItem(position);
TextView titleDateView =
(TextView)convertView.findViewById(R.id.name);
titleDateView.setText(c.getDate().toString());
// Log.i("WeighInFragment: ", "###current Weight " + c.getDate().toString());
TextView titleWeightView =
(TextView)convertView.findViewById(R.id.witem);
titleWeightView.setText(""+c.getWeight());
return convertView;
}
}
}
so what i would like to do is implement my Label only once and then display the list of items! no matter what i try i always get a list of : label item value label item value label item value ...
The problem is that you are inflating the XML with the label for each of your list items. That's why you get the header each time. The XML that is inflated inside of getView is supposed to just be for your individual items, not the label above the list.
Try overriding onCreateView instead of onCreate in your fragment. In that function you can inflate an XML file that has your label and then the ListView. Then have a separate XML for the items, that has your RelativeLayout and its two TextView children.
EDIT:
Sorry, let me be more complete. In your onCreateView, inflate the full fragment's XML, which should include the ListView. Since you are using ListActivity, be sure the ListView's id is android:id/list. This should look something like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="?android:listSeparatorTextViewStyle"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/weight_history_label" />
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
Now create a separate list_item.xml that contains your RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Weigh in Date" />
<TextView
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/item"
android:inputType="numberDecimal"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
android:text="Weight Amount " />
</RelativeLayout>
Finally, your onCreateView is the place where you attach the adapter to your ListView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//get all the entries and store them on mEntries
mEntries = EntriesLab.get(getActivity()).getWeighInEntries();
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.my_fragment, container, true);
setListAdapter(new ItemAdapter(mEntries));
return rootView;
}