I am changing the question at the root, how can I display the data in a list with a view so that each field is separate? I deduce in this way, but the information is all stored in one line of the View.

public class RequestHistoryListActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.history_database_request); DatabaseOpenHelper dbhelper = new DatabaseOpenHelper(getBaseContext()); SQLiteDatabase sqliteDB = dbhelper.getReadableDatabase(); String[] from = { HistoryColumns.REQUEST, BaseColumns._ID }; final Cursor c = sqliteDB.query(History.TABLE_NAME, from, null, null, null, null, null); int[] to = new int[] { R.id.text1 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list, c, from, to); ListView lv = (ListView) findViewById(R.id.listView1); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { // c.moveToPosition(pos); int rowId = c.getInt(c.getColumnIndexOrThrow(BaseColumns._ID)); // Uri outURI = Uri.parse(data.toString() + rowId); /* * Intent outData = new Intent(); History oneRow = new History(); * oneRow.setId(c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID))); * oneRow.setRequest(c.getString(c.getColumnIndexOrThrow(HistoryColumns.REQUEST))); * setResult(Activity.RESULT_OK, outData); finish(); */ Log.v(MainActivity.TAG, rowId); } }); } } 


 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> 

and then what we deduce


 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is text1" android:textSize="25dip" /> </LinearLayout> </LinearLayout> 
  • one
    SimpleCursorAdapter not suitable? Or do custom CursorAdapter. In your code, I do not see where you bind to the ListView data - rasmisha
  • hm.put (BOOKKEY, readHistory (getBaseContext ()). toString ()); This is the line - dajver
  • one
    If I honestly see for the first time that the ArrayList is overtaken from the cursor and passed as a parameter to the SimpleAdapter In the CursorAdapter (SimpleCursorAdapter in particular), one of the parameters for the OnItemClick event is just the necessary id (it’s not difficult to make the desired Uri with it) - rasmisha
  • one
    Why, I'm trying to understand what the problem is. I just didn’t display it either when the TextView was in a container, but using a SimpleCursorAdapter. - rasmisha
  • one
    Yes, ID is> OnItemClick (AdapterView <?> Parent, View view, int position, long id) Inherit from AdapterView.OnItemClickListener AND> listView.setOnItemClickListener (this); - rasmisha

2 answers 2


Create something like this

 Cursor cursor = getCursor() // любой метод возвращающий курсор SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, R.layout.[layout_name], // ваш лэйаут, можно взять стандартный из android.R new String[] { DBMetaData.FirstFieldName, DBMetaData.SecondFieldName }, // имена полей new int[] { R.id.tvFirstField, R.id.tvSecondField } // id текствьюх в выбранном лэйауте ); 
  • I did not understand about any method returning the cursor, what is this method? Samopisny? Or some specific? - dajver
  • one
    It can be either self-written or getContentResolver (). managaQuery (...), for example, the main thing is that the required fields should be this line could be stopped> Cursor cursor = sqliteDB.query (History.TABLE_NAME, columnsToTake,> null, null, null, null,> History.DEFAULT_SORT); - rasmisha
  • one
    Something does not occur. Isn't black text on black?) Is there anything in the database? (you can run the cursor on the value in the log) - rasmisha
  • one
    and could show xml and line adapter constructor? - rasmisha
  • one
    Try replacing> android: id = "@ + id / text1" with> android: id = "@ android: id / text1" Well, respectively, in the code, too, with android.R.id.text1 And why do you need the LinearLayout ( more than 2) You can make the same TextView (once it is one). - rasmisha
  if (cursor.moveToFirst()) { list = new ArrayList<History>(); } 

you move the cursor to the beginning - it is not necessary to do this, it is better to check the courses for zero ...

  for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { 

replaced by

  while (cursor.moveToNext()) { 

 Cursor cursor = sqliteDB.query(History.TABLE_NAME, new String[] { "count(*)" }, null, null, null, null, History.DEFAULT_SORT); if (cursor.moveToFirst()) { countRows = cursor.getInt(0); if (LOGV) { Log.v(TAG, "Count in history table" + String.valueOf(countRows)); } 

In general, all unnecessary, Count can be checked by looking at the size of the cursor.


here you sethesh the HistoryColumns.REQUEST field in the oneRow object

then you have to do something like

 String str = oneRow.getRequest(); 

collect all this into an array and transfer it to an adapter sheet - everything will be displayed ... then I do not think that this will solve your problem

  • Thank you, but on the question can I suggest something? How do I display the Request column in a ListView? - dajver
  • one
    @dajver does not allow writing anywhere) I will write here. Why> int rowId = c.getInt (c.getColumnIndexOrThrow (BaseColumns._ID)); rather than> rowId = (int) id; and does the numeric types appear in the log? - rasmisha
  • one
    long id in the method must be equal to the id in the database for the selected element - rasmisha
  • one
    Well, duck and create Uri> content: //your.application/history/ [id] And send the request and read the data you need. Or a request with the condition BaseColumns._ID + "=" + id - rasmisha
  • one
    @dajver intent.setData (Uri uri) is the same In the host, getIntent (). getData () - rasmisha