Mysterious
Mysterious

Reputation: 173

How to show Multiple Headings and Points in Single Activity

I am following this SO solution, and i have successfully parsed JSON, but don't know How to show both the headings and points in TextViews.

this is how my JSON looks:

{
    "technology": [
        {
            "title": "Android",
            "description": [
                {
                    "heading": "Manufactures",
                    "points": [
                        "Google",
                        "Samsung"
                    ]
                },
                {
                    "heading": "Platforms",
                    "points": [
                        "Kitkat",
                        "ICS"
                    ]
                }
            ]
        }
    ] 
}

For an example, if i do tap on Android, then i want to show something like this:

Manufactures
[dot] Google
[dot] Samsung

Platforms
[dot] Kitkat
[dot] ICS

For reference see this image:

enter image description here

In place of Why We Like It, i want to show Manufactures and in place of Need to Know, i want to show Platforms (with their respective points) as shown in Image.

But getting something like this:

Platforms
[dot] ICS

For reference see this image:

enter image description here

So what is the reason? and where i have to make changes in my code ?

DescriptionActivity.java:-

 for(int s=0; s<arrayListDescription.size(); s++)
    {
        description = arrayListDescription.get(s);
        strHeading = "&nbsp;" + description.getHeading();
        Log.d("heading::", strHeading);
        arrayListPoints = description.getPoints();      

        for(int z=0; z<arrayListPoints.size(); z++)
        {
            String dot = getString(R.string.dot);
            strPoints = "";
            strPoints += "&nbsp;&nbsp;" + dot + "&nbsp;" + arrayListPoints.get(z) + "\n";
            Log.d("points::", strPoints);
        }
        textViewPoints.setText(Html.fromHtml(strPoints));
    }       

    textViewHeading.setText(Html.fromHtml(strHeading));
    textViewHeading.setTextSize(20);

}

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

    <TextView
        android:id="@+id/textHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/textPoints"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

this is what my Log says:

08-28 04:47:39.653: D/heading::(1902): Manufactures
08-28 04:47:39.653: D/points::(1902): &emsp;&middot;Google
08-28 04:47:39.671: D/points::(1902): &emsp;&middot;Samsung
08-28 04:47:39.761: D/heading::(1902): Platforms
08-28 04:47:39.761: D/points::(1902): &emsp;&middot;Kitkat
08-28 04:47:39.761: D/points::(1902): &emsp;&middot;ICS

By using @Dreagen's solution, getting something like this:

Platforms
null[dot]Google[dot]Samsung[dot]Kitkat[dot]ICS

For reference see this image:

enter image description here

I used this code:

for(int z=0; z<arrayListPoints.size(); z++)
{
    String dot = getString(R.string.dot);
    // strPoints += dot + arrayListPoints.get(z) + "\n";
    strPoints += "&emsp;&middot;" + arrayListPoints.get(z) + System.getProperty("line.separator");
    Log.d("points::", strPoints);
}

Upvotes: 2

Views: 99

Answers (3)

KOTIOS
KOTIOS

Reputation: 11194

Activity

public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ArrayList<Model> mydata = new ArrayList<Model>();
            // set 1
            Model model = new Model();
            ArrayList<String> points = new ArrayList<String>();
            model.setHeading("Manufactures");
            points.add("Google");
            points.add("Samsung");
            model.setPoints(points);
            mydata.add(model);

            // set 2

            model = new Model();
            points = new ArrayList<String>();
            model.setHeading("Platforms");
            points.add("Kitkat");
            points.add("ICS");
            model.setPoints(points);
            mydata.add(model);

            // String str =
            // "<h4>An Unordered List:</h4><br/>&#8226; foo<br/>&#8226; bar<br/>&#8226; baz<br/>";

            TextView textview = (TextView) findViewById(R.id.myText);
            String result = "";
            for (int i = 0; i < mydata.size(); i++) {
                result += "<h4>" + mydata.get(i).getHeading() + "</h4>";
                ArrayList<String> appendPoints = new ArrayList<String>(mydata
                        .get(i).getPoints());
                for (int j = 0; j < appendPoints.size(); j++){
                    result += "&#8226; " + appendPoints.get(j) + "<br/>";}

            }

            Spanned spannedResult = Html.fromHtml(result);

            textview.setText(spannedResult);

        }

    }

Model

public class Model {

    private String heading;
    private ArrayList<String> points;

    public String getHeading() {
        return heading;
    }

    public void setHeading(String heading) {
        this.heading = heading;
    }

    public ArrayList<String> getPoints() {
        return points;
    }

    public void setPoints(ArrayList<String> points) {
        this.points = points;
    }

}

XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nestedtext.MainActivity" >

    <TextView
        android:id="@+id/myText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

COMPLETE SOURCE CODE

Upvotes: 1

Dreagen
Dreagen

Reputation: 1743

I think your problem is here

for(int z=0; z<arrayListPoints.size(); z++)
{
    strPoints = "&emsp;&middot;" + arrayListPoints.get(z) + "\n";        
    Log.d("points::", strPoints);
}

You are just overwriting strPoints each time you loop, which means that the at the end you only have the last point in there. The log shows both because you output to the log everytime you loop.

Change it to:

for(int z=0; z<arrayListPoints.size(); z++)
{
    strPoints += "&emsp;&middot;" + arrayListPoints.get(z) + System.getProperty("line.separator");
    Log.d("points::", strPoints);
}

note the += instead of just = and the change of \n to use System.getProperty("line.separator"); Hope this helps

Upvotes: 1

Marius
Marius

Reputation: 820

In addition to Dreagen's answer, you should change new line symbols to br tags. Other solution is using pre for preformatted text, though I'm not sure if Android supports that tag.

Upvotes: 0

Related Questions