我正在使用列表视图活动将数据发送到编辑数据活动,这里有什么问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我正在使用列表视图活动将数据发送到编辑数据活动,这里有什么问题?相关的知识,希望对你有一定的参考价值。
我从数据库中检索项目名称,项目描述和项目价格,当用户点击项目时,它将进入用户可以更新或删除数据的编辑活动,但是当我点击列表项目时,它会将我带回主要活动
当用户点击视图数据时,它会在单击列表视图后显示列表视图,用户应该移动到编辑数据活动,其中编辑数据活动数据将显示在编辑文本框中以进行更新或删除
我的代码运行正常,有一个参数项名称,但是当我使用项目描述和项目价格时,就会出现此错误
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
private ListView mListView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()){
listData.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
String des = adapterView.getItemAtPosition(i).toString();
String pri = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name, des, pri); //get the id associated with that name
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent intent = new Intent(ListDataActivity.this, EditDataActivity.class);
Bundle extras = new Bundle();
extras.putInt("id",itemID);
extras.putString("name",name);
extras.putString("description",des);
extras.putString("price",pri);
intent.putExtras(extras);
startActivity(intent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
新的更新代码,我做了以下更改
package com.example.saveanddisplaysql;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class ListDataActivity extends AppCompatActivity {
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
private ListView mListView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_data);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
Cursor data = mDatabaseHelper.getData();
class Model {
String name;
String desc;
int price;
}
ArrayList< Model> listData = new ArrayList<>();
while(data.moveToNext()){
Model model = new Model();
model.name = data.getString(1);
model.desc = data.getString(2);
model.price = data.getInt(3);
listData.add(model); }
ListAdapter adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Model model = (Model)adapterView.getItemAtPosition(i);
String name = model.name;
String des = model.des;
String pri = model.pri;
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name, des, pri); //get the id associated with these
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent intent = new Intent(ListDataActivity.this, EditDataActivity.class);
Bundle extras = new Bundle();
extras.putInt("id",itemID);
extras.putString("name",name);
extras.putString("description",des);
extras.putString("price",pri);
intent.putExtras(extras);
startActivity(intent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
}
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
logcat的:
2019-04-11 10:51:34.331 32717-32717 /?我/艺术:迟到-Xcheck:jni 2019-04-11 10:51:34.351 32717-32717 /? D / Tida提供者:TidaProvider()2019-04-11 10:51:34.364 32717-32717 / com.example.saveanddisplaysql W / ReflectionUtils:java.lang.NoSuchMethodException:android.os.MessageQueue #enableMonitor()#bestmatch at miui。在miui.util.ReflectionUtils.tryCallMethod的miui.util.ReflectionUtils.callMethod(ReflectionUtils.java:800)的miui.util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:375)上的util.ReflectionUtils.findMethodBestMatch(ReflectionUtils.java:338) (ReflectionUtils.java:818)在android.app.AaseThread.main的android.os.Laseper.prepareMainLooper(Looper.java:112)上的android.os.BaseLooper.enableMonitor(BaseLooper.java:47)(ActivityThread.java: 6221)位于com.android.internal.os.ZygoteInit.main的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)的java.lang.reflect.Method.invoke(Native Method) ZygoteInit.java:794)
您的“listData”ArrayList是一个String类型的数组列表。所以你只能在其中添加字符串。你需要创建一个类
class Model {
String name;
String desc;
int price;
}
然后创建模型的数组列表。
ArrayList< Model> listData = new ArrayList<>();
然后将该模型ArrayList传递给适配器,并且可以在需要时获取。
以上是关于我正在使用列表视图活动将数据发送到编辑数据活动,这里有什么问题?的主要内容,如果未能解决你的问题,请参考以下文章
如何将列表数据从活动 A 发送到活动 B 并在活动 B 中编辑数据