0

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 ...

user3291939
  • 39
  • 1
  • 9

3 Answers3

0

You can use adapter.add method to add new items to your list, because you have set your list to use the adapter : setListAdapter(adapter).

So, you create a new instance of NewEntry, using its setter and add it with adapter.add everytime user click your button (onClick).

Finally, call adapter.notifyDataSetChanged to update your list data.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

I would like to do is implement my Label only once and then display the list of items!

For that you need to put the LABEL in mainLayout

So the layout should look like :

Main layout

<?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="match_parent"
    android:orientation="vertical" >

    <!-- Put Label here -->
    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/weight_history_label"
        android:textSize="18sp" />

    <!-- Your listview -->
    <ListView
        android:id="@+id/activity_main_menu_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="#ffffff"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/transparent" >
    </ListView>

</LinearLayout>

List row layout (list_row.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 android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_weight=".5"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:text="Weigh in Date" />

        <TextView
            android:id="@+id/amount"
            android:layout_width="wrap_content"
            android:layout_weight=".5"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:paddingTop="4dp"
            android:text="Weight Amount " />

</LinearLayout>

Then you need to inflate this layout in getView method :

if (convertView == null) {
       convertView = getActivity().getLayoutInflater()
                    .inflate(R.layout.list_row, null);
}
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

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;
}
Bruce
  • 2,377
  • 1
  • 17
  • 11
  • where would i call my adapter? – user3291939 Nov 13 '14 at 05:05
  • You just create the adapter like you did. Android will call getView to get each of your list's items, and now those items will just have the date and amount, and not the header. – Bruce Nov 13 '14 at 06:11
  • what happens if i have to separate list views which use different layouts within different activities? I believe the ID: – user3291939 Nov 19 '14 at 06:00
  • If you are talking about different activities, then it is fine to use the same view ID for the ListViews in each of those activities. The view ID is not magic; it is only used to find a view inside something else. So when you say activity.findViewById(android.R.list), it only looks inside that activity's views. You don't have to worry about views in other activities that are no on screen right then. – Bruce Nov 20 '14 at 07:13