gpt4 book ai didi

android - 使用 SQLite 游标在 fragment 中的 ListView 中输出表内容

转载 作者:行者123 更新时间:2023-11-29 17:41:09 27 4
gpt4 key购买 nike

我有 MyDBHandler 和一个 getAllDetails 方法,该方法使用游标查询数据库并返回一个列表。

我现在不知道如何在另一个 fragment 的 listview 中输出这个列表。我被告知要创建两个 XML 布局和一个自定义适配器,但我不知 Prop 体怎么做!

MyDBHandler 类

package com.astuetz.viewpager.extensions.sample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

import java.util.ArrayList;
import java.util.List;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "detailsDB.db";
public static final String TABLE_DETAILS = "details";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRSTNAME = "firstname";
public static final String COLUMN_SURNAME = "surname";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_ADDRESS1 = "address1";
public static final String COLUMN_ADDRESS2 = "address2";

public static final String TABLE_KIN_DETAILS = "kindetails";
public static final String COLUMN_KIN_ID = "_id";
public static final String COLUMN_KIN_YOUREMAIL = "youremailkin";
public static final String COLUMN_KIN_FIRSTNAME = "firstnamekin";
public static final String COLUMN_KIN_SURNAME = "surnamekin";
public static final String COLUMN_KIN_PHONE = "phonekin";
public static final String COLUMN_KIN_EMAIL = "emailkin";
public static final String COLUMN_KIN_ADDRESS1 = "address1kin";
public static final String COLUMN_KIN_ADDRESS2 = "address2kin";

// Pass database information along to superclass
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String query = " CREATE TABLE " + TABLE_DETAILS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_FIRSTNAME + " TEXT, "
+ COLUMN_SURNAME + " TEXT, "
+ COLUMN_PHONE + " TEXT, "
+ COLUMN_EMAIL + " TEXT, "
+ COLUMN_ADDRESS1 + " TEXT, "
+ COLUMN_ADDRESS2 + " TEXT "
+ ");";

String query2 = " CREATE TABLE " + TABLE_KIN_DETAILS + "("
+ COLUMN_KIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_KIN_YOUREMAIL + " TEXT, "
+ COLUMN_KIN_FIRSTNAME + " TEXT, "
+ COLUMN_KIN_SURNAME + " TEXT, "
+ COLUMN_KIN_PHONE + " TEXT, "
+ COLUMN_KIN_EMAIL + " TEXT, "
+ COLUMN_KIN_ADDRESS1 + " TEXT, "
+ COLUMN_KIN_ADDRESS2 + " TEXT "
+ ");";
db.execSQL(query);
db.execSQL(query2);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_DETAILS);
db.execSQL(" DROP TABLE IF EXISTS " + TABLE_KIN_DETAILS);
onCreate(db);
}

//Add a new row to the database
public void addDetails(Details details) {
ContentValues values = new ContentValues();
values.put(COLUMN_FIRSTNAME, details.getFirstname());
values.put(COLUMN_SURNAME, details.getSurname());
values.put(COLUMN_PHONE, details.getPhone());
values.put(COLUMN_EMAIL, details.getEmail());
values.put(COLUMN_ADDRESS1, details.getAddress1());
values.put(COLUMN_ADDRESS2, details.getAddress2());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DETAILS, null, values);
db.close();
}

public void addKinDetails(KinDetails kinDetails){
ContentValues values = new ContentValues();
values.put(COLUMN_KIN_YOUREMAIL, kinDetails.getyourEmailkin());
values.put(COLUMN_KIN_FIRSTNAME, kinDetails.getFirstnamekin());
values.put(COLUMN_KIN_SURNAME, kinDetails.getSurnamekin());
values.put(COLUMN_KIN_PHONE, kinDetails.getPhonekin());
values.put(COLUMN_KIN_EMAIL, kinDetails.getEmailkin());
values.put(COLUMN_KIN_ADDRESS1, kinDetails.getAddress1kin());
values.put(COLUMN_KIN_ADDRESS2, kinDetails.getAddress2kin());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_KIN_DETAILS, null, values);
db.close();
}



public List<Details> getAllDetails(){

//create a new list in which we put all persons
List<Details>detailsList = new ArrayList<>();

SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_DETAILS;

//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results

if (c != null) {

c.moveToFirst();

//Position after the last row means the end of the results
while (!c.isAfterLast()) {

//create new details object
Details details = new Details();

//Here use static declared on top of the class..don't use "" for the table column
details.set_id(c.getColumnIndex(COLUMN_ID));
details.setFirstname(c.getString(c.getColumnIndex(COLUMN_FIRSTNAME)));
details.setSurname(c.getString(c.getColumnIndex(COLUMN_SURNAME)));
details.setPhone(c.getString(c.getColumnIndex(COLUMN_PHONE)));
details.setEmail(c.getString(c.getColumnIndex(COLUMN_EMAIL)));
details.setAddress1(c.getString(c.getColumnIndex(COLUMN_ADDRESS1)));
details.setAddress2(c.getString(c.getColumnIndex(COLUMN_ADDRESS2)));

detailsList.add(details);


c.moveToNext();
}

c.close();
}

db.close();

//return our list of persons
return detailsList;

}
}

