我在Android工作室为我的作业建立一个基本的购物清单应用程序。我是Java的新手,在从SQLite传递数据时会对ArrayAdapter
感到困惑。请看一下我的代码,我会尽力解释我面临的问题=(
DatabaseHelper类
package com.puyakul.prin.psychic_shopping;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "shoppinglist";
private static final int DN_VERSION = 2;
public DatabaseHelper(Context context){
super (context,DB_NAME, null, DN_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlLists = "CREATE TABLE lists(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR)";
String sqlItems = "CREATE TABLE items(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, list_id INTEGER, FOREIGN KEY(list_id) REFERENCES lists(id))";
db.execSQL(sqlLists);
db.execSQL(sqlItems);
}
public boolean addLists(String name){
//Open database
SQLiteDatabase db = getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
db.insert("lists", null, contentValues);
//Close database
db.close();
return true;
}
public boolean addListsItem(String name, int id){
//Open database
SQLiteDatabase db = getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("list_id", id);
db.insert("items", null, contentValues);
//Close database
db.close();
return true;
}
public int deleteItem (String id){
SQLiteDatabase db= getReadableDatabase();
return db.delete("item", "ID = ?", new String[] {id});
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sqlLists = "DROP TABLE IF EXISTS lists";
String sqlItems = "DROP TABLE IF EXISTS items";
db.execSQL(sqlLists);
db.execSQL(sqlItems);
onCreate(db);
}
public Cursor getList(){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM lists";
return db.rawQuery(sql, null);
}
public Cursor getListDetail(int id){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT items.name FROM items WHERE items.list_id = '" + id + "'";
//String sql = "SELECT items.name FROM items INNER JOIN lists ON lists.id=items.list_id WHERE items.list_id = '" + id + "'";
return db.rawQuery(sql,null);
}
public Cursor getListID(String name){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT ID FROM lists WHERE name = '" + name + "'";
return db.rawQuery(sql, null);
}
}
MainListDetail类
package com.puyakul.prin.psychic_shopping;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
public class MainListsDetail extends AppCompatActivity {
private Button btn_deleteItem, btn_editListName;
private EditText editText_ListName, editText_addNewItemName;
private TextView textView_listName;
private ListView ListView_ListDetail;
private DatabaseHelper db;
private String selectedList;
private int selectedID;
public ArrayList<String> listDetailData = new ArrayList<>();
//Time stamp code, matching current time zone
//private Time today = new Time(Time.getCurrentTimezone());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_lists_detail);
//declare layout elements
ListView_ListDetail = findViewById(R.id.ListView_ListDetail);
textView_listName = findViewById(R.id.textVie_listName);
editText_addNewItemName = findViewById(R.id.editText_addNewItemName);
//declare database
db = new DatabaseHelper(this);
//get the intent extra from MainListView
Intent receivedIntent = getIntent();
//now get the listID we passed on extra
selectedID = receivedIntent.getIntExtra("id",0); // -1 for the default value
//now get the name we passed on extra
selectedList = receivedIntent.getStringExtra("name");
//set text to show the current selected name
textView_listName.setText(selectedList);
// ArrayAdapter aa = new ArrayAdapter(..., itemsList);
// ArrayAdapter itemListAdapter = new ArrayAdapter();
ListView_ListDetail.setAdapter(new ListItemAdapter(this, R.layout.activity_item_detail_list, listDetailData));
getListDetail();
btn_editListName = findViewById(R.id.btn_addNewItem);
btn_editListName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItem();
editText_addNewItemName.getText().clear();
}
});
}
public void addItem(){
String newItemName = editText_addNewItemName.getText().toString().trim();
if(TextUtils.isEmpty(newItemName)){
Toast.makeText(this, "Please enter a new item", Toast.LENGTH_SHORT).show();
// ****Must have return to make it work****
return;
}
if(db.addListsItem(newItemName,selectedID)){
Toast.makeText(this, "New item added to the list", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "Error, no item add to the list", Toast.LENGTH_SHORT).show();
}
getListDetail();
}
public void getListDetail(){
listDetailData.clear();
Cursor cursor = db.getListDetail(selectedID);
//Log.d("TAG", "selected ID" +selectedID);
//ArrayList<String> listDetailData = new ArrayList<>();
while(cursor.moveToNext()){
listDetailData.add(cursor.getString(0));
}
//ListAdapter listDetailAdapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, listDetailData);
//ListView_ListDetail.setAdapter(listDetailAdapter);
((ArrayAdapter<String>)ListView_ListDetail.getAdapter()).notifyDataSetChanged();
}
}
ListAdapter类
package com.puyakul.prin.psychic_shopping;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ListItemAdapter extends ArrayAdapter<String> {
private static final String TAG = "ListItemAdapter";
public ListItemAdapter(@NonNull Context context, int resource, @NonNull ArrayList<String> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null){
// LayoutInflater inflater = LayoutInflater.from(getContext());
// convertView = inflater.inflate(layout, parent, false);
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_item_detail_list, parent, false);
}
ViewHolder viewHolder = new ViewHolder();
viewHolder.listItem_Thumbnail = convertView.findViewById(R.id.img_thumbnail);
viewHolder.listItem_Name = convertView.findViewById(R.id.textView_listItemText);
viewHolder.listItem_Remove_btn = convertView.findViewById(R.id.btn_listItemRemove);
viewHolder.listItem_Name.setText(getItem(position));
convertView.setTag(viewHolder);
return convertView;
}
public class ViewHolder {
ImageView listItem_Thumbnail;
TextView listItem_Name;
Button listItem_Remove_btn;
}
}
首先,代码运行正常,没有错误。我可以从文本字段插入一个新数据,但据我所知,我的函数是直接将数据插入数据库,它只保存一个字符串。我面临的问题是,每当我将此数据传递到ArrayAdapter
时,例如在此代码行public class ListItemAdapter extends ArrayAdapter<String>
中,它只指向单个字符串对吗?
问题是我如何传递一个类,如果我想创建一个单独的GET-SET类来处理所有这些信息而不是直接用数据库String处理它。
我希望这里的某些人有意义,我很抱歉英语不是我的第一语言=(加上我真的不懂java,这让我很难写出来。
我非常感谢所有帮助。
答案 0 :(得分:2)
我面临的问题是,每当我将此数据传递到
ArrayAdapter
时,例如在这行代码publicclass ListItemAdapter extends ArrayAdapter<String>
中,它只指向单个字符串吗?
第一步是改变
class ListItemAdapter extends ArrayAdapter<String>
到
class ListItemAdapter extends ArrayAdapter<MyClass>
然后在getView()
中,您可以使用position
运算符来获取对MyClass
的正确实例的引用,并使用其值填充视图。
由于您使用的是数据库,我建议您了解CursorAdapter
以替换ArrayAdapter
。从SimpleCursorAdapter
开始,因为它提供了许多基本用途。