我有两个ListViews,treatmentListView和diaryListView。如果在treatmentListView中单击了一个项目,我想显示该项目的diaryListView。我的问题是没有在MyDiaryAdapter中调用getView,也没有显示任何项目。有人得到了修复?
private void fillListView()
{
ArrayAdapter<Treatment> adapter = new MyListAdapter();
list = (ListView) findViewById(R.id.treatmentsListView);
list.setAdapter(adapter);
}
private void fillDiaryListView()
{
ArrayAdapter<Diary> diaryAdapter = new MyDiaryAdapter();
listDiary = (ListView) findViewById(R.id.diaryListView);
listDiary.setAdapter(diaryAdapter);
}
private class MyListAdapter extends ArrayAdapter<Treatment>
{
public MyListAdapter()
{
super(TreatmentList.this, R.layout.treatment_view, treatments);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.treatment_view, parent, false);
}
Treatment currentTreatment = treatments.get(position);
TextView textMake = (TextView)itemView.findViewById(R.id.txtMake);
textMake.setText(currentTreatment.getName());
TextView textStarted = (TextView)itemView.findViewById(R.id.textStarted);
TextView textMethod = (TextView)itemView.findViewById(R.id.textMethod);
return itemView;
}
}
private class MyDiaryAdapter extends ArrayAdapter<Diary>
{
public MyDiaryAdapter()
{
super(TreatmentList.this, R.layout.diary_view, notes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.diary_view, parent, false);
}
Diary currentNote = notes.get(position);
TextView textMake = (TextView)itemView.findViewById(R.id.txtMake);
textMake.setText(currentNote.getTitle());
return itemView;
}
}
编辑: 这里有“笔记”。
private void filldiaryList(Treatment treatment)
{
notes = dbDiary.findDiaryNotes(treatment);
}
下面是如何处理第一个列表的onClick:
private void registerClickCallback() {
list = (ListView)findViewById(R.id.treatmentsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id)
{
Treatment clickedTreatment = treatments.get(position);
loadDiaryList(clickedTreatment);
}
});
}
编辑2: 整个文件:
public class TreatmentList extends Activity
{
private ListView list, listDiary;
private List<Treatment> treatments;
private List<Diary> notes;
private DbHandlerTreatments db;
private DbHandlerDiary dbDiary;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
db = new DbHandlerTreatments(this);
dbDiary = new DbHandlerDiary(this);
loadTreatmentList();
}
private void registerClickCallback() {
list = (ListView)findViewById(R.id.treatmentsListView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id)
{
Treatment clickedTreatment = treatments.get(position);
loadDiaryList(clickedTreatment);
}
});
}
private void registerDiaryClickCallback() {
listDiary = (ListView)findViewById(R.id.diaryListView);
listDiary.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id)
{
Diary note = notes.get(position);
System.out.println(note.getTitle());
}
});
}
private void fillTreatmentList()
{
treatments = db.findAllTreatments();
}
private void filldiaryList(Treatment treatment)
{
notes = dbDiary.findDiaryNotes(treatment);
}
private void loadTreatmentList()
{
setContentView(R.layout.treatments);
fillTreatmentList();
fillListView();
registerClickCallback();
}
private void loadDiaryList(Treatment treatment)
{
setContentView(R.layout.diaries);
filldiaryList(treatment);
fillDiaryListView();
registerDiaryClickCallback();
}
private void fillListView()
{
ArrayAdapter<Treatment> adapter = new MyListAdapter();
list = (ListView) findViewById(R.id.treatmentsListView);
list.setAdapter(adapter);
}
private void fillDiaryListView()
{
ArrayAdapter<Diary> diaryAdapter = new MyDiaryAdapter();
listDiary = (ListView) findViewById(R.id.diaryListView);
listDiary.setAdapter(diaryAdapter);
}
private class MyListAdapter extends ArrayAdapter<Treatment>
{
public MyListAdapter()
{
super(TreatmentList.this, R.layout.treatment_view, treatments);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.treatment_view, parent, false);
}
Treatment currentTreatment = treatments.get(position);
TextView textMake = (TextView)itemView.findViewById(R.id.txtMake);
textMake.setText(currentTreatment.getName());
TextView textStarted = (TextView)itemView.findViewById(R.id.textStarted);
TextView textMethod = (TextView)itemView.findViewById(R.id.textMethod);
return itemView;
}
}
private class MyDiaryAdapter extends ArrayAdapter<Diary>
{
public MyDiaryAdapter()
{
super(TreatmentList.this, R.layout.diary_view, notes);
listDiary.invalidateViews();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null)
{
itemView = getLayoutInflater().inflate(R.layout.diary_view, parent, false);
}
Diary currentNote = notes.get(position);
TextView textMake = (TextView)itemView.findViewById(R.id.txtMake);
textMake.setText(currentNote.getTitle());
return itemView;
}
}
答案 0 :(得分:1)
如果没有处理和日记的onItemClickLister代码。我建议做两件事。
datasetchange
TextView textMake = (TextView)itemView.findViewById(R.id.txtMake);
这可能毫无意义。答案 1 :(得分:0)
加载日记列表后,尝试在diaryAdapter上调用notifyDataSetChanged()
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id)
{
Treatment clickedTreatment = treatments.get(position);
loadDiaryList(clickedTreatment);
diaryAdapter.notifyDataSetChanged()
}
请注意,您需要使diaryAdapter成为类实例变量而不是fillDiaryListView方法的局部变量
编辑:为了完成我的回答,notifyDataSetChanged(尽管名称可能是自解释的)告诉适配器“填充”列表的数据模型已经改变,强制适配器启动所有调用的过程再次回调(现在你的数组应该不为空)