Android数据库自定义列表视图对多个活动开放
Android数据库自定义列表视图对多个活动开放,android,listview,android-cursoradapter,Android,Listview,Android Cursoradapter,我发现这个应用程序使用一个包含多个表的数据库。这是一个列表视图,可以节省收入和费用,但它们在不同的活动中。有人知道如何制作这种listview吗?非常简单。首先,您需要创建类extend listActivity,如下所示: 公共课调味品活动{ private database db; private String[] Name; private String[] Teedad; @Override protected void onCreate(Bundle savedInstanceSta
我发现这个应用程序使用一个包含多个表的数据库。这是一个列表视图,可以节省收入和费用,但它们在不同的活动中。有人知道如何制作这种listview吗?非常简单。首先,您需要创建类extend 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>
<?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" />
请注意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);
}
类listview扩展了ArrayAdapter{
公共列表视图(){
超级(调味品。本,右布局。生调味品,名称);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater in=getLayoutInflater();
视图行=英寸充气(R.layout.raw\u调味,父级,假);
TextView name=(TextView)row.findViewById(R.id.name\u);
TextView teedad=(TextView)row.findViewById(R.id.teedad_dastan);
name.setText(名称[职位]);
teedad.setText(teedad[位置]);
返回(行);
}
对于此活动,我们创建如下布局文件:
<?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>
<?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());用自定义布局填充每一行。这太宽泛了。您需要启动项目,并发布一个更具体的问题,包括相关代码和错误(如果有)。带有经过投票和接受的问题不应标记为“过于宽泛”,即使有可能找到他们可能违反规则的证据。