0

I am so new to android. I have a simple list and a custom raw to fill up the list. I also have a json file in assets folder. this is my xml file for the raw:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="vertical" 
 android:background="@drawable/bcgn">

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TableRow >
        <CheckBox
            android:id="@+id/FoodCheck"
            android:text="Celery"
            android:textColor="#696969"/>
        <TextView
            android:id="@+id/stCal"
            android:text="300"
            android:layout_marginLeft="150dip"
            android:textColor="#696969"/>
    </TableRow>

</TableLayout>


</RelativeLayout>

and this is my simple list:

<?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" 
 android:background="@drawable/bcgn">

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

 <TextView 
     android:id="@+id/txtHeader"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="Vegetables"
    android:textSize="15dip"
    android:textColor="#00008B"
    android:gravity="center"
    android:textAllCaps="true"
    android:background="#7FFFD4"
    />


<ListView android:id="@+id/list"
    android:drawSelectorOnTop="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:divider="#A9A9A9"
android:dividerHeight="2dip"
 android:addStatesFromChildren="true"/>
</TableLayout>
</LinearLayout>

and this is a short version of my Json file:

[{"name":"celery","cal":200},{"name":"carrot","cal":700},{"name":"lettuce","cal":500}]

Do i really need to convert Json to a hashmap? can i just use a list? Do i need to have the adapter on a separate java file? and if yes please provide the complete code and a brief explanation, because I am new and can not understand some parts. Thank you very much

oopscrash
  • 9
  • 1

1 Answers1

0

Do i really need to convert Json to a hashmap?

Yes you have to implement the JSON parsing logic, you can either prepare HashMap or ArrayList.

can i just use a list? Do i need to have the adapter on a separate java file? and if yes please provide the complete code and a brief explanation, because I am new and can not understand some parts.

It's not necessary to define adapter as a separate class but a good practice. Do Google on "how to define a custom adapter in android", there are plenty of resources available on web so I am sure you would get it.

I have written tutorials for both of the above topics, you can go through those tutorials on my blog!

P.S. I know I have pasted a link here but can't include those explanations and details here as it's lengthy code and steps!

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295