I'm learning to fill ListView
using ArrayList
. I've populated the ArrayList
inside the doPostExecute()
of GetParentVitalsName()
using
vitalList.add(parentVitalName);
Logcat shows no error. But the List is not displaying. I'm only getting a blank page. I found that it's not going inside the getView
method of MyListAdapter
. what am I doing wrong here?
acitivity_vital_list.xml
contains the ListView
and vital_list_layout
contains the item template
public class VitalListActivity extends ActionBarActivity {
private List<String> vitalList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vital_list);
Intent intent = getIntent();
String status = intent.getStringExtra(LoginActivity.EXTRA_MESSAGE);
if(status.equals("true")){
new GetParentVitalName().execute();
Log.d("Tag", "Execute complete");
populateListView();
Log.d("Tag", "Populate complete");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_vital_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void populateListView() {
ArrayAdapter<String> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.vitalsListView);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<String>{
public MyListAdapter(){
super(VitalListActivity.this, R.layout.vital_list_layout, vitalList);
Log.d("Tag", "super okay");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
//Making sure we've a view to work with(may have been given null
Log.d("TAG","Inside get view");
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.vital_list_layout, parent, false);
String currentVital = vitalList.get(position);
//Fill the view
TextView vitalText = (TextView) itemView.findViewById((R.id.vitalTextView));
vitalText.setText(currentVital);
return itemView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return vitalList.size();
}
}
private class GetParentVitalName extends AsyncTask<String, String, String> {
protected String doInBackground(String... params){
String responseText = null;
HttpClient httpClient = ServiceHelper.getHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://test.gogch.com/gch-restful/vitals/custome/added/parentvitals");
Log.d("KMN", "Sucess");
try {
HttpResponse response = httpClient.execute(httpGet);
Log.d("DFGH","Success again.");
int statusCode = response.getStatusLine().getStatusCode();
Log.d("Http Post Response V", Integer.toString(statusCode));
responseText = EntityUtils.toString(response.getEntity());
Log.d("Http Post Response V", responseText);
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return responseText;
}
protected void onPostExecute(String responseText){
if (responseText != null){
try {
JSONArray vitalsArray = new JSONArray(responseText);
for(int i =0;i<vitalsArray.length();i++){
JSONObject vitalsObject = vitalsArray.getJSONObject(i);
String parentVitalName = vitalsObject.getString("parentVitalName");
vitalList.add(parentVitalName);
Log.d("YGVJ",vitalList.get(i));
}
}
catch (JSONException e){
e.printStackTrace();
}
}
}
}
}
vital_list_layout.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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/vitalTextView"
android:layout_marginLeft="27dp"
android:layout_marginStart="27dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView" />
</RelativeLayout>
activity_vital_list.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.cinch.getvitalsapp.activities.VitalListActivity">
<TextView android:text="Vitals" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vitalsListView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView" />
</RelativeLayout>
Logcat shows no error. But the List is not displaying. I'm only getting a blank page
Call notifyDataSetChanged()
after adding items in Adapter data-source:
vitalList.add(parentVitalName);
adapter.notifyDataSetChanged();
Note: make sure vitalList
is same List object which is used in MyListAdapter
Adapter class.
EDIT:
Also override getCount()
method in MyListAdapter
class:
@Override
public int getCount() {
// TODO Auto-generated method stub
return vitalList.size();
}