layoutinflater for android中的listview

时间:2013-10-28 13:34:29

标签: android android-layout

我在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;
    }   
}

2 个答案:

答案 0 :(得分:0)

使用带有自定义适配器的listView时,您遗漏了很多。我建议你阅读有关自定义适配器和listView的更多信息。无论如何,这是一个完整的代码来解决您的问题:

res/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 :(得分:0)

三种解决方案,选择一种。

1-假设NotificatioData类保存所有columms 定义另一个只包含要显示的列的类。 从NotificationData填写 将该数组传递给适配器

2 - 从db.getNotifications()返回一个只包含您要显示的列的数组。

3 - 由于数据源是数据库,因此使用CursorAdapter。在bindView方法中,仅将所需的列绑定到列表视图项。