Android:首先在ListView添加LinearLayout

时间:2016-04-01 15:41:55

标签: android listview

我想在listview

添加新项目

运行后,申请人关闭

  

代码:

final ArrayList<StructNote> notesPage = new ArrayList<StructNote>();
final ListView lstContent = (ListView) findViewById(R.id.lstContent);
adapter = new AdapterNote(notesPage);
View addNew = G.inflater.inflate(R.layout.addnew, null);
adapter.insert(addNew, 0);
lstContent.setAdapter(adapter);
adapter.notifyDataSetChanged();
  

错误日志:

04-01 15:39:02.163: E/AndroidRuntime(1908): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.app.network.StructNote
04-01 15:39:02.163: E/AndroidRuntime(1908): at com.app.network.AdapterNote.getView(AdapterNote.java:612)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.ListView.makeAndAddView(ListView.java:1772)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.ListView.fillDown(ListView.java:672)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.ListView.fillFromTop(ListView.java:732)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.ListView.layoutChildren(ListView.java:1611)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.AbsListView.onLayout(AbsListView.java:1863)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.view.View.layout(View.java:11278)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.view.ViewGroup.layout(ViewGroup.java:4224)
04-01 15:39:02.163: E/AndroidRuntime(1908): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)

StructNote保存项目每个listview

  

StructNote:

public class StructNote {
    public int     id;
    public String  singer;
    public String  title;
}

AdapterNote设置为listview的listview foreach子项

  

AdapterNote:

public class AdapterNote extends ArrayAdapter<StructNote> {

    public AdapterNote(ArrayList<StructNote> array) {
        super(G.context, R.layout.post, array);
    }

    private static class ViewHolder {
        ...
}

1 个答案:

答案 0 :(得分:0)

试试这个:

final ArrayList<StructNote> notesPage = new ArrayList<StructNote>();
final ListView lstContent = (ListView) findViewById(R.id.lstContent);
adapter = new AdapterNote(notesPage);
LayoutInflater inflater = getLayoutInflater();
View addNew = inflater.inflate(R.layout.addnew, lstContent, false);
lstContent.addHeaderView(addNew, null, false);
lstContent.setAdapter(adapter);
adapter.notifyDataSetChanged();