最佳答案

您需要先为 fragment 和 ListView 行创建布局文件。

对于 fragment ,您可以创建一个新的空白 fragment ,如下所示:注意:我们使用空白 fragment 是因为这是一种很好的学习习惯,因为它可以让您在复杂的情况下有更多的控制权。

enter image description here

enter image description here

在 fragment_details.xml 中粘贴以下代码:注意将 com.companyname 更改为您的应用程序包名称!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.companyname.myapplication.FragmentDetails">


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag_details_listView"
android:layout_weight="1" />

</LinearLayout>

创建一个新的 xml 布局文件并将其命名为 row_details ,这将是我们 ListView 的自定义行。

enter image description here

并在文件 row_details.xml 中粘贴以下代码:

<?xml version="1.0" encoding="utf-8"?>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/row_details_textview_name"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/row_details_textview_id"
android:layout_marginLeft="10dp" />

</LinearLayout>

我们现在需要的是一个自定义适配器,它将负责将数据加载到我们的 ListView 中。

创建一个新的 Java 类并将其命名为 DetailsAdapter,并粘贴以下代码:请注意在代码中查看我的评论,因为了解 Android 适配器背后的概念非常重要:

public class DetailsAdapter extends ArrayAdapter<Person> {

private Context context;

//Constructor
public DetailsAdapter(Context context, int resource, List<Person> objects) {
super(context, resource, objects);

this.context = context;
}


//The get view is the most crucial part of the adapter, here the listview asks the
//adapter for the row to display

@Override
public View getView(int position, View row, ViewGroup parent) {

//Get an instance of our holder
Holder holder;


//Check if this is the first time we are creating this row for the listview
if (row == null){

//Row was null and thus we need to get components from the row_details.xml
holder = new Holder();

//get the Android's layout inflater service which will read our row_details.xml
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//Fill our row view with the xml layout file
row = inflater.inflate(R.layout.row_details, null);

//Fill our holder with the text view components
holder.textViewName = (TextView)row.findViewById(R.id.row_details_textview_name);
holder.textViewId = (TextView)row.findViewById(R.id.row_details_textview_id);

//This is very imp! attach our holder to the row
row.setTag(holder);

}else{

//row was created before! thus get the holder object from the row tag
holder = (Holder)row.getTag();
}


//At this point we have our row, either created from new or got it from the row tag object
//we can now fill the data

//First get our object from the list which is in the position of the listview
//The position as you can see is passed to the getView method by the listview
Person person = getItem(position);

holder.textViewName.setText(person.getFirstname());
holder.textViewId.setText("ID: " + person.get_id());

//we are done formatting our row..return to listview to show
return row;
}

//A holder will be resposable to hold our components to improve listview performance
//We replicate the components we have in the row_details.xml
private class Holder{

TextView textViewName;
TextView textViewId;


}

}

此时我们准备好隆隆声了!!

在 FragmentDetails java 类中,我们声明了一个全局私有(private)的详细信息列表...我们得到了我们的 ListView 的一个实例..我们得到了数据..创建一个新的 DetailsAdapter 并将其附加到 ListView ..

public class FragmentDetails extends Fragment {

private List<Details>detailsList = new ArrayList<>();

private ListView mListView;

private DetailsAdapter adapter;

public FragmentDetails() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_details, container, false);

//get an instance of our listview
mListView = (ListView)rootView.findViewById(R.id.frag_details_listView);

//Get the data here!!
MyDBHandler dbHandler = new MyDBHandler(getActivity().getApplicationContext());
detailsList = dbHandler.getAllDetails();

//Initiate our adapter
adapter = new DetailsAdapter(getActivity().getApplicationContext(), R.layout.row_details, detailsList);

//set adapter to the listview
if(adapter != null){
mListView.setAdapter(adapter);
}

return rootView;

}


}

注意!!

在您的 MyDbHandler 类中包含另一个构造函数,它仅将上下文作为参数,如下所示:

public MyDBHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

你很高兴..运行项目

通过这个过程你可以在android中创建任何类型的 ListView

关于android - 使用 SQLite 游标在 fragment 中的 ListView 中输出表内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com