[英]“Layout Inflator ” and “Fragment Layout” in android
[英]layout inflator for listview in android
我從arraylist中的數據庫中獲得了完整的列值。 但是我只想在列表視圖中顯示消息和日期列。 我的代碼:
public class DisplayAllNotification extends Activity
{
ListView lv;
Context context;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.notif);
lv = (ListView) findViewById(R.id.listView1);
SqlitenotifDb db = new SqlitenotifDb(getApplicationContext(),
"notifManager", null, 1);
ArrayList<NotificationData> lstString = new ArrayList<NotificationData>();
lstString = db.getNotifications();// catch NotificationData obj
NotificationAdapter notificationAdapter = new NotificationAdapter(this,
android.R.layout.simple_list_item_1, lstString);
lv.setAdapter(notificationAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//on click want to display 2 columns in listview
}
});
}
NotificationAdapter.java
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);
// TODO Auto-generated constructor stub
this.resource = resource;
this.context = context;
this.objects = objects;
}
}
在將listView與自定義適配器一起使用時,您會丟失很多東西。 我建議您閱讀有關自定義適配器和listView的更多信息。 無論如何,這是解決您的問題的完整代碼:
在res/layout
layout下定義一個布局,並將其命名為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;
}
}
}
最后,您的活動的onCreate
方法更改此代碼;
NotificationAdapter notificationAdapter = new NotificationAdapter(this,
android.R.layout.simple_list_item_1, lstString);
與
NotificationAdapter notificationAdapter = new NotificationAdapter(this,
R.layout.my_list_item, lstString);
三種解決方案,選擇一種。
1-假設NotificatioData類包含所有列。
用僅要顯示的列定義另一個類。 從NotificationData填寫
將該數組傳遞給適配器
2-從db.getNotifications()
返回僅包含要顯示的列的數組。
3-由於數據源是數據庫,請使用CursorAdapter。 在bindView
方法中,僅將您想要的列綁定到列表視圖項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.