android - 从 sqlite 数据库中从自定义列表视图的列表视图中选择的问题
问题描述
我无法从我的自定义列表视图中选择并从 sqlite 数据库中获取 itemName。我在下面放置了一些其他代码,不确定它们是否有必要。
Log.d(TAG, "名字是" + itemName); 正在返回“名称是 mypackagename.ListItems@ece56a4”,这不是名称
Log.d(TAG, "ID 是" + ItemID); ID 是 mypackagename.ListItems@ece56a4 而不是 id
ThreeColumn_ListAdapter.class 将 @ece56a4 转换为 3 列。有没有办法从列表视图中选择这些列?
public class ViewListContents extends AppCompatActivity
{
private static final String TAG = "ViewListContents";
DBManager dbManager;
ArrayList<ListItems> listItems;
ListView listView;
ListItems items;
private int selectedID;
private String selectedName;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.example);
dbManager = new DBManager(this);
Intent receiveIntent = getIntent();
selectedID = receiveIntent.getIntExtra("id",-1);
selectedName = receiveIntent.getStringExtra("title");
Log.d(TAG, "TitleID = " + selectedID);
listItems = new ArrayList<>();
Cursor data = dbManager.getItem(selectedID);
int numRows = data.getCount();
if (numRows == 0)
{
Toast.makeText(this, "there is nothing in database", Toast.LENGTH_SHORT).show();
}
else
{
while (data.moveToNext())
{
items = new ListItems(data.getInt(2),data.getString(1), data.getInt(0));
Log.d(TAG, "" + new ListItems(data.getInt(0),data.getString(1), data.getInt(2)));
listItems.add(items);
}
ThreeColumn_ListAdapter adapter = new ThreeColumn_ListAdapter(this, R.layout.custom_list, listItems);
listView = (ListView) findViewById(R.id.item_name_listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String itemName = adapterView.getItemAtPosition(position).toString();
Log.d(TAG, "The name is" + itemName);
Cursor data = dbManager.getItemID2(itemName);
int ItemID = -1;
while (data.moveToNext())
{
ItemID = data.getInt(2);
Log.d(TAG, "The ID is" + ItemID);
}
if (ItemID > -1)
{
Intent edit = new Intent(ViewListContents.this, ItemActivity.class);
edit.putExtra("itemid", ItemID);
edit.putExtra("item", itemName);
startActivity(edit);
}
else
{
Toast.makeText(ViewListContents.this, "No id associated with that name", Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
ThreeColumn_ListAdapter.class
public class ThreeColumn_ListAdapter extends ArrayAdapter<ListItems>
{
private static final String TAG = "ThreeColumn_Listadapter";
private LayoutInflater mInflater;
private ArrayList<ListItems> items;
private int mViewResourceId;
public ThreeColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<ListItems> items)
{
super(context, textViewResourceId, items);
this.items = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents)
{
convertView = mInflater.inflate(mViewResourceId,null);
ListItems listItems = items.get(position);
TextView titleID = (TextView) convertView.findViewById(R.id.text11);
TextView itemID = (TextView) convertView.findViewById(R.id.text22);
TextView itemName = (TextView) convertView.findViewById(R.id.text33);
if (titleID != null)
{
titleID.setText((""+listItems.getid()));
Log.d(TAG, "getting titleid " + listItems.getid());
}
if (itemID != null)
{
itemID.setText((""+listItems.getitemid()));
Log.d(TAG, "getting itemid " + listItems.getitemid());
}
if (itemName != null)
{
itemName.setText((listItems.getcontext()));
Log.d(TAG, "getting itemname " + listItems.getcontext());
}
return convertView;
}
}
ListItem.class getter setter
public class ListItems
{
private final int titleid;
private int id;
private String context;
public int getid()
{
return id;
}
public int getitemid()
{
return titleid;
}
public String getcontext()
{
return context;
}
public ListItems(int id, String context, int titleid)
{
this.id = id;
this.context = context;
this.titleid = titleid;
}
}
dbmanger.class
public class DBManager extends SQLiteOpenHelper
{
static final String TAG ="DBManager";
static final String DB_NAME = "todolist.db";
static final int DB_VERSION = 1;
static final String TABLE_NAME = "title_table";
static final String C_TITLE_ID = "TitleID";///
static final String C_TITLE_NAME = "title_name";
static final String TABLE_ITEM = "item_table";
static final String C_ITEM_ID = "ItemID";///
static final String C_ITEM_NAME = "item_name";
static final String TitleID_ref = "TitleID_ref";
public DBManager(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL("create table " + TABLE_NAME +
" (TitleID INTEGER PRIMARY KEY AUTOINCREMENT, " +
C_TITLE_NAME + " text) ");
database.execSQL("PRAGMA foreign_keys=ON");
database.execSQL("create table " + TABLE_ITEM +
" (ItemID INTEGER PRIMARY KEY AUTOINCREMENT, " +
C_ITEM_NAME + " text, " +
TitleID_ref + " INTEGER,"+
" FOREIGN KEY (TitleID_ref) REFERENCES " + TABLE_NAME + "(TitleID )) ");
Log.d(TAG, "create tables");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("drop table if exists " + TABLE_NAME);
Log.d(TAG, " updating table " + TABLE_NAME);
onCreate(db);
}
public boolean addTitle(String title)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(C_TITLE_NAME,title);
long result = db.insert(TABLE_NAME, null, cv);
db.close();
if (result == -1)
{
return false;
}
else
{
return true;
}
}
public boolean addItem(String title, int item)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(C_ITEM_NAME,title);
cv.put(TitleID_ref,item);
//inserting row
long result = db.insert(TABLE_ITEM, null, cv);
db.close();
if (result == -1)
{
return false;
}
else
{
return true;
}
}
public Cursor getTitle()
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItem(int title)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_ITEM + " WHERE " + TitleID_ref + " = '" + title + " '" ;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItemID(int title)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + C_ITEM_ID + " FROM " + TABLE_ITEM +
" WHERE " + C_TITLE_ID + " = '" + title + "'" ;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItemID2(String itemid)//naming
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + C_ITEM_ID + " FROM " + TABLE_ITEM +
" WHERE " + C_ITEM_NAME + " = '" + itemid + "'" ;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getTitleID(String title)
{
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + C_TITLE_ID + " FROM " + TABLE_NAME +
" WHERE " + C_TITLE_NAME + " = '" + title + "'" ;
Cursor data = db.rawQuery(query, null);
return data;
}
}
解决方案
当您执行以下操作时,您将获取ListItems
对象并将其分配给String
.
// This gives you the object associated with your list
String itemName = adapterView.getItemAtPosition(position).toString();
如果要获取列表项的 id,则需要执行以下操作。
String itemName = adapterView.getItemAtPosition(position).getid() + "";
推荐阅读
- c# - JSON 字符串到 C# 类对象
- elixir - 在 Elixir 中,如何总结非 UTF-8 二进制文件中的所有字节?
- git - 如何一次接受 VSCode 中的所有当前更改?
- json - JSON - 多行字符串输出的数组错误 节点 | 节点请求
- c# - 将三角形对象保存在列表中并再次绘制
- ms-access-2013 - 在参数查询中输入日期条件的正确方法
- python - 列表元素与 pandas 列的关键字匹配
- apache-spark - 使用广泛的数据集 Spark 性能非常慢
- bootstrap-4 - boostrap 响应式导航边框
- angularjs - ADAL 登录和验证用户取决于 Microsoft 帐户登录/注销