android中listview的布局充气器
我在arraylist中有来自数据库的完整列值。但我只想在listview中显示消息和日期列。 我的代码:android中listview的布局充气器,android,android-layout,Android,Android Layout,我在arraylist中有来自数据库的完整列值。但我只想在listview中显示消息和日期列。 我的代码: 公共类DisplayAllNotification扩展活动 { ListView lv; 语境; 创建时受保护的void(Bundle savedInstanceState){ //TODO自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.notif); lv=(ListView)findViewBy
公共类DisplayAllNotification扩展活动
{
ListView lv;
语境;
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.notif);
lv=(ListView)findViewById(R.id.listView1);
SqlitenotifDb=新的SqlitenotifDb(getApplicationContext(),
“notifManager”,空,1);
ArrayList lstString=新的ArrayList();
lstString=db.getNotifications();//捕获NotificationData对象
NotificationAdapter NotificationAdapter=新NotificationAdapter(此,
android.R.layout.simple_list_item_1,lstString);
lv.设置适配器(通知适配器);
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、,
长id){
//单击“希望在listview中显示2列”
}
});
}
NotificationAdapter.java
公共类NotificationAdapter扩展了ArrayAdapter{
私人语境;
私有int资源;
私有ArrayList对象;
公共NotificationAdapter(上下文、int资源、,
ArrayList对象)
{
超级(上下文、资源、对象);
//TODO自动生成的构造函数存根
这个资源=资源;
this.context=上下文;
this.objects=对象;
}
}
在使用带有自定义适配器的listView时,您缺少很多内容。我建议您阅读有关自定义适配器和listView的更多信息。无论如何,以下是解决您问题的完整代码:
在res/layout
下定义一个布局,并将其命名为my\u list\u item.xml
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
与
三个解决方案,选择一个 1-假设NotificatioData类包含所有列。
定义另一个仅包含要显示的列的类。 从NotificationData中填写它
将该数组传递给适配器 2-从
db.getNotifications()
返回一个数组,其中只包含要显示的列
3-由于数据源是数据库,请使用游标适配器。在
bindView
方法中,仅将您想要的列绑定到列表视图项。您必须共享您的NotificationAdapter才能获得答案我想从arraylist中检索列值以获取进一步的代码。您能指导我如何检索项字段的值吗您可以从NotificationData对象中检索任何值。您可以更改行my_list_item.xml和适配器以显示不同的值。
<?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="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
public class NotificationAdapter extends ArrayAdapter<NotificationData> {
private Context context;
private int resource;
private ArrayList<NotificationData> objects;
public NotificationAdapter(Context context, int resource, ArrayList<NotificationData> objects) {
super(context, resource, objects);
this.resource = resource;
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
View listRow = LayoutInflater.from(context).inflate(resource, null);
convertView = listRow;
}
TextView textView1 = (TextView)convertView.findViewById(R.id.textView1);
TextView textView2 = (TextView)convertView.findViewById(R.id.textView2);
NotificationData notificationData = getItem(position);
textView1.setText(notificationData.getMessage());
textView2.setText(notificationData.getDate());
return convertView;
}
@Override
public NotificationData getItem(int position) {
if (this.objects != null) {
return this.objects.get(position);
} else {
return null;
}
}
@Override
public int getCount() {
if (this.objects != null) {
return this.objects.size();
} else {
return 0;
}
}
}
NotificationAdapter notificationAdapter = new NotificationAdapter(this,
android.R.layout.simple_list_item_1, lstString);
NotificationAdapter notificationAdapter = new NotificationAdapter(this,
R.layout.my_list_item, lstString);