samedi 23 janvier 2016

EditText for filtering/searching listView

I am new to programming and with help of a tutorial i created an application, where you can create shelfs and store these in a sqlite database. With help of a listview the shelfs will be displayed. Now I set an EditText on Top of the ListView and with stackoverflow I tried to implement a "dynamic filtering". I thought this would be possible with the "addTextChangedListener" but it didnt work at all. I tried almost 10 different methods and 4 different other tutorials, spent day and almost nights by trying but nothing worked. Hopefully you guys can help me.

thanks

Following my Code of the DataListActivity:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.kasutwentyseven.gui4selfshelf.R;
import com.kasutwentyseven.gui4selfshelf.Scans.ScanActivity;

public class DataListActivity extends Activity {
public ListView listView;
public SQLiteDatabase sqLiteDatabase;
public ShelfDBHelper shelfDBHelper;
public Cursor cursor;
public EditText inputSearch;
public ListDataAdapter listDataAdapter; //DataSource - Adapter

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

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
    TextView myTextView = (TextView) findViewById(R.id.text_yourshelfs);
    myTextView.setTypeface(myTypeface);

    listView = (ListView) findViewById(R.id.list_view);
    listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout); //ArrayAdapter

    inputSearch = (EditText)findViewById(R.id.editText_search_shelf);


    listView.setAdapter(listDataAdapter);
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            listDataAdapter.getFilter().filter(s);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent n = new Intent(getApplicationContext(), ScanActivity.class);
            n.putExtra("postition", position);
            startActivity(n);

shelfDBHelper = new ShelfDBHelper(getApplicationContext());
    sqLiteDatabase = shelfDBHelper.getReadableDatabase();
    cursor = shelfDBHelper.getInformations(sqLiteDatabase);
    if (cursor.moveToFirst()) {
        do {
            String name;

            name = cursor.getString(0);

            DataProvider dataProvider = new DataProvider(name);
            listDataAdapter.add(dataProvider);
        } while (cursor.moveToNext());
    }


}

And the Code of my Adapter:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.kasutwentyseven.gui4selfshelf.R;

import java.sql.SQLDataException;
import java.util.ArrayList;
import java.util.List;

public class ListDataAdapter extends ArrayAdapter{
private List list= new ArrayList();
private static final String TAG = "Shelf ADAPTER";
private ShelfDBHelper shelfDBHelper;
private SQLiteDatabase sqLiteDatabase;

public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}
static class LayoutHandler
{
    TextView NAME;
}
@Override
public void add(Object object){
    super.add(object);
    list.add(object);
}
@Override
public int getCount(){
    return list.size();
}

@Override
public Object getItem(int position){
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    LayoutHandler layoutHandler;
    if(row == null){
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler = new LayoutHandler();
        layoutHandler.NAME = (TextView)row.findViewById(R.id.text_shelf_name);
        row.setTag(layoutHandler);
    }else{
        layoutHandler = (LayoutHandler) row.getTag();

    }

    DataProvider dataProvider = (DataProvider)this.getItem(position);
    layoutHandler.NAME.setText(dataProvider.getName());

    return row;
}

Aucun commentaire:

Enregistrer un commentaire