Reputation: 105
I have a custom listview. when te listview is called i want it to load all the profiles from my database, this works. but i also want to check the status of the profile and depending of the result i want to show a certain icon next to it. but i cant seem to figure out how to get this done properly.. this is what i have so far:
profiles_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_above="@+id/menuBtn"
android:background="#2cffffff">
</ListView>
<RelativeLayout/>
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/statusIcon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"/>
</TableRow>
</TableLayout>
MainActivity.class
public class ProfilesList extends CredenceOneActivity implements AdapterView.OnItemClickListener {
static final int STATUS_SUCCES =1;
static final int STATUS_WARNING =2;
static final int STATUS_ERROR =3;
private ListView list;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList;
private DB_Handler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
setContentView(R.layout.profiles_list);
}
list = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
try {
db = new DB_Handler(this);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
// Here, you set the data in your ListView
list.setAdapter(adapter);
list.setOnItemClickListener(this);
loadRegisteredProfiles();
}
public void loadRegisteredProfiles() {
List<DB_Profiles> storedProfiles = db.getAllProfiles();
for (DB_Profiles profile : storedProfiles) {
String name= profile.getNAME();
arrayList.add(name);
adapter.notifyDataSetChanged();
}
i want to do something like this for each profile that is loaded in the listview:
DB_Profiles profile;
if (profile.getStatus==STATUS_SUCCES{
statusIcon.setImageResource(R.drawable.succes);}
else{profile.getStatus==STATUS_ERROR{
statusIcon.setImageResource(R.drawable.error);}}
}
any advice would be greatly appreciated!
Upvotes: 0
Views: 66
Reputation: 257
In the getView() of your Custom Adapter write the below piece of code.
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowLayout, null);
holder = new ViewHolder();
holder.imagView = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (profile.getStatus==STATUS_SUCCES)
{
holder.imagView.setImageResource(R.drawable.succes);
}
else if(profile.getStatus==STATUS_ERROR)
{
holder.imagView.setImageResource(R.drawable.error);
}
return convertView;
And declare static inner class in your custom adapter, like this
static class ViewHolder {
ImageView img;
}
Upvotes: 2
Reputation: 12996
In the getView() of the listview's adapter, you can use the setImageResource() of the imageView, in your list_item layout.
Upvotes: 0