我有两个旋转器(主题和章节),我有两个表(章节,主题) 我在主题微调器中添加所有主题名称,我需要在中添加所有章节名称 主题微调器中项目选择的基础。我的问题是如何做到这一点,因为我的 代码无法执行此操作。请给出一些提示或参考 这是我的示例代码。
onCreate()中的
addItemsOnSpinner1();
addListenerOnSpinnerItemSelection();
addItemsOnSpinner2();
addListenerOnSpinner2ItemSelection();
使用上面的调用函数我调用适当的方法。
private void addItemsOnSpinner1() {
try
{
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final List<ObjectiveWiseQuestion> LocWiseProfile= m.getAllSubject();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
list.add(cn.getSubjectName());
}
}
catch(Exception e)
{
e.printStackTrace();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subSpinner.setAdapter(dataAdapter);
}
private void addListenerOnSpinnerItemSelection()
{
subSpinner.setOnItemSelectedListener(new mySpinnerListener());
}
private void addItemsOnChapter()
{
try
{
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
//String name=owq.getSubjectName();
int id=db.getSubjectId(name);
owq.setSubjectId(id);
long subId=owq.getSubjectId();
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final List<ObjectiveWiseQuestion> LocWiseProfile= m.getAllChapter(subId);
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
list.add(cn.getChapterName());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
chapSpinner.setAdapter(dataAdapter);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void addListenerOnSpinner2ItemSelection()
{
try
{
chapSpinner.setOnItemSelectedListener(new mySpinnerListener2());
}
catch(Exception e)
{
e.printStackTrace();
}
}
这是我的听众课程。
class mySpinnerListener2 implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView parent, View v, int position,
long id)
{
Toast.makeText(parent.getContext(), "The chapter is " +
parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
owq.setChapterName(parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
class mySpinnerListener implements Spinner.OnItemSelectedListener
{
@Override
public void onItemSelected(AdapterView parent, View v, int position,
long id)
{
Toast.makeText(parent.getContext(), "The subject is " +
parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
owq.setSubjectName(parent.getItemAtPosition(position).toString());
name=parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
当我尝试编译它但它显示空值。给一些帮助。
提前致谢.......
找不到任何好的答案.....
MysqliteHelper.java
public List<ObjectiveWiseQuestion> getAllChapter(long subId)
{
List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
String selectQuery=("select chapterName from chapter where s_id ='"+ subId +"'");
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do {
ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();
owq.setChapterName(cursor.getString(2));
LocwiseProfileList.add(owq);
}
while(cursor.moveToNext());
db.close();
}
return LocwiseProfileList;
}
答案 0 :(得分:1)
在myspinnerlistener类中进行以下更改。
class mySpinnerListener implements Spinner.OnItemSelectedListener
{
Spinner chapSpinner;
public mySpinnerListener(Spinner chapSpinner){
this.chapSpinner = chapSpinner;
}
@Override
public void onItemSelected(AdapterView parent, View v, int position,
long id)
{
Toast.makeText(parent.getContext(), "The subject is " +
parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
owq.setSubjectName(parent.getItemAtPosition(position).toString());
name=parent.getItemAtPosition(position).toString();
// fetch the chaper names and set them on spinner
chapSpinner.setAdapter(Here pass your adapter);
}
@Override
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
这种变化也是
private void addListenerOnSpinnerItemSelection()
{
subSpinner.setOnItemSelectedListener(new mySpinnerListener(chapSpinner));
}
我认为这可以解决您的问题。