求一个Android ListView例子 要求是可以有TextView 显示图片,后面还有有button
1个回答
展开全部
adapter =new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=getLayoutInflater().inflate(R.layout.r1, parent, false);
TextView tv_user=(TextView) v.findViewById(R.id.textView1);
Button btn_default=(Button) v.findViewById(R.id.button1);
tv_user.setText(datas.get(position).getName());
btn_default.setText(datas.get(position).getPwd());
return v;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
};
listview.setAdapter(adapter);
}
这是适配器的代码 在改个XML布局文件就OK 了
r1.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="52dp"
android:layout_marginTop="144dp"
android:drawableLeft="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:text="Button" />
</RelativeLayout>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=getLayoutInflater().inflate(R.layout.r1, parent, false);
TextView tv_user=(TextView) v.findViewById(R.id.textView1);
Button btn_default=(Button) v.findViewById(R.id.button1);
tv_user.setText(datas.get(position).getName());
btn_default.setText(datas.get(position).getPwd());
return v;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
};
listview.setAdapter(adapter);
}
这是适配器的代码 在改个XML布局文件就OK 了
r1.xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="52dp"
android:layout_marginTop="144dp"
android:drawableLeft="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:text="Button" />
</RelativeLayout>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询