如何从SimpleCursorAdapter的选定项中获取字符串?

发布于 2024-10-23 22:16:25 字数 5355 浏览 2 评论 0原文

我使用 AutoCompleteTextView 在用户键入要搜索的输入字符串时建议用户从我的 sqlite 数据库中输入一些单词。

我尝试使用 simple_list_item_2 使建议看起来友好,这是我的代码:

package com.suit.kamus;

import android.app.Activity;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SuitAuto extends Activity implements TextWatcher{
AutoCompleteTextView auto; TextView result;
Button search; Button add; Spinner chooser;
String input; static String selection; String main;
String[] options = {"en to ina", "ina to en"};
SimpleCursorAdapter simple;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    auto = (AutoCompleteTextView)findViewById(R.id.auto);
    auto.addTextChangedListener(this);

    result = (TextView)findViewById(R.id.result);

    chooser = (Spinner)findViewById(R.id.chooser);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, options);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    chooser.setAdapter(adapter);

    KamusDbAdapter dbHelper = new KamusDbAdapter(getApplicationContext());
    dbHelper.open();
    String status = dbHelper.getstatedb();
    selection = status;
    dbHelper.close();

    if (selection.equalsIgnoreCase("en")){
        chooser.setSelection(0);
    } else {chooser.setSelection(1);}

    Log.d("statelang", selection);

    add = (Button)findViewById(R.id.add);
    add.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startAdding(main);
        }

    });

    chooser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            if (chooser.getSelectedItemId() == 0){
                selection = "en";
                select();
                updateDb();
            }else{
                selection = "ina";
                select();
                updateDb();
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    auto.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            main = auto.getText().toString();
            //Log.v("CURSOR",finish);
            result.setText("");
            auto.setText("");
        }
    });


}

public void startAdding(String dWord) {
    // TODO Auto-generated method stub
    KamusDbAdapter adding = new KamusDbAdapter(getApplicationContext());
    adding.open();
    adding.Favorite(dWord);
    adding.close();
}

protected void updateDb() {
    // TODO Auto-generated method stub
    KamusDbAdapter save = new KamusDbAdapter(getApplicationContext());
    save.open();
    save.updatestatedb(selection);
    save.close();
}


public static String select() {
    // TODO Auto-generated method stub
    Log.v("STRING",selection);
    return selection;
}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub

}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    input = auto.getText().toString();

    KamusDbAdapter x = new KamusDbAdapter(getApplicationContext());
    x.open();
    Cursor cur = x.getCall(input, selection);
    //getCall is in KamusDbAdapter class, it used to return result cursor from sqlite db
    //i use rawQuery "SELECT * FROM en_to_ina WHERE word LIKE 'input%'"
    x.close();

    String[] displayFields = new String[] {"word", "meaning"};

    int[] displayViews = new int[] { android.R.id.text1,android.R.id.text2 };

    simple = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cur,displayFields, displayViews);

    auto.setAdapter(simple); 

}
}

我在从单击的项目中检索字符串时遇到问题。它位于:

auto.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            main = auto.getText().toString();
            //Log.v("CURSOR",finish);
            result.setText("");
            auto.setText("");
        }
    });

我需要单词和含义字段中的字符串。 任何回应都会很棒...

I'm using an AutoCompleteTextView to suggest user some words from my sqlite db as they type the input string to search.

I try to make the suggestion look friendly by using simple_list_item_2, here's my code:

package com.suit.kamus;

import android.app.Activity;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SuitAuto extends Activity implements TextWatcher{
AutoCompleteTextView auto; TextView result;
Button search; Button add; Spinner chooser;
String input; static String selection; String main;
String[] options = {"en to ina", "ina to en"};
SimpleCursorAdapter simple;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    auto = (AutoCompleteTextView)findViewById(R.id.auto);
    auto.addTextChangedListener(this);

    result = (TextView)findViewById(R.id.result);

    chooser = (Spinner)findViewById(R.id.chooser);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, options);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    chooser.setAdapter(adapter);

    KamusDbAdapter dbHelper = new KamusDbAdapter(getApplicationContext());
    dbHelper.open();
    String status = dbHelper.getstatedb();
    selection = status;
    dbHelper.close();

    if (selection.equalsIgnoreCase("en")){
        chooser.setSelection(0);
    } else {chooser.setSelection(1);}

    Log.d("statelang", selection);

    add = (Button)findViewById(R.id.add);
    add.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startAdding(main);
        }

    });

    chooser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            if (chooser.getSelectedItemId() == 0){
                selection = "en";
                select();
                updateDb();
            }else{
                selection = "ina";
                select();
                updateDb();
            }
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    auto.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            main = auto.getText().toString();
            //Log.v("CURSOR",finish);
            result.setText("");
            auto.setText("");
        }
    });


}

