-1

私は多くの単語を使用する予定の辞書アプリケーションを作成しています (約 1 万ルピー) 単語の意味とともにリストに表示しています。

カスタム配列アダプターを使用して listView に入力し、リストビューでカスタム レイアウトを膨らませています。

これが私のシナリオです:

1>ユーザーが探している単語を入力できるedittextがあります。2>ユーザーが単語を入力したら、たとえばR、最初の文字が R のリストビューの選択を設定したい 、ユーザーが次の作業を入力したら、REとしましょう、リストビューの位置をリストの位置に変更する必要があります最初の 2 文字はREです。

これを実装するために、addTextchanged Listner を edittext に追加し、アダプタをフィルタリングしましたが、機能していません。何かを入力すると、リストが空になります。

より良いアプローチを提案してください。または、次の機能を実装するにはどうすればよいですか。

どんな助けでも大歓迎です。

どうもありがとう。

4

3 に答える 3

1

あなたの要件を正しく理解している場合は、入力した文字に基づいてリストをスクロールすることで、ユーザーが辞書アプリケーションで行う入力を最小限に抑えたいと考えています。
編集テキストの代わりにオート コンプリート テキスト ビューを使用して、同じ結果を得ることができます。
ユーザーがいくつかの文字を入力すると、Android はそれらの単語で始まるすべての単語を表示します。
ユーザーが正しい単語を選択すると、リストをその位置までスクロールできます。これのサンプルコードは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <AutoCompleteTextView
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="3" />

</LinearLayout>

public class AutoCompleteActivity extends Activity 実装 OnItemClickListener{

private AutoCompleteTextView edit;

private static final String[] items={"lorem", "ipsum", "dolor",
    "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi",
    "vel", "ligula", "vitae", "arcu", "aliquet", "mollis","etiam",
    "vel", "erat", "placerat", "ante", "porttitor", "sodales", 
    "pellentesque", "augue", "purus"};

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    edit=(AutoCompleteTextView)findViewById(R.id.edit);
    edit.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line,
            items));

    edit.setOnItemClickListener(this);
}

@Override
public void onItemClick (AdapterView<?> parent, View v, int position, long Id)
{
    Toast.makeText(AutoCompleteActivity.this, items[position], Toast.LENGTH_SHORT).show();
    // scroll the list to the correct position
    //listView.setSelection(position);
}

}


ここでは、アダプターが autoCompleteText ビューに関連付けられており、ユーザーが入力する
と、それらの文字で始まるすべての単語がドロップダウン リストに表示され、ユーザー
がリスト内の任意の単語をクリックすると、onItemClick メソッドが呼び出されます。

これが役立つことを願っています

于 2012-08-11T14:03:24.657 に答える
0

ここであなたに2つの提案があります。
1、 と一緒に使用Search InterfaceContent Providerます。この方法は少し複雑ですが、マネージャーの大きなデータにはより効果的です。これDictionary sampleにより、2 つの検索オプションが提供されます
- キーワードの入力と、一致項目を含む提案リストビューのドロップダウン。クリックして単語の意味を取得するだけです
-完全ではないキーワードを入力し、拡大鏡アイコンを押すと、相対一致アイテムのリストビューが返されます。

サンプルをダウンロードしてインポートするか、単にCreate a sample projectインしてサンプルEclipseを選択できますSearchableDictionary

2、貼り付けてEdittext濾すListView。私たちがあなたを助けることができるようにあなたのコードを追加してください。

私の意見AutoCompleteTextViewでは、ListView以下は便利ではありませんUser。ドロップダウンリストビューを提案しました(AutoCompleteTextView)。ユーザーは、下のListViewで再度クリックするのではなく、提案をクリックしたときに単語の意味を取得することを期待しています(単語の意味を取得するには2回クリックします)。表示語の意味をAutoCompleteTextView持つが適しているようです。TextView

于 2012-08-11T14:53:01.133 に答える
0
public class SearchableDictionary extends Activity {

    private TextView mTextView;
    private ListView mListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mTextView = (TextView) findViewById(R.id.text);
    mListView = (ListView) findViewById(R.id.list);

    Intent intent = getIntent();

    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        // handles a click on a search suggestion; launches activity to show word
        Intent wordIntent = new Intent(this, WordActivity.class);
        wordIntent.setData(intent.getData());
        startActivity(wordIntent);
        finish();
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        // handles a search query
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);
    }
}

/**
 * Searches the dictionary and displays results for the given query.
 * @param query The search query
 */
private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        // There are no results
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        // Display the number of results
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        mTextView.setText(countString);

        // Specify the columns we want to display in the result
        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        // Specify the corresponding layout elements where we want the columns to go
        int[] to = new int[] { R.id.word,
                               R.id.definition };

        // Create a simple cursor adapter for the definitions and apply them to the ListView
        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.result, cursor, from, to);
        mListView.setAdapter(words);

        // Define the on-click listener for the list items
        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Build the Intent used to open WordActivity with a specific word Uri
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);
            }
        });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

}

于 2016-11-18T07:05:46.643 に答える