Android开发:列表项搜索与音频不匹配

时间:2013-02-18 14:13:06

标签: android

我在eclipse中有这个代码,我有很多列表项,我只是缩短了代码。这里的问题是,每当我搜索某个项目时,语音音频就不再与该项目匹配。我想知道我是否可以为列表项分配索引。它甚至可能吗?

public class IndexActivity extends Activity implements OnClickListener{

    Button btnHome;
    Button btnBack;
    Button btnHelp;
    Button btnCateg;

     // List view
    private ListView lv1;

    // Listview Adapter
    ArrayAdapter<String> adapter1;

    // Search EditText
    EditText inputSearch1;

    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList1;

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_index);

            TabHost th = (TabHost) findViewById (R.id.tabhost);
            th.setup();

            TabSpec ts = th.newTabSpec("tag1");
            ts.setContent(R.id.tab1);
            ts.setIndicator("Chavacano - English");
            th.addTab(ts);

            ts = th.newTabSpec("tag2");
            ts.setContent(R.id.tab2);
            ts.setIndicator("English - Chavacano");
            th.addTab(ts);

            btnHome = (Button) findViewById(R.id.btnHome);
            btnHome.setOnClickListener(this);
            btnBack = (Button) findViewById(R.id.btnBack);
            btnBack.setOnClickListener(this);
            btnHelp = (Button) findViewById(R.id.btnHelpFooter);
            btnHelp.setOnClickListener(this);
            btnCateg = (Button) findViewById(R.id.btnCategoriesFooter);
            btnCateg.setOnClickListener(this);


            // Listview Data
            final String products1[] = {
            "January - Enero",
            "February - Pebrero",
            "March - Marso",
            "April - Abril",
            "May - Mayo",
            "June - Hunyo",
            "July - Hulyo",
            "August - Agosto",
            "September - Setyembre",
            "October - Oktubre",
            "November - Nobyembre",
            "December - Disyembre"
                    };




            lv1 = (ListView) findViewById(R.id.list_view1);
            inputSearch1 = (EditText) findViewById(R.id.txtSearch1);

            // Adding items to listview
            adapter1 = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products1);
            //adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.btnSound);
            lv1.setAdapter(adapter1); 

          //Audio
            lv1.setOnItemClickListener(new OnItemClickListener() {
                   public void onItemClick(AdapterView<?> a, View v, int position, long id)
                  { 
                       //oneoclock
                       if (lv1.getItemIdAtPosition(position) == 0)
                       {
                           MediaPlayer january = MediaPlayer.create(IndexActivity.this, R.raw.oneoclock);
                           january.start();
                       }
                       //February
                       else if (lv1.getItemIdAtPosition(position) == 1)
                       {
                           MediaPlayer february = MediaPlayer.create(IndexActivity.this, R.raw.fiveoclock);
                           february.start();
                       }
                       //March
                       else if (lv1.getItemIdAtPosition(position) == 2)
                       {
                           MediaPlayer march = MediaPlayer.create(IndexActivity.this, R.raw.below);
                           march.start();
                       }
                       //April
                       else if (lv1.getItemIdAtPosition(position) == 3)
                       {
                           MediaPlayer april = MediaPlayer.create(IndexActivity.this, R.raw.open);
                           april.start();
                       }
                       //May
                       else if (lv1.getItemIdAtPosition(position) == 4)
                       {
                           MediaPlayer may = MediaPlayer.create(IndexActivity.this, R.raw.abio);
                           may.start();
                       }
                       //June
                       else if (lv1.getItemIdAtPosition(position) == 5)
                       {
                           MediaPlayer june = MediaPlayer.create(IndexActivity.this, R.raw.aboard);
                           june.start();
                       }
                       //July
                       else if (lv1.getItemIdAtPosition(position) == 6)
                       {
                           MediaPlayer july = MediaPlayer.create(IndexActivity.this, R.raw.april);
                           july.start();
                       }
                       //August
                       else if (lv1.getItemIdAtPosition(position) == 7)
                       {
                           MediaPlayer august = MediaPlayer.create(IndexActivity.this, R.raw.boring);
                           august.start();
                       }
                       //September
                       else if (lv1.getItemIdAtPosition(position) == 8)
                       {
                           MediaPlayer september = MediaPlayer.create(IndexActivity.this, R.raw.take);
                           september.start();
                       }
                       //October
                       else if (lv1.getItemIdAtPosition(position) == 9)
                       {
                           MediaPlayer october = MediaPlayer.create(IndexActivity.this, R.raw.achara);
                           october.start();
                       }
                       //November
                       else if (lv1.getItemIdAtPosition(position) == 10)
                       {
                           MediaPlayer november = MediaPlayer.create(IndexActivity.this, R.raw.remember);
                           november.start();
                       }
                       //December
                       else if (lv1.getItemIdAtPosition(position) == 11)
                       {
                           MediaPlayer december = MediaPlayer.create(IndexActivity.this, R.raw.inside);
                           december.start();
                       }

                  }
            });



            inputSearch1.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    IndexActivity.this.adapter1.getFilter().filter(cs);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {}

                @Override
                public void afterTextChanged(Editable arg0) {} 
            });
      }         


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.btnHome){
            startActivity(new Intent(this, HomescreenActivity.class));
            MediaPlayer button = MediaPlayer.create(IndexActivity.this, R.raw.button);
            button.start();
        }
        else if (v.getId() == R.id.btnBack){
            finish();
            MediaPlayer button = MediaPlayer.create(IndexActivity.this, R.raw.button);
            button.start();
        }
        else if (v.getId() == R.id.btnHelpFooter){
            startActivity(new Intent(this, HelpActivity.class));
            MediaPlayer button = MediaPlayer.create(IndexActivity.this, R.raw.button);
            button.start();
        }
        else if (v.getId() == R.id.btnCategoriesFooter){
            startActivity(new Intent(this, CategoriesActivity.class));
            MediaPlayer button = MediaPlayer.create(IndexActivity.this, R.raw.button);
            button.start();
        }   
    }   
}

1 个答案:

答案 0 :(得分:-1)

项目不再匹配的原因是因为你正在使用getItemIdAtPosition Android文档不多说它但我不会依赖它,因为这似乎是问题的来源。我建议使用position(可能我从未使用适配器的过滤器功能)或使用lv1.getItemAtPosition(position)并且应该返回适​​配器中的选定项(在本例中为字符串)并使用确定你要播放的声音。

您还可以创建一个自定义类,其中包含要显示的字符串和要播放的声音引用,然后由getItemAtPosition返回,您可以使用媒体播放器的声音参考。那样你就不需要这个巨人如果阻止了。