Novice
Novice

Reputation: 77

How to add TextView automatically base on TABLE rows

Good day! How can I add TextView automatically base on table rows and display the row data in the generated TextView, or is there another way to accomplish this.

Row 1
    PA : Savings Account // Col 1
    2015-08-17           // Col 2  
    483.67               // Col 3
    483.67               // Col 4

Row 2
    PA : Savings - Cash Bond // Col 1
    2015-08-28               // Col 2
    10129.43                 // Col 3
    10129.43                 // Col 4

this is my current code

 private TextView textView_SL_Desc;
 private TextView textView_SL_Bal;

 textView_SL_Desc = (TextView) findViewById(R.id.sl_desc);
 textView_SL_Bal = (TextView) findViewById(R.id.actual_balance);

 // Fetching USER sl details from SQlite
 HashMap<String, String> sl_summ = db.getUserSLDTL();
 String sl_desc = sl_summ.get("sl_desc");
 String actual_balance = sl_summ.get("actual_balance");

 //Set text to text view
 textView_SL_Desc.setText(sl_desc);
 textView_SL_Bal.setText(actual_balance);

My XML

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center"
        android:orientation="vertical" >

       <TextView
           android:id="@+id/sl_desc"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="13sp" />

      <TextView
          android:id="@+id/actual_balance"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="13sp" />

 </LinearLayout>

Upvotes: 0

Views: 266

Answers (3)

wdina
wdina

Reputation: 455

RecyclerView will solve your problem for dynamically creating a list of views (in your case, your XML file) in an efficient way.

How to use RecyclerView:

  1. Create a ViewHolder class to hold your TextView widgets

    public class RowViewHolder extends RecyclerView.ViewHolder {
         public final TextView textView_SL_Desc;
         public final TextView textView_SL_Bal;
    
         public RowViewHolder(View itemView) {
             super(itemView);
    
             textView_SL_Desc = (TextView) itemView.findViewById(R.id.sl_desc);
             textView_SL_Bal = (TextView) itemView.findViewById(R.id.actual_balance);
         }
    }
    
  2. Create a RecylerView.Adapter class to bind your data with your UI (something similar to two-way binding model if you are familiar with AngularJS)

    public class RecyclerAdapter RecyclerView.Adapter<RowViewHolder> {
        private List<HashMap<String, String>> sl_summList;
    
        public RecyclerAdapter(List<HashMap<String, String>> sl_sumnList) {
                    this.sl_sumnList = sl_sumnList;
        }
    
        // This is the code where RecyclerView knows how to create views from your ViewHolder class (in this case, RowViewHolder)            
        @Override
        public RowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.your_xml_layout, parent, false);
            return new RowViewHolder(view);
        }
    
        // This is the code where RecyclerView populates data (binds data to UI)
        @Override
        public void onBindViewHolder(RowViewHolder holder, int position) {
            // This is the code where you get data based on position of the table. I cannot write specific code because I don't know how your hashmap looks like
            String sl_summ = this.sl_sumnList.get(position);
            holder.textView_SL_Desc.setText(sl_summ.get("sl_desc"));
            holder.textView_SL_Bal.setText(sl_summ.get("actual_balance"));
        }
    
        // How RecyclerView knows how many items are in the list
        @Override
        public int getItemCount() {
            return sl_sumnList.size();
        }
    }    
    
  3. Hooks RecyclerView with your RecylerView.Adapter class

        mRecyclerView = (RecyclerView) findViewById(R.id.your_recycler_view_id);
    
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
    
        List<HashMap<String,String> dbRowList = db.get_all_rows(); // get all items from your database)
        mRecyclerAdapter = new RecyclerAdapter(dbRowList);
        mRecyclerView.setAdapter(mRecyclerAdapter); // finally hooks up RecyclerView.Adapter with RecyclerView.
    

Here is an official tutorial of ReyclerView, https://developer.android.com/training/material/lists-cards.html.

Upvotes: 1

Nostram&#228;rus
Nostram&#228;rus

Reputation: 27

there are several ways to accomplish this. I prefer to add Linearlayouts dynamically. It´s easy and you can Display nearly every data you want.

First: Take your .xml file and name it into "show_data_item"

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="100sp"
    android:weightSum="2"
    android:id="@+id/show_data_item">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2sp"
    android:background="#000000"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0sp"
    android:layout_weight="1"
    android:weightSum="2">
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
    <TextView
        android:id="@+id/col1"
        android:layout_width="wrap_content"
        android:text="Display Data Col1"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
    <TextView
        android:id="@+id/col2"
        android:text="Display Data Col2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2sp"
    android:background="#000000"/>
<LinearLayout
    android:layout_height="0sp"
    android:layout_weight="1"
    android:weightSum="2"
    android:layout_width="match_parent">
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
    <TextView
        android:id="@+id/col3"
        android:layout_width="wrap_content"
        android:text="Display Data Col3"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
    <TextView
        android:id="@+id/col4"
        android:layout_width="wrap_content"
        android:text="Display Data Col4"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="2sp"
        android:layout_height="match_parent"
        android:background="#000000"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="2sp"
    android:background="#000000"/>


  </LinearLayout>

Step 2: Create an xml Layout for your activity and don´t forget to add it to "manifest.xml", like this (made it with a scrollview so that you have unlimited space) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/displaydata"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"></LinearLayout>
</ScrollView>

</LinearLayout>

Step 3: Create an class in java which fill´s the textviews with your col1-4, and add this to your "id/displaydata" with the funktion:

  myView.addview(show_data_item); //myView is an Object of LinearLayout and show_data_item is also a object of LinearLayout.

This is how you do it.

You can also use a List/RecylerView and add your data with an Adapter. You still need an Layout for your show_data_item.

Here is a code example for this

All ways leads to rome, i guess.

Upvotes: 0

JRG
JRG

Reputation: 4187

You should explore ListView and ArrayAdapter for displaying dynamic list of data (in your case table rows).

You can have your own designed ListView to display data in specific manner.

Upvotes: 0

Related Questions