It is necessary to display a list from the database in the format simple_list_item_2 . In this system layout there are two textview . An application using this layout crashes. However, if you use your own layout , which contains one textview , the application starts and displays everything, but of course not in the right format. I understand that it is necessary to prescribe somehow that from the ArrayList 'resulting from the database, one field corresponds to the 1st textview , and another field the 2nd textview from the simple_list_item_2 . Saw examples with redefinition, but the class is used there, and I have a DB. Please tell me how to solve such problems.

 public class IdevOwnSqliteDbActivity extends ListActivity { private static final String DB_NAME = "yourdb.sqlite3"; private static final String TABLE_NAME = "friends"; private static final String FRIEND_ID = "_id"; private static final String FRIEND_NAME = "name"; private static final String FRIEND_POEM = "poem"; private SQLiteDatabase database; private ListView listView; private ArrayList<String> friends; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME); database = dbOpenHelper.openDataBase(); fillFreinds(); setUpList(); } private void setUpList() { setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, friends)); //R.layout.my_list, friends)); listView = getListView(); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position,long id) { Toast.makeText(getApplicationContext(), ((TextView) view).getText() + " could be iDev's friend", Toast.LENGTH_SHORT).show(); } }); } private void fillFreinds() { friends = new ArrayList<String>(); Cursor friendCursor = database.query(TABLE_NAME, new String[] {FRIEND_ID, FRIEND_NAME, FRIEND_POEM}, null, null, null, null , FRIEND_NAME); friendCursor.moveToFirst(); if(!friendCursor.isAfterLast()) { do { String name = friendCursor.getString(1); String poem = friendCursor.getString(2); friends.add(name); friends.add(poem); } while (friendCursor.moveToNext()); } friendCursor.close(); } } 

    1 answer 1

    Most likely the problem is that the ArrayAdapter will not work with the second textview , try this way.

     private SQLiteDatabase db; private Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // если вы используете ListActivity не нужно прикреплять макет. ListView listView = getListView(); ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME); database = dbOpenHelper.openDataBase(); try { db = dbOpenHelper.getReadableDatabase(); cursor = db.query("friends", new String[] {"_id", "name", "poem"}, null, null, null, null, null); ListAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_2, cursor, new String[]{"name", "poem"}, new int[]{android.R.id.text1, android.R.id.text2}); listView.setAdapter(adapter); } catch (SQLiteException e) { Toast.makeText(this, "База данных недоступна", Toast.LENGTH_SHORT).show(); } } @Override public void onListItemClick(ListView listView, View itemView, int position, long id) { Toast.makeText(YourActivity.this, "position == " + position, Toast.LENGTH_SHORT).show(); } 
    • I do not understand) No where and what context, list, getName () and getAge (). It is clear that I should lead to my code, but since I started to learn only that obvious things are not always obvious to me. Please do not judge strictly. - Shura Balaganov
    • for example, in Klimov in the first example, the link is what is needed, but I don’t understand how to tie it to my case from the database. - Shura Balaganov
    • Updated the code, now it seems more clearly - Tommy
    • You open the database at the very beginning, then query the tables, then create simpleCursorAdater and transfer data from the DB to it, then, in the onListItemClick method onListItemClick implement what will happen when you click on any of the items - Tommy
    • Fine! It all worked! The design of the SimpleCursorAdapter I had to arrange correctly. Thank! - Shura Balaganov