我是Android Dev的初学者,所以我正在关注我能找到的指南。我想从ArrayList填充ListFragment。到目前为止,我已经使用http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/获取了一个滑动菜单,当菜单中的项目被单击时,该菜单会显示不同的碎片。然后我用了
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
答案 0 :(得分:1)
Bingo,问题在于你的populateListView
:
private void populateListView() {
ArrayAdapter<Patient> adapter = new PatientListAdapter();
//add the list patients to your adapter
for(Patient p: patients)
{
adapter.add(p);
}
ListView patientList = (ListView) findViewById(R.id.listPatientsInER);
patientList.setAdapter(adapter);
}
您已将adapter
设置为listview
,但您忘记将列表patients
插入适配器,因此您的adapter
没有数据而您的listview
1}}什么都不显示。
答案 1 :(得分:0)
试试这个
public PatientListAdapter(Context context,List<Patient> patients){
super(context, R.layout.view_list_item, patients);
}
和
ArrayAdapter<Patient> adapter = new PatientListAdapter(this,patients);
并且更喜欢使用getApplicationContext()
而不是MainAcitivity.this