package sampleapp.razen.com.sampleapp;
import android.app.ListActivity;
import android.os.Bundle;
public class SectionListView extends ListActivity {
private CustomAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new CustomAdapter(this);
for (int i = 1; i < 30; i++) {
mAdapter.addItem("Row Enter #" + i);
if (i % 4 == 0) {
mAdapter.addSectionHeaderItem("Group #" + i);
}
}
setListAdapter(mAdapter);
}
}
如何编辑此for循环以实现此目的
#1 normal for loop
Text 1
Text 2
Text 3
#2 advance for loop
Text 1
Text 2
Text 3
答案 0 :(得分:0)
public class SectionListView extends ListActivity {
private CustomAdapter mAdapter;
List<String> list = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new CustomAdapter(this);
fillList();
fillAdapter(mAdapter);
setListAdapter(mAdapter);
}
private void fillList(){
for (int i = 0; i < 10; i++) {
//this will alternate between "normal" and "advance"
list.add(i % 2 == 0 ? "normal" : "advance");
list.add("Text");
list.add("Text.");
list.add("Text..");
}
}
private void fillAdapter(CustomAdapter adapter) {
for (int i = 0; i < list.size(); i++) {
int row = i % 3;
if (row == 0) {
adapter.addSectionHeaderItem(list.get(i) + (i + 1));
continue;
}
adapter.addItem(list.get(i) + (row + 1));
}
}
}
答案 1 :(得分:0)
mAdapter = new CustomAdapter(this);
for (int i = 0; i < 29; i++) {
int value = i % 3;
if (value == 0) {
mAdapter.addSectionHeaderItem("Group #" + (i+1));
}
mAdapter.addItem("Row Enter #" + (value + 1));
}
“它现在正在工作如何编辑这些行输入#1到名称所在的位置 群体有不同的名称。我的意思是我组的名字 一个人不应该在第二组。提前感谢你帮助“
你可以这样做:
class Group {
String name;
String members[];
public Group(String name, String [] members) {
this.name = name;
this.members = members;
}
}
在你的onCreate()中:
ArrayList<Group> groups = new ArrayList<>();
groups.add(new Group("Group A", new String[]{"A","B","C"}));
groups.add(new Group("Group B", new String[]{"A","B","C"}));
groups.add(new Group("Group C", new String[]{"A","B","C"}));
groups.add(new Group("Group D", new String[]{"A","B","C"}));
groups.add(new Group("Group F", new String[]{"A","B","C"}));
mAdapter = new CustomAdapter(this);
for(Group group : groups) {
mAdapter.addSectionHeaderItem(group.name);
for (int i = 0; i < group.members.length; i ++) {
mAdapter.addItem(group.members[i]);
}
}
setListAdapter(mAdapter);