Call database with (this) in fragment

0

I am trying to make a catalog by linking to a database that is in the folder assets, so that it is displayed in a recycleview. This activity already works on its own but now that I want to embed it in a fragment I can not (I'm new to this). Does anyone know how to adapt to fragment ??

public class MainActivity extends AppCompatActivity {

private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
public static ArrayList<DictObjectModel> data;
DatabaseHelper db ;
ArrayList<String> nombrelist;
ArrayList<String> descripcionlist;
LinkedHashMap<String,String> namelist;
SearchView searchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);
    db= new DatabaseHelper(this);
    searchView = (SearchView) findViewById(R.id.searchView);
    searchView.setQueryHint("Buscar");
    searchView.setQueryRefinementEnabled(true);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    data = new ArrayList<DictObjectModel>();
    fetchData();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return  false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            newText = newText.toLowerCase();

            final ArrayList<DictObjectModel> filteredList = new ArrayList<DictObjectModel>();

            for (int i = 0; i < nombrelist.size(); i++) {

                final String text = nombrelist.get(i).toLowerCase();
                if (text.contains(newText)) {

                    filteredList.add(new DictObjectModel(nombrelist.get(i),descripcionlist.get(i)));
                }
            }
            adapter = new CustomAdapter(filteredList);
            recyclerView.setAdapter(adapter);


            return true;
        }
    });
}

public void fetchData()
{
    db =new DatabaseHelper(this);
    try {

        db.createDataBase();
        db.openDataBase();

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }


    namelist=new LinkedHashMap<>();
    int ii;
    SQLiteDatabase sd = db.getReadableDatabase();
    Cursor cursor = sd.query("Productos" ,null, null, null, null, null, null);
    ii=cursor.getColumnIndex("nombre");
    nombrelist=new ArrayList<String>();
    descripcionlist= new ArrayList<String>();
    while (cursor.moveToNext()){
        namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("descripcion")));
    }
    Iterator entries = namelist.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry thisEntry = (Map.Entry) entries.next();
        nombrelist.add(String.valueOf(thisEntry.getKey()));
        //QUITAR el -
        descripcionlist.add(""+String.valueOf(thisEntry.getValue()));
    }

    for (int i = 0; i < nombrelist.size(); i++) {
    data.add(new DictObjectModel(nombrelist.get(i), descripcionlist.get(i)));
    }

    adapter = new CustomAdapter(data);
    recyclerView.setAdapter(adapter);
}

}

Thank you.

    
asked by Cesar Perez 17.10.2017 в 06:56
source

1 answer

1

Recently I have had problems with the fragments for issues like yours if I have not misunderstood.

I'll give you some examples and I hope they will help you solve your problem:

If for example, you have a Toast in your Main activity:

Toast.makeText(this, "Estoy en el Main Activity", Toast.LENGTH_SHORT).show();

As you will see, this is used.

If that same Toast you want to put in a fragment:

Toast.makeText(getContext(), "Ahora estoy en el Fragment", Toast.LENGTH_SHORT).show();

You will be able to verify that the this, has changed by getContext ()

On the other hand, if what you need is to work with the View, in the fragment it apparently does not detect it.

If you notice, when creating a fragment, android-studio creates it with the following code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_vestidos, container, false);;
}

To get to work with the view, then it would be as easy as doing the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.fragment_vestidos, container, false);

    /*Tu codigo aquí*/

    return v;
}

In this way when you need the view, your view will be the variable v

I hope you get the examples I gave you ^ _ ^

    
answered by 09.02.2018 в 11:23