如何从sqlite数据库获取字符串到autocompletetextview

时间:2018-02-23 15:30:27

标签: android sqlite autocompletetextview

我正在开发一个关于android的sqlite数据库项目。这是我的第一个sqlite项目。我阅读了很多文章并创建了我的应用程序,但是有一个问题我无法找到解决问题的方法。我希望将NAME_FILTER表格转换为autoCompleteTextView

这是我的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:text="@string/customers_name"
            android:textSize="16sp" />

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="5"/>

        <EditText
            android:id="@+id/edt_filter_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionNext"
            style="@style/EditText.Input"
            android:theme="@style/EditText.Input"/>



    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="20dp"
        android:paddingBottom="20dp">

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_submit"
            android:textColor="#333333"
            android:background="@drawable/buttonshape"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:textSize="18sp"
            android:drawableStart="@drawable/ic_check_black_24dp"/>

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_cancel"
            android:textColor="#333333"
            android:background="@drawable/red_button_background"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"

            android:drawableStart="@drawable/ic_cancel_black_24dp"/>



    </LinearLayout>

</LinearLayout>

这是我的活动

public class FiltersActivity extends AppCompatActivity implements ActionMode.Callback{

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
    private static int Filter_list_position = -1;
    private Button btnSubmit;
    private Button btnCancel;
    private ListView listView;
    private FilterListAdapter adapter;
    private EditText edtName;
    private int seletcedFilterId;
    ActionMode mActionMode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.filters_title);
        setContentView(R.layout.activity_filters);

        btnCancel = (Button) findViewById(R.id.btn_cancel);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        edtName = (EditText) findViewById(R.id.edt_filter_name);
        btnCancel.setOnClickListener(onCancelbuttonClicked);
        listView = (ListView) findViewById(R.id.listView);


        //Creating the instance of ArrayAdapter containing list of fruit names
        ArrayAdapter<String> adapterr = new ArrayAdapter<String>
                (this, android.R.layout.select_dialog_item, NAME_FILTER);

        //Getting the instance of AutoCompleteTextView
        AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

        actv.setThreshold(1);//will start working from first character
        actv.setAdapter(adapterr);//setting the adapter data into the AutoCompleteTextView



        refreshList();

        if(getSupportActionBar() != null){
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        }
    }

DataBaseHelper Cllas

public class DatabaseHelper {

    private SQLiteDatabase mydb;

    public DatabaseHelper(Context context){
        mydb = new MyDatabase(context).getWritableDatabase();
    }



    public void addNewFilter(filter filter){
        ContentValues values = new ContentValues();
        values.put("name", filter.getName());

        mydb.insert(MyDatabase.tableFilters, null, values);
        mydb.close();
    }

    public List<filter> getListOfFilters(){

        Cursor c = mydb.rawQuery("select * from " + MyDatabase.tableFilters, null);
        List<filter> filters = new ArrayList<>();

        while (c.moveToNext()){

            filter em = new filter();
            em.setId(c.getInt(c.getColumnIndex(MyDatabase.ID_FILTER)));
            em.setName(c.getString(c.getColumnIndex(MyDatabase.NAME_FILTER)));

            filters.add(em);
        }

        c.close();
        mydb.close();
        return filters;
    }

    public void editFilter(filter filter){
        ContentValues values = new ContentValues();
        values.put("name", filter.getName());

        mydb.update(MyDatabase.tableFilters, values, "id = " + filter.getId(), null);
        mydb.close();
    }

    public void deleteFilter(filter filter){
        mydb.delete(MyDatabase.tableFilters, "id = " + filter.getId(), null);
        mydb.close();
    }

    public List<filter> searchFilterByName(String name){

        Cursor c = mydb.rawQuery("select * from " + MyDatabase.tableFilters + " where name like '%" + name + "%'", null);
        List<filter> filters = new ArrayList<>();

        while (c.moveToNext()){

            filter em = new filter();
            em.setId(c.getInt(c.getColumnIndex(MyDatabase.ID_FILTER)));
            em.setName(c.getString(c.getColumnIndex(MyDatabase.NAME_FILTER)));

            filters.add(em);
        }

        c.close();
        mydb.close();
        return filters;
    }

}

这是我的ListAdapter类

class FilterListAdapter extends BaseAdapter {

    private Context context;
    private List<filter> filters;

    FilterListAdapter(Context context, List<filter> filters){
        this.context = context;
        this.filters = filters;
    }

    @Override
    public int getCount() {
        return filters.size();
    }

    @Override
    public Object getItem(int i) {
        return filters.get(i);
    }

    @Override
    public long getItemId(int i) {
        return filters.get(i).getId();
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {

        View rowView = LayoutInflater.from(context).inflate(R.layout.filter_list_item, viewGroup, false);
        TextView txtName = (TextView) rowView.findViewById(R.id.txt_filter_name);
        txtName.setText(filters.get(position).getName());


        return rowView;
    }
}

0 个答案:

没有答案