代码之家  ›  专栏  ›  技术社区  ›  George Zoiade

Android数据库自定义列表视图对多个活动打开[已关闭]

  •  -2
  • George Zoiade  · 技术社区  · 11 年前

    enter image description here

    我发现这个应用程序使用具有多个表的数据库。这是一个节省收入和开支的列表视图,但它们的活动不同。有没有人知道我怎么做这种列表视图?

    1 回复  |  直到 11 年前
        1
  •  0
  •   zheek    11 年前

    这很简单。首先,您需要像这样创建class extend 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(newlistview());用于使用自定义布局填充每行。