DB结果变为ListView,数据可能会有所不同

时间:2015-12-31 13:54:36

标签: android sql listview android-listview

你有点新的java / android编程,我试图从数据库填充listview。问题是每条记录的数据库输出可能会有不同的结果。为了给你一个想法,我在我的xml中为listview提供了以下布局;

<?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="match_parent"
android:background="#a28d7a">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/coreHeader"
    android:textColor="#ffffff"
    android:padding="4dp" />
<LinearLayout
    android:id="@+id/coreLinear"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="4dp">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/coreStatsLeft"
        android:textColor="#ffffff" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView"
        android:textColor="#ffffff"
        android:textAlignment="textEnd"
        android:gravity="right" />
</LinearLayout>
</LinearLayout>

第一个TextView是排序的标题,更大的字体等。在这个区域它将保存数据库中的两个字段,例如NAME和LEVEL;蜘蛛侠(Lvl 5)。

它下面的两个文本字段将保存统计数据,例如此区域可以有多个;

Name                Value
------------------------------------
Armor                 10%
Speed                  5%
... and so on

每个记录可能不同,有些可能没有速度,有些可能没有盔甲......也许这很难通过ListView来做,但我需要它可以搜索到我不知道我有什么其他选择可用于此。

我的数据库字段如下:

Id,Name,Level,Stats

填充的数据库的示例将读取;

1,蜘蛛侠,5,3 | 10%; 5 | 5%; 6 | 3%

1 个答案:

答案 0 :(得分:1)

我创建了一个示例来说明如何将变量数据添加到列表视图中的每一行。

这是活动实施:

public class MainActivity extends AppCompatActivity
{
    private DynamicAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Simulating variable data per row. Array Adapter instead Cursor Adapter for simplicity
        final DataRow[] data =
                {
                        new DataRow("Spider Man", new String[] {"3|10%", "5|25%"}),
                        new DataRow("Spider Man Brother", new String[] {"3|20%"}),
                        new DataRow("Other Spider Man", new String[] {"3|22%", "1|12%", "4|7%"})
            };

        // Assign data to adapter
        adapter = new DynamicAdapter(this, data);

        // Getting reference to ListView
        ListView listViewData = (ListView) findViewById(R.id.listViewData);

        // Setting adapter to listView
        listViewData.setAdapter(adapter);
    }
}

这是适配器为每行放置数据的方式:

public class DynamicAdapter extends ArrayAdapter<DataRow>
{
    private final Context context;

    private final DataRow[] data;

    public DynamicAdapter(Context context, DataRow[] data)
    {
        super(context, R.layout.row_layout, data);

        this.context = context;

        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.row_layout, parent, false);

        // Get a listview reference
        LinearLayout linearLayoutRow = (LinearLayout) rowView.findViewById(R.id.coreLinear);

        if(position < data.length)
        {
            // Put text on header
            TextView textHeader = (TextView) rowView.findViewById(R.id.coreHeader);

            // Set the header for this row
            textHeader.setText(data[position].GetHeader());

            // Creating a vertical linear layout to put the data row
            LinearLayout linearLayoutData = new LinearLayout(context);
            linearLayoutData.setOrientation(LinearLayout.VERTICAL);

            // Get column amount for this row.
            int columnAmount = data[position].GetColumnsSize();

            for(int i = 0; i < columnAmount; i++)
            {
                // Creating dynamically a TextView for this column.                    
                TextView dynamicData = new TextView(context);

                dynamicData.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));

                // Adding data to TextView
                dynamicData.setText(data[position].GetDataAt(i));

                // Adding TextView to linearLayout
                linearLayoutData.addView(dynamicData);
            }

            // Adding LinearLayout to LinearLayout on XML  
            linearLayoutRow.addView(linearLayoutData);
        }

        return rowView;
    }
}

这是Github上的完整演示:https://github.com/deinier/DynamicDataOnListView

我希望这有帮助。