我正面临Android app.So的问题。我有3个活动用OnItemClickListener一个接一个地打开。从第一个活动到第二个活动我没有遇到任何麻烦,因为我只需要在第二个活动中加载listview中的ArrayList。第三个活动是在第二个活动中点击listview中的一个项目而在第三个活动中打开我要从中打开数据我点击项目的数据库。 这是一些代码:
1.第二项活动:
DatabaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewlistcontents_layout);
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if(data.getCount() == 0){
Toast.makeText(this, "There are no contents in this list!",Toast.LENGTH_LONG).show();
}else{
while(data.moveToNext()){
theList.add(data.getString(1));
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ViewListContents.this, ElevatorInfo.class);
startActivity(intent);
}
});
}
2.我希望从数据库中获取已打开的项目ID的第三个活动,并从数据库加载第三列但不知道如何执行此操作,这是我的实际问题。此外,我做同样的事情在第二个活动中只是为了尝试从列表视图中的第三列加载信息,但它也无法正常工作。
DatabaseHelper myDB;
@Override
ListView listView = (ListView) findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getListContents();
if (data.getCount() == 0) {
Toast.makeText(this, "There are no contents in this list!", Toast.LENGTH_LONG).show();
} else {
while (data.moveToNext()) {
theList.add(data.getString(2));
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
listView.setAdapter(listAdapter);
}
}
}
public void AddData(String newEntry) {
boolean insertData = myDB.addData2(newEntry);
if(insertData == true){
Toast.makeText(this, "Data Successfully Inserted!", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Something went wrong :(.", Toast.LENGTH_LONG).show();
}
}
}
3.我的DatabaseHelper类。
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + MyDatabase.ElevatorEntry.TABLE_NAME + " ( " + MyDatabase.ElevatorEntry.COL1 + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
MyDatabase.ElevatorEntry.COL2 + "TEXT" + MyDatabase.ElevatorEntry.DATE + " TEXT " + " )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF TABLE EXISTS " + MyDatabase.ElevatorEntry.TABLE_NAME);
onCreate(db);
}
public boolean addData(String item1) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyDatabase.ElevatorEntry.COL2, item1);
long result = db.insert(MyDatabase.ElevatorEntry.TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public boolean addData2(String item2) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyDatabase.ElevatorEntry.DATE, item2);
public Cursor getListContents(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + MyDatabase.ElevatorEntry.TABLE_NAME, null );
return data;
}
}
答案 0 :(得分:1)
你在问题中写了很多代码,专注于你想问的问题
旁注:为什么会这样
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
在while循环中?当循环结束并且所有项目都已添加到列表theList
while(data.moveToNext()){
theList.add(data.getString(1));
}//while loop
ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
现在,对于您的问题,onItemClick()
使用position
从theList
获取该项目,然后随意执行任何操作:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected = theList.get(position);
Intent intent = new Intent(ViewListContents.this, ElevatorInfo.class);
intent.putExtra("SELECTED_ITEM",selected);
startActivity(intent);
}
现在您可以在意图中添加selected
作为额外内容并开始下一个活动。
修改强>
有关如何从第二个活动中读取值。检查this answer
你需要使用getIntent().getStringExtra("SELECTED_ITEM");
。