I have a function who gets a list of products from a webpage, and I want to add a row in a tableLayout for each element of the list.
public void getProductsOfCategory() throws IOException{
new Thread(){
public void run(){
//background code...
try {
Bundle extras = getIntent().getExtras();
String categoryName = extras.getString("categoryName");
HttpGet httpget = new HttpGet("http://romal.hopto.org/foodadvisor/users/getProductsOfCategory.json?category="+categoryName);
HttpResponse httpresp = httpClient.execute(httpget);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpresp.getEntity().writeTo(baos);
final String content = new String(baos.toByteArray());
CategoryActivity.this.runOnUiThread(new Runnable(){
public void run(){
//foreground code (UI)
//update user interface, for example, showing messages
try {
JSONObject jObject = new JSONObject(content);
JSONArray productsList = jObject.getJSONArray("response");
for(int i=0; i<productsList.length();i++){
JSONObject product = productsList.getJSONObject(i);
JSONObject productData = product.getJSONObject("Product");
String productDescription = productData.getString("description");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
In my layout xml file I have defined the table layout like this:
<TableLayout
android:id="@+id/tableOfProducts"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/productDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:paddingBottom="7dp"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:paddingTop="7dp"
android:textSize="20dp" />
</TableLayout>
I imagine I have to add some extra code in the for loop, adding a new row and a new textview for each element, and setting the content of the text view with the string that has the description of the product. How can I do this?
for this, you have to create one xml file for row, then you have to modify your layout.xml file like
Then in for loop, inflate the row layout and add it runtime to rowHolder object.
Check this out, this is the general way of creating table rows dynamically. Modify it accordingly
XML file
JAVA PART
Create table row header to hold the column headings
I'm adding two data sections to the table row
After adding the columns to the table row its time to add the table row the the main table layout that we fetched at the start
EDIT : YOU CAN DO THE FOLLOWING IN YOUR CODE
// Add each table row to table layout
Creating the TextView and TableRow array are not necessary. You can just include
part1
,part2
,part3
(if you need more than 1 field) andpart4
inside your for loop.I'm new myself to Android, and after years with Python/Gtk, I'm lost if I can't think in terms of Model/View/Presenter.
one useful approach I managed to apply, it puts me back into that optics, and even if it's not relative to a Table, I'm quite sure it can be extended to that (I guess this page would be a good start, for me too).
start by declaring the two objects in your fragment (or activity):
define the model in the fragment constructor:
next, when the onCreateView of my fragment is invoked, I do this (edit correspondingly if you're using Activity, not Fragment):
textrow.xml
is an extra layout which is being reused every time I add an element to thestringList
model.in the rest of the code, I only "talk" to
listAdapter
, and I only need its twoadd
andclear
methods, to add elements, or for clearing the list.Look, i think you have to modify your xml to adding rows to your tableview:
First, the inflater:
or in this way:
If you need more help, tell me, if it is helpful, rating me!!! ;) Un saludo Gallega! O como se dice en gallego: una aperta!