本文介绍了如何在基于第一微调对第二微调新增项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个微调(Subject和章),我的箱子两条表(第二章,主题)。结果
我的主题微调添加所有科目的名字,我需要添加的结果的所有章节名称
在主题飞旋项目选择的依据。我的问题是,如何处理,因为我的结果
code是无法做到这一点。请给一些提示或引用。结果
下面是我的示例code。

在的onCreate()

  addItemsOnSpinner1();
    addListenerOnSpinnerItemSelection();
    addItemsOnSpinner2();
    addListenerOnSpinner2ItemSelection();

使用调用函数我叫适当的方法上面。

 私人无效addItemsOnSpinner1(){
    尝试
    {
        最后MySQLiteHelper M =新MySQLiteHelper(getBaseContext());
        最终名单< ObjectiveWiseQuestion> LocWiseProfile = m.getAllSubject();        对于(最终ObjectiveWiseQuestion CN:LocWiseProfile)
        {
            list.add(cn.getSubjectName());        }
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }        ArrayAdapter<串GT; DataAdapter的=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,清​​单);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            subSpinner.setAdapter(DataAdapter的);} 私人无效addListenerOnSpinnerItemSelection()
{
    subSpinner.setOnItemSelectedListener(新mySpinnerListener());}
私人无效addItemsOnChapter()
{
    尝试
    {        DB =新MySQLiteHelper(getBaseContext());
        db.getWritableDatabase();
        //字符串名称= owq.getSubjectName();
        INT ID = db.getSubjectId(名);
        owq.setSubjectId(ID);
        长SUBID = owq.getSubjectId();
        最后MySQLiteHelper M =新MySQLiteHelper(getBaseContext());
         最终名单< ObjectiveWiseQuestion> LocWiseProfile = m.getAllChapter(SUBID);        对于(最终ObjectiveWiseQuestion CN:LocWiseProfile)
        {            list.add(cn.getChapterName());
        }
        ArrayAdapter<串GT; DataAdapter的=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_spinner_item,清​​单);
            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            chapSpinner.setAdapter(DataAdapter的);
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }}
    私人无效addListenerOnSpinner2ItemSelection()
{
    尝试
    {
        chapSpinner.setOnItemSelectedListener(新mySpinnerListener2());
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }}

下面是我的监听器类。

 类mySpinnerListener2实现Spinner.OnItemSelectedListener
{
    @覆盖
    公共无效onItemSelected(适配器视图父母,视图V,INT位置,
            长ID)
    {
        Toast.makeText(parent.getContext()的章是+
                  parent.getItemAtPosition(位置)的ToString(),Toast.LENGTH_LONG).show();
        owq.setChapterName(parent.getItemAtPosition(位置)的ToString());    }    @覆盖
    公共无效onNothingSelected(适配器视图父)
    {
        // 没做什么。
    }}
类mySpinnerListener实现Spinner.OnItemSelectedListener
{
    @覆盖
    公共无效onItemSelected(适配器视图父母,视图V,INT位置,
            长ID)
    {
        Toast.makeText(parent.getContext()的主题是+
                  parent.getItemAtPosition(位置)的ToString(),Toast.LENGTH_LONG).show();
        owq.setSubjectName(parent.getItemAtPosition(位置)的ToString());
        名称= parent.getItemAtPosition(位置)的ToString();    }    @覆盖
    公共无效onNothingSelected(适配器视图父)
    {
        // 没做什么。
    }}

当我试图编译它,但它显示空值。给予一定的帮助。结果
提前致谢.......
没有找到任何好的答案.....

MysqliteHelper.java

 公开名单< ObjectiveWiseQuestion> getAllChapter(长SUBID)
    {
     清单< ObjectiveWiseQuestion> LocwiseProfileList =新的ArrayList< ObjectiveWiseQuestion>();
     字符串selectQuery =(从章选择CHAPTERNAME,其中S_ID ='+ SUBID +');
     SQLiteDatabase分贝= this.getWritableDatabase();
        光标光标= db.rawQuery(selectQuery,NULL);        //通过所有行循环,并增加列表
        如果(cursor.moveToFirst())
        {
            做{
                ObjectiveWiseQuestion owq =新ObjectiveWiseQuestion();
                owq.setChapterName(cursor.getString(2));
                LocwiseProfileList.add(owq);
              }
            而(cursor.moveToNext());
            db.close();
        }        返回LocwiseProfileList;
    }


解决方案

不要在您的myspinnerlistener类以下更改。

 类mySpinnerListener实现Spinner.OnItemSelectedListener
    {
    微调chapSpinner;
        公共mySpinnerListener(微调chapSpinner){
    this.chapSpinner = chapSpinner;
        }
        @覆盖
        公共无效onItemSelected(适配器视图父母,视图V,INT位置,
                长ID)
        {
            Toast.makeText(parent.getContext()的主题是+
                      parent.getItemAtPosition(位置)的ToString(),Toast.LENGTH_LONG).show();
            owq.setSubjectName(parent.getItemAtPosition(位置)的ToString());
            名称= parent.getItemAtPosition(位置)的ToString();
            //获取村庄和集镇名称和设置它们微调
    chapSpinner.setAdapter(这里通过你的适配器);
        }        @覆盖
        公共无效onNothingSelected(适配器视图父)
        {
            // 没做什么。
        }    }

而这种变化也

 私人无效addListenerOnSpinnerItemSelection()
{
    subSpinner.setOnItemSelectedListener(新mySpinnerListener(chapSpinner));}

我认为这将解决您的问题。

I have Two spinner(Subject and Chapter),I crate two table(Chapter,subject).
I add all subject name in subject spinner and i need to add all chapter name on the
basis of item selection in subject spinner. My question is that how to that,because my
code is not able to do this. Please give some hint or reference.
Here is my sample code.

in onCreate()

    addItemsOnSpinner1();
    addListenerOnSpinnerItemSelection();
    addItemsOnSpinner2();
    addListenerOnSpinner2ItemSelection();

using above calling function i call appropriate method.

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();
    }

}

Here is my Listener Class.

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.
    }

}

When i am trying to compile it but it show null value. Give some help.
Thanks in Advance.......Not find any good answer.....

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;
    }
解决方案

Do the following change in your myspinnerlistener class.

   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.
        }

    }

And this change also

 private void addListenerOnSpinnerItemSelection()
{
    subSpinner.setOnItemSelectedListener(new mySpinnerListener(chapSpinner));

}

I think this will solve your problem.

这篇关于如何在基于第一微调对第二微调新增项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:48