Armen Arzumanyan Armen Arzumanyan -4 years ago 256
Android Question

Android: ListView does not show items

I have simple list from database and want to show just in simple list view.

public class FindAllJobs extends ListActivity {

private JobOperations operations;
ArrayList<JobModel> jobs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_all_jobs);
operations = new JobOperations(this);
operations.open();
jobs = operations.getJobListBySearchKey("python");
operations.close();
//ListView listView = (ListView)findViewById(R.id.list);
//JobAdapter adapter = new JobAdapter(this,jobs);

ArrayAdapter<JobModel> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, jobs);
//listView.setAdapter(adapter);
setListAdapter(adapter);

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_find_all_jobs"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="jobs.find.com.finddevjobs.models.FindAllJobs">


<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>


</LinearLayout>


But list show always
com.packages.JobModel@dfdfdfdfdfdfdfd
, why?
Where is a mistake? Even created custom adapter but result is the same.
I am new in Android, just first ListView from db:) . Db part works fine.

enter image description here
This is adapter, but actually it is not finished, I do not understand should I return view or list? :(

public class JobAdapter extends ArrayAdapter<JobModel> {
public JobAdapter(Context context, ArrayList<JobModel> jobs) {
super(context, 0, jobs);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

JobModel job = getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_find_all_jobs, parent, false);
}


return convertView;
}


}

Answer Source

The mistake is, that the ListView gets a List of of Objects. But it don't know how it should name them. There are 2 solutions to this problem:

The easy one
Add the overridden toString() method inside your JobModel and define there what the labeling in the ListView should look like and return this.

Something like this

 @Override
 public String toString(){
   return "Name: " + this.getName + "  Date: " + this.getDate();
 }

The complex one that can be modified
Create a custom ListViewAdapter taking the information your want to display from your JobModel. There are many examples on the internet how create such an adapter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download