В БД:
NAME text
IMG text
....
IMG - это url изображения
NoteList.java
Код: Выделить всё
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class SNoteList extends Activity {
ListView lvData;
DB db;
SimpleCursorAdapter scAdapter;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notelist);
// открываем подключение к БД
db = new DB(this);
db.open();
// получаем курсор
cursor = db.getAllData();
startManagingCursor(cursor);
// формируем столбцы сопоставления
String[] from = new String[] { DB.COLUMN_NAME, DB.COLUMN_IMG };
int[] to = new int[] { R.id.n_name, R.id.n_img,};
// создааем адаптер и настраиваем список
scAdapter = new ImageCursorAdapter(this, R.layout.item, cursor, from, to);
lvData = (ListView) findViewById(R.id.lvData);
lvData.setAdapter(scAdapter);
...
}
}
Код: Выделить всё
<?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:background="#ffffff"
android:orientation="vertical" >
<ListView
android:id="@+id/lvData"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Код: Выделить всё
<?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="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/n_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/n_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp" />
</LinearLayout>
Код: Выделить всё
private void showNotes() {
Intent intent = new Intent(this, SNoteList.class);
startActivity(intent);
}
Код: Выделить всё
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// managedQuery takes care of the lifecycle of the cursor.
// it will close the cursor itself.
// However, the use of managedQuery is discouraged and Loaders are now
// the prefered way of loading data in activity. It is a topic for the
// future.
Cursor cursor = managedQuery(ClubCP.CONTENT_URI, null, null, null, null);
// adapter to show the data. Go through android docs about SimpleCursorAdapter.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.club_row, cursor, UI_BINDING_FROM,
UI_BINDING_TO);
// We set the view binder for the adapter to our own CustomViewBinder.
// The code for the custom view binder is below.
adapter.setViewBinder(new CustomViewBinder());
// Provides the cursor for the list view. The list view should be defined
// in main.xml
setListAdapter(adapter);
}
Код: Выделить всё
private class CustomViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(ClubCP.KEY_IS_STAR)) {
// If the column is IS_STAR then we use custom view.
int is_star = cursor.getInt(columnIndex);
if (is_star != 1) {
// set the visibility of the view to GONE
view.setVisibility(View.GONE);
}
return true;
}
// For others, we simply return false so that the default binding
// happens.
return false;
}
}