我想在我的应用程序中完成一项任务,该任务是使用我创建的B
从我的sqlite数据库中获取项目ID,并且这次我想使用在活动中获取的ID(不是适配器,而是我)知道如何从适配器执行此操作。虽然我是第一次尝试,但我什至不知道是否可能。
这是我尝试过的代码
5
Model.class
private ViewPager mViewPager;
ArrayList<Model> modelArrayList;
ArrayList<String>listItem;
RecyclerView recyclerView;
SongAdapter adapter;
int id = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
listItem = new ArrayList<>();
SQLiteDbHelper dbHelper = new SQLiteDbHelper(this);
dbHelper.openDatabase();
modelArrayList = dbHelper.getDetails();
adapter = new SongAdapter(modelArrayList);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_search){
Intent intent = new Intent(MainActivity.this,SearchActivity.class);
startActivity(intent);
}
if (item.getItemId() == R.id.lacate){
goToChapter();
}
if (item.getItemId() == R.id.locate){
Intent intent = new Intent(MainActivity.this,ChaptersActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
private void goToChapter() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.AlertDialog);
builder.setTitle("Enter Here :");
final EditText goTo = new EditText(MainActivity.this);
goTo.setHint("input number");
builder.setView(goTo);
builder.setPositiveButton("GO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String locate = goTo.getText().toString();
if (!TextUtils.isEmpty(locate) ){
if (!locate.equals(String.valueOf(modelArrayList.get(id).getChapter()))) {
Toast.makeText(MainActivity.this,"bad",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"good",Toast.LENGTH_LONG).show();
}
}
}
});
builder.show();
}
但是id仅注册为零,并且不会从数据库中检索任何内容