添加Intent以过滤文本

时间:2015-09-03 19:54:31

标签: android search

我正在制作Android歌曲应用。一切都很好......除了搜索部分。歌曲存储在MySQLite数据库中,当我打开一首歌时,它会正常打开。但是当我搜索一首歌然后尝试从搜索结果中打开这首歌时,它不会打开实际的歌曲,但它会打开另一首歌。例如,如果我搜索这首歌,古代词,它会打开另一首歌,一个荣耀之地的家。

这是我的MainActivity:

package com.swm.empire.psalmsongbook;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
private EditText filterText;
private ArrayAdapter<String> listAdapter;

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

    filterText = (EditText)findViewById(R.id.editText);
    ListView itemList = (ListView)findViewById(R.id.listView);

    DbBackend dbBackend = new DbBackend(MainActivity.this);
    String[] terms = dbBackend.dictionaryWords();

    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, terms);

    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            // make Toast when click
            Toast.makeText(getApplicationContext(), "Song " + position, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, SongbookActivity.class);
            intent.putExtra("SONG._ID",position);
            startActivity(intent);
        }
    });

    filterText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            MainActivity.this.listAdapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

这是SongbookActivity:

package com.swm.empire.psalmsongbook;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.Locale;

/**
 * Created by VinceGee on 8/4/2015.
 */
public class SongbookActivity extends ActionBarActivity {


private TextView lyrics;

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

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    int dictionaryId = bundle.getInt("SONG._ID");
    int id = dictionaryId + 1;

    TextView title = (TextView)findViewById(R.id.title);
    lyrics = (TextView)findViewById(R.id.lyrics);

    DbBackend dbBackend = new DbBackend(SongbookActivity.this);
    SongObject allQuizQuestions = dbBackend.getQuizById(id);

    title.setText(allQuizQuestions.getWord());
    lyrics.setMovementMethod(new ScrollingMovementMethod());
    lyrics.setText(allQuizQuestions.getDefinition());

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_songbook, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPause() {
    if(convertToSpeech != null){
        convertToSpeech.stop();
        convertToSpeech.shutdown();
    }
    super.onPause();
}
}

如果我从过滤结果中点击它,我希望能够查看正确的歌曲

0 个答案:

没有答案