我正在学习使用ListView
填充ArrayList
。我已使用
ArrayList
doPostExecute()
内的GetParentVitalsName()
vitalList.add(parentVitalName);
Logcat没有显示错误。但列表没有显示。我只得到一个空白页面。我发现它不会进入getView
的{{1}}方法。我在这做错了什么?
MyListAdapter
包含acitivity_vital_list.xml
,ListView
包含项目模板
vital_list_layout
vital_list_layout.xml
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();
}
}
}
}
}
activity_vital_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">
<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>
答案 0 :(得分:3)
Logcat没有显示错误。但列表没有显示。我只是 得到一个空白页
在适配器数据源中添加项目后调用notifyDataSetChanged()
:
vitalList.add(parentVitalName);
adapter.notifyDataSetChanged();
注意:确保vitalList
与MyListAdapter
适配器类中使用的List对象相同。
修改强>
还覆盖getCount()
类中的MyListAdapter
方法:
@Override
public int getCount() {
// TODO Auto-generated method stub
return vitalList.size();
}
答案 1 :(得分:1)
制作您的适配器类如下所示。
private class MyListAdapter extends ArrayAdapter<String>{
Context context;
ArrayList<String> arraylist= new Arraylist<>;
public MyListAdapter(Context context,ArrayList<String> arraylist){
super(context, R.layout.vital_list_layout, vitalList);
Log.d("Tag", "super okay");
this.context=context;
this.arraylist=arraylist;
}
@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(arralist.get(position));
return itemView;
}
}
在您的Activity类中。初始化适配器并将arraylist放入其中,如下所示:
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vital_list);
list = (ListView) findViewById(R.id.vitalsListView);
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");
}
}
private void populateListView() {
MyListAdapter adapter = new MyListAdapter(VitalListActivity.this,vitalList); //where vitalList will be get from asynctask.
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 2 :(得分:0)
您没有覆盖getCount()方法。尝试这样做,它会正常工作。
@Override
public int getCount() {
return vitalList.size();
}
然后尝试将list设置为match_parent。