如何基于TABLE行自动添加TextView

时间:2017-07-13 06:35:30

标签: android

美好的一天!如何基于表行自动添加TextView并在生成的TextView中显示行数据,或者是否有其他方法可以实现此目的。

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

这是我目前的代码

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

我的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>

3 个答案:

答案 0 :(得分:1)

RecyclerView将以有效的方式解决您动态创建视图列表(在您的情况下,您的XML文件)的问题。

如何使用RecyclerView:

  1. 创建一个ViewHolder类来保存TextView小部件

    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. 创建一个RecylerView.Adapter类,将您的数据与UI绑定(如果您熟悉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. 使用RecylerView.Adapter类

    挂钩RecyclerView
        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.
    
  4. 这是ReyclerView的官方教程,https://developer.android.com/training/material/lists-cards.html

答案 1 :(得分:0)

您应该浏览ListViewArrayAdapter以显示动态数据列表(在您的案例表行中)。

您可以使用自己设计的ListView以特定方式显示数据。

答案 2 :(得分:0)

有几种方法可以实现这一目标。 我更喜欢动态添加Linearlayouts。这很容易,你几乎可以显示你想要的每一个数据。

首先:获取.xml文件并将其命名为“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>

第2步:为您的活动创建一个xml布局,不要忘记将其添加到“manifest.xml”,就像这样(使用scrollview创建它,以便您拥有无限的空间):

<?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>

步骤3:在java中创建一个用col1-4填充textviews的类,并使用funktion将其添加到“id / displaydata”中:

  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.

您还可以使用List / RecylerView并使用适配器添加数据。您仍然需要show_data_item的布局。

Here is a code example for this

我猜,所有方式都会导致罗马。