我有一个列表视图,该列表视图在列表中显示arrays.xml中的数组。问题是,我希望它从int自动创建项目,而不是从xml读取它。例如,如果int为10,则应创建10个列表项-第1章,第2章,第10章。
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;
}
}
}
});
答案 0 :(得分:2)
然后您应该手动填充列表。
int count = x;
String[] array = new String[count];
for(int i = 0; i < count; i++){
array[i] = ("Chapter " + (i + 1));
}
答案 1 :(得分:0)
代替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);