0

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

2 回答 2

0

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

1-假设 NotificatioData 类包含所有列。
定义另一个仅包含您要显示的列的类。从 NotificationData 填充它
将该数组传递给 Adapter

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

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

于 2013-10-28T17:08:01.523 回答
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);
于 2013-10-28T17:01:31.980 回答