问题描述
我要显示两个字符串以下任一其他的,就好像它是一个列表。
我试着用simple_list_item_1管理来填充列表视图但没有什么是显示在屏幕上。
I want to display two strings one below other as if it is a list.I tried using simple_list_item_1 to populate listview but nothing is visible on the screen.
((ListView)view).setAdapter(new ArrayAdapter<String>
(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));
,其中标签是包含两个项目要显示的阵列。所有我能看到的是一个白色的屏幕。此外,我想这个列表无法点击。
where label is an array which contains two items to be displayed. All I can see is a white screen. Also I want this list to be unclickable.
我想要的输出是这样的:
I want the output to be like this:
String 1
String 2
我不与活动合作,而不是与查看。
这里是一个创建视图中的方法
I am not working with the Activity, instead with the View.Here is the method which creates view
public void createView(JSONObject definition) {
// TODO Auto-generated method stub
super.createView(definition);
final JSONArray rowArray = definition.optJSONArray(KeyConstants.KEY_ROWS);
final JSONArray columnArray = definition.optJSONArray(KeyConstants.KEY_COLUMNS);
if (rowArray != null) {
label = new String[rowArray.length()];
for (int i = 0; i < rowArray.length(); i++) {
final JSONObject candidate = rowArray.optJSONObject(i);
if(candidate!=null){
label[i]=candidate.optString(KeyConstants.KEY_LABEL_TEXT, null);
}
}
}
((ListView)view).setAdapter(new ArrayAdapter<String>(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));
}
感谢。
推荐答案
首先创建一个自定义适配器类,如下
First create a custom Adapter Class as below
类MyAdapter扩展ArrayAdapter
{
class MyAdapter extends ArrayAdapter {
private Context context;
private ArrayList<String> list;
public MyAdapter(Context context,
ArrayList<String> list)
{
super(context, R.layout.layout_list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_list,
null);
convertView.setTag(holder);
holder.string1 = (TextView) convertView
.findViewById(R.id.stringgID1);
holder.string2 = (TextView) convertView
.findViewById(R.id.stringID2);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.string1 .setText(list.get(postion));
holder.string2 .setText(list.get(position));
return convertView;
}
@Override
public int getCount() {
return list.size();
}
class ViewHolder {
TextView string1 = null;
TextView string2 = null;
}
}
然后让你的活动延伸活动和不ListActivity
listView.setAdapter(新MyAdapter(youtactivityame.this,标签);
listView.setAdapter(new MyAdapter(youtactivityame.this,label);
这样做有可以显示任意数量的列表项的标签。
Doing this the label with any number of list items can be displayed.
这篇关于使用arrayadapter填充的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!