public void startAdding(String dWord) {
    // TODO Auto-generated method stub
    KamusDbAdapter adding = new KamusDbAdapter(getApplicationContext());
    adding.open();
    adding.Favorite(dWord);
    adding.close();
}

protected void updateDb() {
    // TODO Auto-generated method stub
    KamusDbAdapter save = new KamusDbAdapter(getApplicationContext());
    save.open();
    save.updatestatedb(selection);
    save.close();
}


public static String select() {
    // TODO Auto-generated method stub
    Log.v("STRING",selection);
    return selection;
}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub

}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    input = auto.getText().toString();

    KamusDbAdapter x = new KamusDbAdapter(getApplicationContext());
    x.open();
    Cursor cur = x.getCall(input, selection);
    //getCall is in KamusDbAdapter class, it used to return result cursor from sqlite db
    //i use rawQuery "SELECT * FROM en_to_ina WHERE word LIKE 'input%'"
    x.close();

    String[] displayFields = new String[] {"word", "meaning"};

    int[] displayViews = new int[] { android.R.id.text1,android.R.id.text2 };

    simple = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cur,displayFields, displayViews);

    auto.setAdapter(simple); 

}
}

I got a problem with retrieving the string from clicked item. It is in:

auto.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            main = auto.getText().toString();
            //Log.v("CURSOR",finish);
            result.setText("");
            auto.setText("");
        }
    });

I need both strings from word and meaning fields.
Any response would be great...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-10-30 22:16:25

您需要使用 http://developer.android.com/参考/android/widget/CursorAdapter.html#getItem(int)

Cursor cursor = (Cursor) simple.getItem(position);
// retrieve the data from the cursor

You need to use http://developer.android.com/reference/android/widget/CursorAdapter.html#getItem(int).

Cursor cursor = (Cursor) simple.getItem(position);
// retrieve the data from the cursor
ヤ经典坏疍 2024-10-30 22:16:25

这似乎是 2.1 上 SimpleCursorAdapter 的问题,在 2.2 上光标位于请求的项目上,您可以检索列数据,但在 2.1 中光标位置为 0 并且 cursor.move (itemIndex)cursor.moveToFirst() 都返回 false。

我计划做一个 RYO 数据库适配器。

This seems to be a problem with SimpleCursorAdapter on 2.1, on 2.2 the cursor is positioned on the requested item and you can retrieve the column data but in 2.1 the cursor position is 0 and cursor.move(itemIndex) and cursor.moveToFirst() both return false.

I plan to do a RYO database adapter.

爱你是孤单的心事 2024-10-30 22:16:25

使用游标从 SimpleCurstor Adapter 获取字符串

    @Override
public boolean onSuggestionClick(int position) {
    //First get cursor from your Adapter,
    Cursor cursor = (Cursor) mAdapter.getItem(position);
    /* Then get string data from cursor's column, I am getting it from column 0 in this case. You can use your own column index. */
    String s=cursor.getString(0);
    //Then set the searchview or autotextview with that string
    mSearchView.setQuery(s, true); //true will submit the query 
}

Getting string from SimpleCurstor Adapter using cursor

    @Override
public boolean onSuggestionClick(int position) {
    //First get cursor from your Adapter,
    Cursor cursor = (Cursor) mAdapter.getItem(position);
    /* Then get string data from cursor's column, I am getting it from column 0 in this case. You can use your own column index. */
    String s=cursor.getString(0);
    //Then set the searchview or autotextview with that string
    mSearchView.setQuery(s, true); //true will submit the query 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文