多个ArrayAdapter - 在最后一个中不调用getView

时间:2014-11-03 22:21:50

标签: android listview getview

我有两个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;


    }
}

2 个答案:

答案 0 :(得分:1)

如果没有处理和日记的onItemClickLister代码。我建议做两件事。

  1. 如果您使用任何数组,在适配器中列出...,在每个数组的getCount()函数中,您应该返回大小。注意:请务必致电datasetchange
  2. Listview有一件事,它回收视图。在MyListAdapter中,检查convertView是否为null但没有其他大括号。 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(尽管名称可能是自解释的)告诉适配器“填充”列表的数据模型已经改变,强制适配器启动所有调用的过程再次回调(现在你的数组应该不为空)