我在数据库ID中有一列,当单击列表视图项时,我想获取此ID。但是,当我运行app> error时:不幸的是,应用程序已停止! :(请帮助我!
MainActivity.java
public class MainActivity extends AppCompatActivity {
DBHelper dbHelper;
ListView listView;
ArrayList<TuDienAVModel> list_Word;
TuDienAVAdapter tuDienAVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DBHelper(this);
listView = findViewById(R.id.listView);
list_Word = new ArrayList<>();
list_Word = dbHelper.getAnhViet();
tuDienAVAdapter = new TuDienAVAdapter(MainActivity.this,
R.layout.layout_item,
list_Word);
listView.setAdapter(tuDienAVAdapter);
// listView = ( ListView ) findViewById (R.id. listView );
// list_Word = new ArrayList <TuDienAVModel>() ;
// list_Word . addAll ( dbHelper . getAnhViet ());
// lvAdapter = new LVAdapter (this , R. layout . layout_item , list_Word );
// listView. setAdapter ( lvAdapter );
listView. setOnItemClickListener ( new AdapterView. OnItemClickListener
(){
public void onItemClick ( AdapterView <?> arg0 , View arg1 ,
int arg2 , long arg3 ) {
Intent intent = new Intent ( MainActivity .this ,
DetailTuDienActivity. class );
TuDienAVModel av = (TuDienAVModel) listView.getAdapter().getItem(arg2);
Toast.makeText(MainActivity.this, av.getId() + " selected", Toast.LENGTH_LONG).show();
intent . putExtra ("id", av.getId());
startActivity ( intent );
}
});
registerForContextMenu(listView);
}
TuDienAVAdapter.java
public class TuDienAVAdapter extends ArrayAdapter<TuDienAVModel> {
Context context;
int layout;
ArrayList<TuDienAVModel> arrayList;
ArrayList<TuDienAVModel> arrayListFilter;
int layout_id ;
public TuDienAVAdapter(@NonNull Context context, int resource, @NonNull ArrayList<TuDienAVModel> model) {
super(context, resource, model);
this.context = context;
this.layout = resource;
this.arrayList = model;
this.arrayListFilter = model;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(layout,null);
TuDienAVModel tuDienAVModel=arrayList.get(position);
Log.d("ABC", position+"");
TextView txt_word = convertView.findViewById(R.id.textView_Word);
txt_word.setText(tuDienAVModel.getEnglish());
return convertView;
}
@Override
public int getCount() {
return arrayList.size();
}
DBHelper.java
public TuDienAVModel detailTuVung ( int _id ) {
db = context.openOrCreateDatabase(DATABASE_NAME,context.MODE_PRIVATE,null);
String s = " SELECT * FROM tbl_AnhViet where id=" +_id;
Cursor cursor = db. rawQuery (s, null );
cursor . moveToFirst ();
TuDienAVModel av = new TuDienAVModel (cursor . getInt (0) ,
cursor . getString (1) ,cursor . getString (2) );
cursor . close ();
db . close ();
return av ;
}
DetailTuDienActivity.java
public class DetailTuDienActivity extends AppCompatActivity {
WebView webView;
TextView textView;
DBHelper db;
TuDienAVModel tuDienAVModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super . onCreate ( savedInstanceState );
setContentView (R. layout.activity_detail_tu_dien );
int id = getIntent (). getIntExtra ("id", 0);
db = new DBHelper ( getApplicationContext ());
TuDienAVModel av = db.detailTuVung(id);
if(av != null ){
TextView txtEn = ( TextView ) findViewById (R.id.textView_English
);
txtEn . setText ( av.getEnglish());
}
}
我在数据库ID中有一列,当单击列表视图项时,我想获取此ID。但是,当我运行app> error时:不幸的是,应用程序已停止! :(请帮助我!