Android数据库自定义列表视图可以打开多个活动

时间:2015-02-27 19:52:18

标签: android listview android-cursoradapter

enter image description here

我发现这个应用程序使用带有多个表的数据库。这是一个列表视图,可以节省收入和开支,但它们处于不同的活动中。有谁知道我怎样才能制作这种列表视图?

1 个答案:

答案 0 :(得分:0)

这很简单。首先你需要创建类扩展listActivity,如下所示:      公共类赛季扩展了ListActivity {

private database db;
private String[] Name;
private String[] Teedad;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.seasone);
    db = new database(this);


    refresher();
    setListAdapter(new listview ());

}

和Xml就像这样:

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

  <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" >
  </ListView>

  </RelativeLayout>

请注意android:id =“@ android:id / list”这非常重要。 好。  现在我们为每一行创建自定义布局类。首先创建类并扩展ArrayAdapter。

  class listview extends ArrayAdapter<String>{

    public listview (){
        super(seasones.this,R.layout.raw_seasone,Name);

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater in = getLayoutInflater();
        View row = in.inflate(R.layout.raw_seasone, parent,false);

        TextView name = (TextView) row.findViewById(R.id.name_season);
        TextView teedad = (TextView) row.findViewById(R.id.teedad_dastan);

        name.setText(Name [position]);
        teedad.setText(Teedad [position]);



        return (row);
    }

对于此活动,我们创建一个这样的布局文件:

   <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TextView
      android:id="@+id/teedad_dastan"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginLeft="18dp"
       android:layout_marginTop="26dp"
       android:text="Small Text"
       android:textAppearance="?android:attr/textAppearanceSmall" />

   <TextView
       android:id="@+id/name_season"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBottom="@+id/teedad_dastan"
       android:layout_alignParentRight="true"
       android:layout_marginRight="22dp"
       android:text="Large Text"
       android:textAppearance="?android:attr/textAppearanceLarge" />

我使用refresher();从数据库填充数组的方法。并使用setListAdapter(new listview());用自定义布局填充每一行。