Android 为listview自动生成项目
我有一个listview,它在列表中显示arrays.xml中的数组。问题是,我不想从xml读取它,而是希望它自动从int创建项。例如,如果int是10,那么它应该创建10个列表项-第1章,第2章。。。第十章Android 为listview自动生成项目,android,listview,Android,Listview,我有一个listview,它在列表中显示arrays.xml中的数组。问题是,我不想从xml读取它,而是希望它自动从int创建项。例如,如果int是10,那么它应该创建10个列表项-第1章,第2章。。。第十章 list1 = (ListView) findViewById(R.id.ListView01); array = getResources().getStringArray(R.array.myArray); adapter = new ArrayAdapter<
list1 = (ListView) findViewById(R.id.ListView01);
array = getResources().getStringArray(R.array.myArray);
adapter = new ArrayAdapter<String>(this, R.layout.listview_row_customizations, getResources().getStringArray(R.array.myArray));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
AudioBible.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String productName = adapter.getItem(position);
for (int i = 0; i < array.length; i++) {
String product = array[i];
if (product.equals(productName)) {
Intent myIntent = new Intent(AudioBible.this, TTS.class);
myIntent.putExtra("key", i);
startActivity(myIntent);
break;
}
}
}
});
list1=(ListView)findViewById(R.id.ListView01);
array=getResources().getStringArray(R.array.myArray);
adapter=new ArrayAdapter(这个,R.layout.listview_row_customizations,getResources().getStringArray(R.array.myArray));
列表1.设置适配器(适配器);
inputSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列cs、int arg1、int arg2、int arg3){
//当用户更改文本时
AudioBible.this.adapter.getFilter().filter(cs);
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
}
});
list1.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
String productName=adapter.getItem(位置);
for(int i=0;i
然后您应该手动填充列表
int count = x;
String[] array = new String[count];
for(int i = 0; i < count; i++){
array[i] = ("Chapter " + (i + 1));
}
int count=x;
字符串[]数组=新字符串[计数];
for(int i=0;i
而不是执行array=getResources().getStringArray(R.array.myArray)代码>
以编程方式创建数组并将其传递到适配器中
ArrayList<String> array = new ArrayList<>();
for(int i = 0; i < 10; i++){
array.add("Chapter " + i++);
}
adapter = new ArrayAdapter<String>(this, R.layout.listview_row_customizations, array);
ArrayList数组=新的ArrayList();
对于(int i=0;i<10;i++){
添加(“章节”+i++);
}
adapter=new ArrayAdapter(此,R.layout.listview\u行\自定义,数组);
无法使其工作。你能编辑我的代码吗?试试看,我用了array
而不是arraylist