我是Android開發者的完整初學者,所以我遵循我可以找到的指南。我想從ArrayList填充一個ListFragment。到目前爲止,我使用http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/來獲得一個滑動菜單,該菜單在點擊菜單中的項目時顯示不同的片段。然後我用使用ArrayList來輪詢ListFragment
package de.vogella.android.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListFragment;
public class MyListFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}
來填充ListFragment。
我想使用ArrayList來代替。我想要使用此代碼:
package com.example.triage;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<Patient> patients = new ArrayList<Patient>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populatePatientList();
populateListView();
registerClickCallback();
}
private void populatePatientList() {
patients.add(new Patient("Vijay Vazirani", 0));
patients.add(new Patient("Jie Wu", 2));
patients.add(new Patient("Nadia Magnenat Thalmann", 3));
patients.add(new Patient("Andrew Ng", 1));
patients.add(new Patient("Steve Omohundro", 0));
patients.add(new Patient("Joseph Sifakis", 4));
patients.add(new Patient("Eva Tardos", 2));
patients.add(new Patient("Konrad Zuse", 1));
}
private void populateListView() {
ArrayAdapter<Patient> adapter = new PatientListAdapter();
ListView patientList = (ListView) findViewById(R.id.listPatientsInER);
patientList.setAdapter(adapter);
}
private class PatientListAdapter extends ArrayAdapter<Patient> {
public PatientListAdapter(){
super(MainActivity.this, R.layout.view_list_item, patients);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get a view to work with
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.view_list_item, parent, false);
}
// find the patient to work with
Patient currentPatient = patients.get(position);
//patient name
TextView nameText = (TextView)itemView.findViewById(R.id.listItem_textPatientName);
nameText.setText(currentPatient.getName());
//Urgency
TextView urgencyText = (TextView)itemView.findViewById(R.id.listItem_textUrgencyValue);
urgencyText.setText(currentPatient.getUrgency());
return itemView;
}
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.listPatientsInER);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Patient clickedPatient = patients.get(position);
Toast.makeText(MainActivity.this, clickedPatient.getName(), Toast.LENGTH_SHORT).show();
}
});
}
}
有人可以指導我如何做到這一點?每次嘗試時,我都卡在方法populateListView()
中,因爲ListFragment中沒有ListView(我認爲),因此findViewById()
不起作用。
謝謝! Makrand
檢查ArrayList的大小可能是其0.1 – 2014-11-06 05:02:19
你得到任何錯誤? – GrIsHu 2014-11-06 05:45:39