Android - how do I display a list of items in an HTML-like Ordered list?
Still new to Android
开发者_开发百科I want to display some data as an HTML-like ordered list (non-actionable).
Example;
- Item One
- Item Two
- Item Three
I feel I am missing something obvious.
Thanks, JD
Either use a ListView and do nothing when something is selected, Use an AlertDialog.Builder and call setItems and do nothing in the Select handler, or use a WebView and create the list in html as on ordered list.
There's no default view or widget to do this i guess. You can try with ListView or doing the views programatically (however this is a little dirty)
The xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMain"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
The Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Get your main layout from the XML
LinearLayout llMain = (LinearLayout) findViewById(R.id.llMain);
ArrayList<String> alItems = new ArrayList<String>();
//Put some example data
alItems.add("Text1");
alItems.add("Text2");
alItems.add("Text3");
for(int i=0; i<alItems.size(); i++){
//We create a Layout for every item
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
//A TextView to put the order (ie: 1.)
TextView tv1 = new TextView(this);
tv1.setText(i+1 + ". ");
ll.addView(tv1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0));
//TextView to put the value from the ArrayList
TextView tv2 = new TextView(this);
tv2.setText(alItems.get(i));
ll.addView(tv2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
//Add this layout to the main layout of the XML
llMain.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0));
}
}
精彩评论