How to show a list of items with 2 fields? - android

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?

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.

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);
}

Related

DataGrid Android Studio horizontal vertical scroll

I am looking for solution where could dispay large data with horizontal and vetical scroll. Something like this https://www.syncfusion.com/blogs/post/new-datagrid-for-xamarinios-and-xamarinandroid.aspx. I would like to change number of columns and change the cell values.
Do you have please something like this? When it be a free i will be really happy. Thank you
You can create it by custom TableLayout. And the good news is you can add both side scrolling mechanism in TableLayout. You have to first set a TableLayout xml. For both side scrolling you have to add first ScrollView then HorizontalScrlollView then your TableLayout.
Now you you can create row and column. You can create it dynamically because you (may be) dont know how many column has required. So you have to create custom TableRow and cell and you can easily add your tableLayout .
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// then create your text view and add it row. and last add row in your table layout
This is just example. You can create your own data grid library
And you can some ready mate library to show data grid in table view like your reference. :
https://github.com/evrencoskun/TableView
https://github.com/ISchwarz23/SortableTableView

How to dynamic insert elements of a string array into textview and position it?

I get the list of elements from the HTTP response, then I want to dynamically insert that list into the textview inside the "box" that you can see, currently it just inserts a string and overlaps them one over the other. I tried changing the layout (all three constraint, relative and linear) and it didn't help. Does anyone know how to position them dynamicly inside the boxes and not overlap but have margins like in the second picture? Otherwise, inside the project, I use a constrain layout.
Here is my code:
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.layout);
int size = response.toArray().length;
final TextView[] tv = new TextView[size];
TextView temp;
for (int i = 0; i < size; i++)
{
temp = new TextView(Activity.this);
temp.setText(response.get(i).getName());
parentLayout.addView(temp);
tv[i] = temp;
}
Here is the picture how it looks right now:
And here is the picture how I want it to looks like:
This sounds like a typical ListView use case.
Firstly, I'd suggest you go through the documentation -
https://developer.android.com/reference/android/widget/ListView
You can see an implementation example of a list view with an array of strings here -
https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65
In general, you choose the UI of your item and the listView populates the view to each item in your list (each string in your case).
In the adapter, you give each item the data it needs for the UI.
I would suggest you to use RecyclerView for this type of task. You may use ListView as well.
But RecyclerView is more flexible and advanced than ListView.
Create a simple layout or xml file for your row item to be shown in RecyclerView.
Add that row xml file in onCreateViewHolder method. And inside method onBindViewHolder do necessary task like for example, showing name in the list for each position.
Go to this link for your reference : https://developer.android.com/guide/topics/ui/layout/recyclerview
Instead of Array<String> you can use Array<CustomModel> as well depending on your requirement.
Simple example of RecyclerView with model objects as list : https://www.javatpoint.com/android-recyclerview-list-example
Well, the proper way to do what you need is use ListView or RecyclerView.
Anyway, if you want to use your current solution, you need to specify the position of each TextView.
For example, assign an ID to each textview you create and then set the position of it under the previous one. Here you can find how to do that.

How to add blocks of linearLayouts and is there a better way?

This is the main idea of what I am trying to do.
I would like to ask you what is the best way to make such a design. the problem is that these gray/black blocks might not show up (user will choose which should show up). So I would like to find out do I need to make these 3 text views inside linearLayout programatically? Is there anyway to create some sort of template which I would only have to edit by setting new texts for textViews and add them to some sort of layout?
Another option is to just have a LinearLayout and an xml for the row entry. Then you can do:
LinearLayout layout = (LinearLayout) this.findViewById(R.id.your_layout_id);
List<Blocks> userBlocks = getMyUserBlocks();
for(Block b : userBlocks) {
View blockView = LayoutInflater.from(getBaseContext())
.inflate(R.layout.your_row_layout, layout, false);
TextView someData = (TextView) blockView.findViewById(R.id.your_text_view_id);
someData.setText(b.someAttribute.toString());
layout.addView(blockView);
}
You will need a separate xml layout for the block row.
You can use a ListView with large dividers. Your dark gray blocks are the row views, and you can just append them to a dataset and update the adapter for the ListView. For the dividers, see setDivider() and setDividerHeight().

Get Values From Group Layout in Custom ExpandableListView OnChildClick

So if in
void ChildClick(object o, ExpandableListView.ChildClickEventArgs e)
o is the ExpandableListView that was clicked and
e.Parent is also the ExpandableListView that was clicked and
trying to create an instance of the LinearLayout in the Group for which the ChildClick took place by doing
ExpandableListView view = o;
LinearLayout group = (LinearLayout) view.GetChildAt(e.GroupPosition);
returns the LinearLayout in the nth position and not the GROUP layout in the nth position, how do I get an instance of the LinearLayout in the Group for which the child was clicked?
I'm not really sure about where in the View tree you are trying to get too.
But once you have a handel into that tree you can always call
view.getParent();
to go up a level. And
view.findViewById(R.id.yourlayoutname);
to go do several layers
So this seems like going around my elbow to get to my ass instead of just having the parent of the child group actually be the parent group instead of the list view itself, but I've been able to get what I needed thusly:
ExpandableListView view = (ExpandableListView) o;
IExpandableListAdapter mAdapter = view.ExpandableListAdapter;
string s = mAdapter.GetGroup(e.GroupPosition).ToString(); //returns comma separated string with all my values from the group layout in the clicked item group position.
string[] items = s.Split(new [] {","}, StringSplitOptions.None);
Still open for suggestions if there is a way to get an instance of the actual group layout rather than just hoping the string in mAdapter always has the same number of comma delimited strings and the one I am after is always in the same spot. I want to get the actual LinearLayout in group position n so I can find the TextView by ID inside of it.

Updating some rows dynamically in TableLayout

I'm adding rows to my TableLayout dynamically and each row has more than one TextView.
I want to loop for each row in my TableLayout and I want to update some rows (not every row), for example the text of TextView in my rows. How can I manage this?
Loop through the children of the TableLayout, they should be TableRows (or another layout) - skip the ones you don't want to update. Loop through or findViewById the children of the TableRow (or whatever) which will be your TextViews
When you add the TextView / CheckBox to the TableRow, you can set the View's id with setId(R.id.id_of_the_view) (which you might want to add to the res/values/ids.xml file).
Then loop through like so:
TableLayout tableLayout = null;
for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
TableRow row = (TableRow)tableLayout.getChildAt(n);
TextView name = (TextView)row.findViewById(R.id.tv_name);
}
since you're using row.findViewById, you're looking for a specific id inside a specific row.
I recommend using the setTag method to quick reach specific rows, and Views within rows.
For example, lets say you have a CheckBox in a certain column, which represents "isSomethingEnabled". When creating the CheckBox, do setTag("foo").
Use setTag on the row as well, such as setTag("rowKey")
To quickly get to a specific row
TableRow tRow = (TableRow) tLayout.findViewWithTag("rowKey");
And to quickly get to a specific child
View v = tRow.findViewWithTag("foo");

Categories

Resources