1

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

1 回答 1

-1

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

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

于 2013-02-18T15:50:55.193 回答