Java 自定义ListView从Android Eclipse中的SqliteDatabase值设置背景图像
假设我创建了一个自定义列表视图来显示图像图标、标题和副标题。3视图的值将从sqlite数据库查询。 这是我的main.xml 所以,场景是,我想为Java 自定义ListView从Android Eclipse中的SqliteDatabase值设置背景图像,java,android,eclipse,sqlite,listview,Java,Android,Eclipse,Sqlite,Listview,假设我创建了一个自定义列表视图来显示图像图标、标题和副标题。3视图的值将从sqlite数据库查询。 这是我的main.xml 所以,场景是,我想为 -> wordTitle TextView is equal to KEY_NAME, -> wordSub TextView is equal to KEY_SUBTITLE, and to set the background image of -> word_icon ImageView is equal to KEY_
-> wordTitle TextView is equal to KEY_NAME,
-> wordSub TextView is equal to KEY_SUBTITLE, and to set the background image of
-> word_icon ImageView is equal to KEY_CODE value
所以listView看起来像
====================================
= ListView =
====================================
= word_icon = wordTitle = wordSub =
====================================
= p.png = Sam_Tit1 = Sam_Sub1 =
= x.png = Sam_Tit2 = Sam_Sub2 =
= n.png = Sam_Tit3 = Sam_Sub3 =
====================================
现在,这是我的主要活动,我将其命名为AndroidListViewCursorAdaptorActivity.Java
所以,一切都很好,一切正常,我设置了WordTitle和WordSub的文本,并将其绑定到我的ListView。
现在,我的问题是,我在设置ImageView的背景图像时遇到了一些问题,即word_图标。我想设置word_图标的背景图像
取决于按键代码值。我不知道该怎么做。我尝试用此代码更新我的mainActivity
private CountriesDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
private ImageView im;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new CountriesDbAdapter(this);
dbHelper.open();
displayListView(); // I call this function to set an Adapter for my ListView
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
CountriesDbAdapter.KEY_CODE, //Trying to fetch the KEY_CODE column
CountriesDbAdapter.KEY_NAME,
CountriesDbAdapter.KEY_SUBTITLE };
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.word_icon, //Trying to Put it here
R.id.wordTitle,
R.id.wordSub };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.custom,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
但是运气不好,这段代码不起作用,正如我预期的,因为我知道我需要设置背景图像
im.setBackgroundImage(R.Drawable.something_image);
但我有个主意,需要有人帮忙。
就这些,我希望有人能帮我。如果有的话,请你澄清一下。
提前谢谢 您需要编写自定义适配器谢谢您的回答,我遇到了一些麻烦,因为我只是一个新手。。。由于链接演示只是如何将listview的自定义适配器与imageView一起使用,因此我无法将其同步到我自己的,因为没有像我这样的sqlite数据库。。
====================================
= ListView =
====================================
= word_icon = wordTitle = wordSub =
====================================
= p.png = Sam_Tit1 = Sam_Sub1 =
= x.png = Sam_Tit2 = Sam_Sub2 =
= n.png = Sam_Tit3 = Sam_Sub3 =
====================================
public class AndroidListViewCursorAdaptorActivity extends Activity {
private CountriesDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new CountriesDbAdapter(this);
dbHelper.open();
displayListView(); // I call this function to set an Adapter for my ListView
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] { CountriesDbAdapter.KEY_NAME, CountriesDbAdapter.KEY_SUBTITLE };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.wordTitle, R.id.wordSub };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.custom,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}
private CountriesDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
private ImageView im;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbHelper = new CountriesDbAdapter(this);
dbHelper.open();
displayListView(); // I call this function to set an Adapter for my ListView
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllCountries();
// The desired columns to be bound
String[] columns = new String[] {
CountriesDbAdapter.KEY_CODE, //Trying to fetch the KEY_CODE column
CountriesDbAdapter.KEY_NAME,
CountriesDbAdapter.KEY_SUBTITLE };
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.word_icon, //Trying to Put it here
R.id.wordTitle,
R.id.wordSub };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
dataAdapter = new SimpleCursorAdapter(this, R.layout.custom,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
im.setBackgroundImage(R.Drawable.something_image);