There is a method:

//Метод формирования ListView Расходов private void setupLVtableCost() { //Формируем столбцы сопоставления String[] from = new String[]{db.COST_TABLE_NAME, db.COST_TABLE_SUMM, db.CURRENCY_TABLE_SHORT_NAME, db.CATEGORY_TABLE_NAME, db.WALLET_TABLE_NAME, db.COST_TABLE_DATE}; int[] to = new int[]{R.id.cost_lv_item_name, R.id.cost_lv_item_summ, R.id.cost_lv_item_currency, R.id.cost_lv_item_cat_name, R.id.cost_lv_item_wallet_name, R.id.cost_lv_item_date}; // создаем адаптер и настраиваем список scAdapterTableCost = new SimpleCursorAdapter(getActivity(), R.layout.item_cost, null, from, to, 0); //Привязываем адаптер в Листу lvCost.setAdapter(scAdapterTableCost); // добавляем контекстное меню к списку registerForContextMenu(lvCost); // создаем лоадер для чтения данных loaderCost = getActivity().getSupportLoaderManager().initLoader(Constants.LOADER_ID_TABEL_COST, null, this); } 

He works here no questions. It is necessary for the same example to build another list, "translation". The hitch is that I need information from the same field, but for different wallets (related to another table). It turns out you need to specify the field where to get the info and ID where to insert, but the field with the name of one. How to specify that in the first ID you need a name from the first key, and three times from the second?

 //В формировании курсора надо заполнить 2 массива - это не работает, как правильно? String[] from = new String[]{db.WALLET_TABLE_NAME, db.WALLET_TABLE_NAME, db.TRANSFER_TABLE_SUMM, db.CURRENCY_TABLE_SHORT_NAME}; int[] to = new int[]{R.id.trans_wall_name_one, R.id.trans_wall_name_sec, R.id.trans_summ, R.id.trans_wall_curr}; 

request:

 select * from transfer t, wallet w1, wallet w2 where t.wall_id_one=w1._id and t.wall_id_sec=w2._id 
  • show request. judging by the description, most likely you need pseudonyms of fields in the request, which are then used in your from . - Yura Ivanov
  • Added query, how to get to aliases? - Eugene Zaychenko
  • Need to open a star. aliases are set directly for specific fields select w1.id as w1id, w2.id as w2id... and then in String[] from = new String[]{"w1id","w2id"....} - Yura Ivanov
  • request in another class, in my method, how can I see ALIAS from another class? - Eugene Zaychenko
  • You did not understand. "Alias" is the alias of the field name in the selection ( see here , for example). In my example, the aliases are w1id and w2id . Your query with an asterisk select * needs to be rewritten, specifying an alias for each of your fields. Otherwise, SQLite duplicates the column names for you (two times id, two times name, etc.), the other subd would either give an error, either display only one column, or come up with an alias of name_1 themselves. - Yura Ivanov

2 answers 2

In your custom CursorLoader:

 @Override public Cursor loadInBackground() { Cursor cursor = db.rawQuery( "select w1.name as name_one, w2.name as name_sec, " +" t.summ, c.name as curr_name " +"from transfer t " +" join wallet w1 on t.wall_id_one=w1._id " +" join wallet w2 on t.wall_id_sec=w2._id " +" join currencies c on t.cur_id = c._id", null); return cursor; } 

The method of configuring the adapter (apparently in the fragment you have it):

 void setupLVTransfers(){ String[] from = new String[]{"name_one", "name_sec", "summ","curr_name"}; int[] to = new int[]{R.id.trans_wall_name_one, R.id.trans_wall_name_sec, R.id.trans_summ, R.id.trans_wall_curr}; scAdapterTransfers = new SimpleCursorAdapter(getContext(), R.layout.item_transfer, null, from, to, 0); lvTransfers.setAdapter(scAdapterTransfers); // etc... } 

The names name_one , name_sec , curr_name are called aliases of the fields in the selection, with their help you can select columns from different tables with the same name, in the selection the names of the columns will be renamed into aliases. You can also sew them into constants, if it is somehow useful.

  • Works!! Thank! - Eugene Zaychenko

You can’t do it in the framework of SimpleCursorAdapter - it's Simple , that is, simple:

An XML file adapter

Write your implementation and in the bindView() method bindView() the data that you want the adapter to show you.

It is not as difficult as it seems. You can simply take the source code of SimpleCursorAdapter and tweak them a bit as you need.

  • I thought about that. thank. - Eugene Zaychenko