-2

I'm developing my first app and I want to show a grid in which there is a list of entries. I get those entries through queries on local SQLite database so this it is a dynamic list. Every item of this list should have 2 field: a string and a value.

How to correctly do it in an activity? I see ListView but it doesn't seem to suit my needs and I do not need clickable items.

Can you suggest a better solution?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • I would use a ListView. They don't have to be clickable, you can customise the view to have 2 columns (or to be whatever you want) and they have great performance benefits with view recycling, etc during scrolling. – Ken Wolf Oct 03 '14 at 17:34
  • Can you show an example with ListView that have 2 columns? – smartmouse Oct 03 '14 at 17:36
  • http://www.technotalkative.com/android-multi-column-listview/ for example. It's just a custom view that you return as the row. – Ken Wolf Oct 03 '14 at 17:37
  • Great! It seems what i was looking for! Thank you. – smartmouse Oct 03 '14 at 17:39
  • I've added it as an answer for completeness. Once you get the hang of customising ListView you'll be using it all the time :) – Ken Wolf Oct 03 '14 at 17:41

2 Answers2

1

Create programatically or in XML LinearLayout with vertical orientation:

LinearLayout layout = findViewById(r.id.layout);
layout.setOrientation(LinearLayout.VERTICAL);

Then create Textview for every item with text consisting of string and value of item and add them to the layout:

for (int i=0; i<items.size; i++) {
    TextView tv = new TextView(context);
    tv.setText(items[i].stringName + ": " + items[i].value);
    layout.addView(tv);
}
Axel Stone
  • 1,521
  • 4
  • 23
  • 43
1

I would still use a ListView.

They don't have to be clickable, you can customise the view to have 2 columns (or to be whatever you want) and they have great performance benefits with view recycling, etc during scrolling.

Essentially you need to return a custom view as the row - this row view will have data aligned horizontally and so you can get columns.

Here is an example.

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • I'm encountering a problem: my MultiColumnActivity class is not an Activity, it is a Fragment. So if i write "public class StatTab1 extends Fragment" instead of "extend Activity" i get errors on setContentView, ListView and ListViewAdapter... – smartmouse Oct 03 '14 at 18:06
  • I think this is outside the scope of this question now. Clearly you'll need to modify any code you find to work for your fragment setup. In fragments, for example, you don't use `setContentView` but set everything up in `onCreateView` - however without seeing any of your code it's impossible for me to answer. – Ken Wolf Oct 03 '14 at 18:10