问题
Good afternoon everyone,
I have a quick question. I have a query result that I store in a list/array and I am trying to display the appropriate image in the imageview. The query provides a name of the image that is stored in the resource folder(res/drawable). I am getting error syntax. I am not sure how to solve this issue. I have a database that stores the name of the image in a field (database) called spic.
I have tried this code:
how to work with images and database in android?
but it doesnt work in this class. I am not sure is it because this class is "extends ArrayAdapter {" instead of "extends Activity {"
here is my code:
zCustomUsersAdapter class:
package com.example.yao;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class zCustomUsersAdapter extends ArrayAdapter<YAOYVD> {
public zCustomUsersAdapter(Context context, List<YAOYVD> users) {
super(context, 0, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// User user = getItem(position);
YAOYVD user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.zitem_user, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ImageView tvImage = (ImageView) convertView.findViewById(R.id.ivUserIcon);
// Populate the data into the template view using the data object
tvName.setText(String.valueOf(user.getID_YAO()));
//.name);
tvHome.setText(String.valueOf(user.getNAME_YAO()));
//.hometown);
// tvImage.setBackgroundResource( getResourceID (String.valueOf(user.getSPIC_YAO()), "drawable",getApplicationContext() ));
// Return the completed view to render on screen
tvImage.setBackgroundResource(user.getSPIC_YAO()); //getSpic_YAO this is string in the YAO class. getSpic_YAO retrive the name of the image in the drawable folder.
return convertView;
}
}
zCustomListActivity
package com.example.yao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
public class zCustomListActivity extends Activity {
private YAOGetDataSource2 datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.zactivity_custom_list);
//calling the method to create the database from asset folder
copydbfromassest();
// datasource = new YAODeckListDataSource(this);
datasource = new YAOGetDataSource2(this);
datasource.open();
populateUsersList();
}
private void populateUsersList() {
// Construct the data source
//List<YAOYVD> values = datasource.SQLYVDTABLESEARCH();
List<YAOYVD> arrayOfUsers = datasource.SQLYVDTABLESEARCH();
//.getUsers();
// Create the adapter to convert the array to views
zCustomUsersAdapter adapter = new zCustomUsersAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvUsers);
listView.setAdapter(adapter);
}
//@Override
public void onClick(View view) {
// TODO Auto-generated method stub
}
//database open and close
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
protected void onDestroy(){
datasource.close();
super.onDestroy();
}
/****
* new things to considered
*/
///copy the database from assest folder
private void copydbfromassest() {
// TODO Auto-generated method stub
YAOMySQLiteHelper myhelper = new YAOMySQLiteHelper (this);
try{
//create datbase
myhelper.importIfNotExist();
}catch (IOException e){
throw new Error("Unable to Create the Database ");
}
try{
myhelper.openDataBase();
}catch (SQLException sqle){
throw sqle;
}
}
}
回答1:
You need to retrieve the resource identifier associated with that image's name. For this, you will need to get your app's Resources, which requires a context.
Context ctx = getContext();
int resId = ctx.getResources().getIdentifier(user.getSPIC_YAO(), "drawable", ctx.getPackageName());
if(resId != 0){
tvImage.setBackgroundResource(resId);
}
来源:https://stackoverflow.com/questions/26851592/how-to-assing-an-image-to-imageview-based-on-a-query-results