我是Android开发的初学者。我已从预先填充的SQLite DB填充了listview。但我需要使用自定义列表视图,因为在这里它只显示主项目,我需要在此列表视图中填充一个值到子项目。请帮我完成这个任务。
这是分配列表视图的java。
public class ContactView extends AppCompatActivity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
this.listView = (ListView) findViewById(R.id.listView);
DBAccess databaseAccess = DBAccess.getInstance(this);
databaseAccess.open();
List<String> quotes = databaseAccess.getQuotes();
databaseAccess.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
this.listView.setAdapter(adapter);
}}
这是自定义列表视图XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/hello_avator"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Company Name"
android:id="@+id/org_name"
android:layout_alignTop="@+id/imageView2"
android:layout_toRightOf="@+id/imageView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="000-0000000"
android:id="@+id/cn_num"
android:layout_alignLeft="@+id/org_name"
android:layout_alignStart="@+id/org_name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/org_name"
android:layout_alignBottom="@+id/imageView2" />
这是DataBase Access java文件。
public class DBAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
public DBAccess(Context context) {
this.openHelper =new HelloDatabase(context);
}
public static DBAccess getInstance(Context context) {
if (instance == null) {
instance = new DBAccess(context);
}
return instance;
}
public void open() {
this.database = openHelper.getWritableDatabase();
}
public void close() {
if (database != null) {
this.database.close();
}
}
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT org_name FROM org_name", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
答案 0 :(得分:0)
您需要通过扩展ArrayAdapter
类(如果需要,还可以从BaseAdapter
扩展)来创建自定义适配器,而不是使用默认的ArrayAdapter
,然后定义自定义在 getView() 中查看。
public class MyCustomAdapter extends ArrayAdapter<String> {
....
public MyCustomAdapter(....) {
super(...);
...
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom view for the row and set the UI
View rowView = inflater.inflate(R.layout.my_custom_list_row, parent, false);
return rowView;
}
}
您可以在 this tutorial 中详细了解自定义适配器。
答案 1 :(得分:0)
您必须创建一个扩展ArrayAdapter或... Adapter的自定义适配器。在自定义适配器中,您必须设置值。 Checkout this article