在我的应用程序中,我想在列表中显示一些项目,我将所有项目保存在 String 数组中并使用代码带来
String[] activities = getResources().getStringArray(R.array.Contents);
现在我的任务是将这些字符串数组值分配给我的TextView
.
有什么建议么?
在我的应用程序中,我想在列表中显示一些项目,我将所有项目保存在 String 数组中并使用代码带来
String[] activities = getResources().getStringArray(R.array.Contents);
现在我的任务是将这些字符串数组值分配给我的TextView
.
有什么建议么?
在 MainActivity.class
String[] activities = getResources().getStringArray(R.array.Contents);
List<RowItem> rowItems;
In onCreate(){
super.onCreate(savedInstanceState);
setContentView(layoutname);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(activities[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(id of ListView);
CustomBaseAdapter adapter = new CustomBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
}
在 RowItem 类中
public class RowItem {
private String activities;
public RowItem(String activities {
this.activities = activities;
}
public String getActivities() {
return activities;
}
public void setactivities(String activities) {
this.activities = activities;
}
@Override
public String toString() {
return activities;
}
}
在 CustomBaseAdapter 类中
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
private class ViewHolder {
TextView txtActivities;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.activities);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtActivites.setText(rowItem.getActivity());
return convertView;
}
}
尝试这个
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, activities);
mylistView.setAdapter(aa);