我有一个用于列{id,eng,kurd,ar}的数据库,我已经从中填充了一个listview,它只显示数据库中的{eng}。我希望在用户点击列表视图中的项目时传递具有相同{id}的{eng,kurd,ar},但是当我点击某个项目时,我的应用程序崩溃,但其他一切正常,非非错误其他类[adapter,sqliteopenhelper]
MainActivity.class
package com.rawa.rawadict;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Adapter adapter;
ArrayList<Item> myList = new ArrayList<Item>();
ListView myListView;
Databasehelper myDbHelper = new Databasehelper(this);
String engRes,kurdRes,arRes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
myDbHelper.createDatabase();
myDbHelper.openDatabase();
} catch (IOException e) {
throw new Error("Unable to create database");
}
Cursor cursor = myDbHelper.QueryData("select * from zankodict");
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Item itemm = new Item();
itemm.setId(cursor.getString(0));
itemm.setEng(cursor.getString(1));
myList.add(itemm);
} while (cursor.moveToNext());
}
}
adapter = new Adapter(this, R.layout.single_row, myList);
myListView = (ListView) findViewById(R.id.lView);
myListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView text_id = (TextView) view.findViewById(R.id.tid);
String sel_id = text_id.getText().toString();
Cursor cursor = myDbHelper.QueryData("SELECT * FROM zankodict WHERE id =" + sel_id);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
engRes = cursor.getString(1);
kurdRes=cursor.getString(2);
arRes= cursor.getString(3);
} while (cursor.moveToNext());
}
}
// create intent to start another activity
Intent intent = new Intent(MainActivity.this, Result.class);
// add the selected text item to our intent.
Bundle extras = new Bundle();
extras.putString("eng",engRes);
extras.putString("kurd",kurdRes);
extras.putString("ar",arRes);
intent.putExtras(extras);
startActivity(intent);
}
}) ;
}
}
Result.class
package com.rawa.rawadict;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
public class Result extends AppCompatActivity {
TextView etView2,ktView2,atView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_view);
etView2 = (TextView) findViewById(R.id.etextView2);
ktView2 = (TextView) findViewById(R.id.ktextView2);
atView2 = (TextView) findViewById(R.id.atextView2);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String enG = extras.getString("eng");
String kurD = extras.getString("kurd");
String aR = extras.getString("ar");
etView2.setText(enG);
etView2.setText(kurD);
etView2.setText(aR);
}
}
我想将Bundle传递给Result.class
答案 0 :(得分:0)
将数据从活动传递到活动,特别是从数据库传递的最佳方法是传递一个(id)作为额外的意图,然后当您到达目标活动时,提取它并使用它来查询基于该数据库的数据库标识。
这将减少您必须通过意图实例传递的数据量。
如果你只想传递3个项目,你可以使用intent extras而不是像这样的Bundle:
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("eng",engRes);
intent.putExtra"kurd",kurdRes);
intent.putExtra("ar",arRes);
startActivity(intent);
在结果活动中提取此内容;
Intent intent = getIntent();
String enG = intent.getExtras().getString("eng");
String kurD = intent.getExtras().getString("kurd");
String aR = intent.getExtras().getString("ar");
我希望这有帮助并祝你好运!