【发布时间】:2015-01-23 05:40:59
【问题描述】:
我通过基本适配器创建了一个自定义 ListView,如下代码所示。 实际上我需要获取 CustomListView 的长度。感谢您提前提供的帮助
public class ListViewWithBaseAdapter extends Activity {
public class codeLeanChapter {
String chapterName;
String chapterDescription;
}
CodeLearnAdapter chapterListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_with_simple_adapter);
chapterListAdapter = new CodeLearnAdapter();
ListView codeLearnLessons = (ListView)findViewById(R.id.listView1);
codeLearnLessons.setAdapter(chapterListAdapter);
codeLearnLessons.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
codeLeanChapter chapter = chapterListAdapter.getCodeLearnChapter(arg2);
Toast.makeText(ListViewWithBaseAdapter.this, chapter.chapterName,Toast.LENGTH_LONG).show();
}
});
}
// ---------------------------------------------Adapter class Start from Here--------------------------------------------------------------
public class CodeLearnAdapter extends BaseAdapter {
List<codeLeanChapter> codeLeanChapterList = getDataForListView();
@Override
public int getCount() {
// TODO Auto-generated method stub
return codeLeanChapterList.size();
}
@Override
public codeLeanChapter getItem(int arg0) {
// TODO Auto-generated method stub
return codeLeanChapterList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1==null)
{
LayoutInflater inflater = (LayoutInflater) ListViewWithBaseAdapter.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2,false);
}
TextView chapterName = (TextView)arg1.findViewById(R.id.textView1);
TextView chapterDesc = (TextView)arg1.findViewById(R.id.textView2);
codeLeanChapter chapter = codeLeanChapterList.get(arg0);
chapterName.setText(chapter.chapterName);
chapterDesc.setText(chapter.chapterDescription);
return arg1;
}
public codeLeanChapter getCodeLearnChapter(int position)
{
return codeLeanChapterList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_view_with_simple_adapter, menu);
return true;
}
public List<codeLeanChapter> getDataForListView()
{
List<codeLeanChapter> codeLeanChaptersList = new ArrayList<codeLeanChapter>();
for(int i=0;i<10;i++)
{
codeLeanChapter chapter = new codeLeanChapter();
chapter.chapterName = "Chapter "+i;
chapter.chapterDescription = "This is description for chapter "+i;
codeLeanChaptersList.add(chapter);
}
return codeLeanChaptersList;
}
}
【问题讨论】:
-
自定义列表视图的长度?
-
如果我正确理解您的问题,这将返回您的列表视图长度。 codeLeanChapterList.size().
-
其实我做了一个CustomListView如上。我需要知道我的 ListView 中有多少项。所以问题是“如何获取 ListView 中的项目数并在 Toast 中显示”。就是这样
-
您的适配器中有一个
getCount()方法,它应该会告诉您这一点。 -
列表的大小将是列表视图的长度
标签: android