Android的CustomListAdapter notifydatachange()无法正常工作 [英] Android CustomListAdapter notifydatachange() is not working

查看:155
本文介绍了Android的CustomListAdapter notifydatachange()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想DISPLY使用自定义的产品清单 ArrayAdapter

I want to disply list of items using Custom ArrayAdapter.

我会通过点击按钮插入数据。

I will insert data by clicking a button.

MainFragment.java

@Override
        public void onClick(View v) {
            DatabaseClass feed = new DatabaseClass(getActivity());
            new DatabaseClass(getActivity()).getList();
            new MyFragment().updateList();
        }

我的布局文件如下:

My layout file is as follows.

的main.xml

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <fragment
                    android:id="@+id/update"
                    android:name="com.android.MyFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>

MyFragment.java

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyFragment extends ListFragment {

    // creating database reference
    private static DatabaseClass feed;
    private List<CustomModel> customList;
    private static CustomListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        feed = new DatabaseClass(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // To get List of CustomModel objects
        feed.getList();

        // CUSTOM_LIST is list constant
        adapter = new CustomListAdapter(getActivity(),
                R.layout.update, Utils.CUSTOM_LIST);

        // setting adapter      
        setListAdapter(adapter);
        View view = inflater.inflate(R.layout.update, container, false);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    // this method is called after inserting data into database
    public void updateList() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        } else {
            adapter = new CustomListAdapter(getActivity(),
                    R.layout.update, Utils.CUSTOM_LIST);
            adapter.notifyDataSetChanged();
        }
    }

    private class CustomListAdapter extends ArrayAdapter<CustomModel> {

        private final Context context;
        private final List<CustomModel> list;
        private TextView name, message;

        public CustomListAdapter(Context context, int resource,
                List<CustomModel> list) {
            super(context, resource, list);
            this.context = context;
            this.list = list;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.single_row, parent,
                    false);

            name = (TextView) view.findViewById(R.id.name);
            message = (TextView) view.findViewById(R.id.message);
            CustomModel obj = list.get(position);

            name.setText(obj.getName());
            message.setText(obj.getMessage());

            return view;
        }
    }
}

而对于MyFragment.java布局文件是

And layout file for MyFragment.java is

update.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
 />

我的数据库类

My Database class is

DatabaseClass.java

    public void getList(){
        ourDatabase = this.getReadableDatabase();
        String sql = "SELECT * FROM " + DATABASE_TABLE;
        Cursor c = ourDatabase.rawQuery(sql, null);

        int iName = c.getColumnIndex(KEY_NAME);
        int iMessage = c.getColumnIndex(KEY_MESSAGE);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            CustomModel model = new CustomModel();


            model.setName(c.getString(iFromName));
            model.setMessage(c.getString(iMessage));

            Utils.LIST.add(model);
        }
}

按钮的onclick,数据插入到数据库。但它不反映在UI中。 为什么我的列表视图不更新?

onclick of button, data is inserted into db. but it is not reflecting in UI. Why my list view is not updated?

感谢

推荐答案

更​​改适配器如下:

    private class CustomListAdapter extends ArrayAdapter<CustomModel> {

    private final Context context;
    private final List<CustomModel> list;
    private TextView name, message;

    public CustomListAdapter(Context context, int resource,
            List<CustomModel> list) {
        super(context, resource, list);
        this.context = context;
        this.list = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.single_row, parent, false);

        name = (TextView) view.findViewById(R.id.name);
        message = (TextView) view.findViewById(R.id.message);
        CustomModel obj = list.get(position);

        name.setText(obj.getName());
        message.setText(obj.getMessage());

        return view;
    }

    public void updateList(List<CustomModel> list) {

        this.list = list;
        notifyDataSetChanged();

    }
}

这样的方法添加updateList在你的适配器本身并调用方法 notifyDataSetChanged();

所以从你的活动,而不是调用 notifyDataSetChanged(); 呼叫 updateList(名单&LT; CustomModel&GT; newList)

so from your activity instead of calling notifyDataSetChanged(); call updateList(List<CustomModel> newList)

我相信这会工作。

这篇关于Android的CustomListAdapter notifydatachange()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