Android的定制行项目的ListView控件 [英] Android custom Row Item for ListView
本文介绍了Android的定制行项目的ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有应具有以下布局的行一个ListView:
头
文本
头
应该是静态的,但文本
改变每隔几秒钟。
我实现它通过填充 String []数组
,把它传递给一个 ArrayAdapter
,每一次设置数据的变化:
data_array中= populateString();
适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,android.R.id.text1,data_array中);
listView.setAdapter(适配器);
我的问题是,我不知道如何显示在上面的格式的数据。
在此先感谢。
解决方案
将此row.xml向版式文件夹
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
< TextView的Android的:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=标题/>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /文本/>
< / LinearLayout中>
让你的主XML布局,因为这
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
<的ListView
机器人:ID =@ + ID /列表视图
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< / ListView控件>
< / LinearLayout中>
这是您的适配器
类yourAdapter扩展了BaseAdapter {
上下文语境;
的String []的数据;
私有静态LayoutInflater吹气= NULL;
公共yourAdapter(上下文的背景下,的String []数据){
// TODO自动生成构造函数存根
this.context =背景;
this.data =数据;
充气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@覆盖
公众诠释getCount将(){
// TODO自动生成方法存根
返回data.length;
}
@覆盖
公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回数据[位置]
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
查看VI = convertView;
如果(六== NULL)
VI = inflater.inflate(R.layout.row,NULL);
TextView的文字=(TextView的)vi.findViewById(R.id.text);
text.setText(数据[位置]);
返回六;
}
}
您的Java活性
公共类StackActivity延伸活动{
ListView控件列表视图;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
列表视图=(ListView控件)findViewById(R.id.listview);
listview.setAdapter(新yourAdapter(这一点,新的String [] {DATA1,
数据2}));
}
}
的结果
I have a ListView that should have the following layout in its rows:
HEADER
Text
HEADER
should be static but the Text
changes every few seconds.
I implemented it by populating a String[] array
, pass it to an ArrayAdapter
and set it every time the data changes:
data_array = populateString();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, data_array);
listView.setAdapter(adapter);
My problem is that I do not know how to display the data in the format above.
Thanks in advance.
解决方案
Add this row.xml to your layout folder
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"/>
</LinearLayout>
make your main xml layout as this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
This is your adapter
class yourAdapter extends BaseAdapter {
Context context;
String[] data;
private static LayoutInflater inflater = null;
public yourAdapter(Context context, String[] data) {
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.row, null);
TextView text = (TextView) vi.findViewById(R.id.text);
text.setText(data[position]);
return vi;
}
}
Your java activity
public class StackActivity extends Activity {
ListView listview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(new yourAdapter(this, new String[] { "data1",
"data2" }));
}
}
the results
这篇关于Android的定制行项目的ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文