gpt4 book ai didi

java - 使用 ArrayList 填充 ListFragment

转载 作者:行者123 更新时间:2023-11-30 02:34:38 26 4
gpt4 key购买 nike

我是 Android 开发的完全初学者,所以我遵循我能找到的指南。我想从 ArrayList 填充 ListFragment。到目前为止,我已经使用了 http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/获得一个滑动菜单,在单击菜单中的项目时显示不同的 fragment 。然后我用了

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() 不起作用。

谢谢!马克兰

最佳答案

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);

}

您已将适配器设置为listview,但您忘记将列表patients插入适配器,因此您的适配器 没有数据,您的listview 不显示任何内容。

关于java - 使用 ArrayList 填充 ListFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com