Edward P Matt Edward P Matt - 14 days ago 8
Android Question

Blank list activity, ListView object is not null

My ListedActivity is blank and throws no errors or exceptions. It just sits there, doing nothing at all.

My database should not be empty, there are some entries.

public class ListedActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null);
ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);
DBHelper dbHelper = new DBHelper(getApplicationContext());
ArrayList<MyLayoutObject> b = dbHelper.getAllResults();

MyAdapter a = new MyAdapter(this, b);

listView.setAdapter(a);

listView.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
Intent intent = new Intent(ListedActivity.this, ResultsActivity.class);
intent.putExtra("position", position);
intent.putExtra("id", id);
startActivity(intent);
}
}
);
}
}


Activity List should write out all of the results from my database. Here is it's .xml.

<?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_list"
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="smart.tuke.sk.makac.ListedActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:id="@+id/listedactivity_list_view" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/empty"
android:text="@string/no_data"
android:visibility="gone" />
</LinearLayout>


This is my row layout. Every row should look like this. Its .xml is below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="61dp"
android:orientation="vertical">

<TextView
android:id="@+id/date_recorded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#111111" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/duration_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/distance_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/pace_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/calories_recorded" />
</LinearLayout>

</LinearLayout>


MyLayoutObject is 5 strings with getters, nothing more

Help me, Overflow Kenobi, you're my only hope!

edit: adapter code

class MyAdapter extends BaseAdapter{

private LayoutInflater inflater;
private ArrayList<MyLayoutObject> objects;

private class ViewHolder {
TextView date;
TextView duration;
TextView distance;
TextView pace;
TextView calories;
}

MyAdapter(Context context, ArrayList<MyLayoutObject> objects) {
inflater = LayoutInflater.from(context);
this.objects = objects;
}

public int getCount() {
return objects.size();
}

public MyLayoutObject getItem(int position) {
return objects.get(position);
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_layout, null);
holder.date = (TextView) convertView.findViewById(R.id.date_recorded);
holder.distance = (TextView) convertView.findViewById(R.id.distance_recorded);
holder.duration = (TextView) convertView.findViewById(R.id.duration_recorded);
holder.pace = (TextView) convertView.findViewById(R.id.pace_recorded);
holder.calories = (TextView) convertView.findViewById(R.id.calories_recorded);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.date.setText(objects.get(position).getDate());
holder.duration.setText(objects.get(position).getDuration());
holder.distance.setText(objects.get(position).getDistance());
holder.pace.setText(objects.get(position).getPace());
holder.calories.setText(objects.get(position).getCalories());
return convertView;
}
}


And adapter's .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="61dp"
android:orientation="vertical">

<TextView
android:id="@+id/date_recorded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#111111" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/duration_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/distance_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/pace_recorded" />

<TextView
android:layout_width="345dp"
android:layout_height="wrap_content"
android:id="@+id/calories_recorded" />
</LinearLayout>

</LinearLayout>

Answer

You are trying to "find" an android.R view in your inflated custom view. The layout you are inflating does not contain this view (such views only appear inside predefined layouts, such as android.R.layout.simple_list_item)

Also, consider this line in your activity's XML: android:id="@android:id/list"

You are not really assigning an ID to the element. Do it like this: android:id="@+id/listedactivity_list_view" (the important bit is the @+id).

Now, in your activity, do this: ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);

By doing it like this, you are actually referencing an actual element inside your view instead of referencing some "ghost element" which doesn't exist in your layout.

EDIT: After re-reading your code more carefully, I've finally found the issue... Look at these lines:

setContentView(R.layout.activity_list);

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.activity_list, null);
ListView listView = (ListView) mContainer.findViewById(R.id.listedactivity_list_view);

In line 1 (of the snippet), you are setting the content view to a specific layout resource. However, in line 3 (mContainer), you are inflating this layout AGAIN - so when you do the findViewById in line 4, you are actually looking for something in the inflated view RATHER THAN the ACTUALLY shown view. So the solution to your issue is to remove the inflater and mContainer variable:

setContentView(R.layout.activity_list);

ListView listView = (ListView) findViewById(R.id.listedactivity_list_view);
Comments