notifySetDataChanged无法正常工作

时间:2016-01-12 09:35:24

标签: android listview refresh baseadapter notifydatasetchanged

我有ListView,自定义适配器扩展BaseAdapter。该列表包含一些消息,每个消息在看不见时都会突出显示,但在点击后我将状态更改为SQLite数据库中显示的状态,然后使用notifySetDataChanged,但仍然没有刷新。

这是我的清单:

public class Exams extends Fragment {
    List<NotificationMessages> messagesList = new ArrayList();
    private ListView lv;
    String[] subject;
    String[] message;
    String[] date;
    String[] seen;
    ListViewAdapter lva;
    Bundle bundle;

    public Exams() {
        // Required empty public constructor
   }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_exams, container, false);
    lv = (ListView) view.findViewById(R.id.examList);
    bundle = getArguments();
    final Dialog showdialog = new Dialog(getActivity());
    DatabaseHandler dh = new DatabaseHandler(getActivity());
    try {
        dh.Open();
        messagesList = dh.getExams(bundle.getInt("studentId", 0));
        subject = new String[messagesList.size()];
        message = new String[messagesList.size()];
        date = new String[messagesList.size()];
        seen = new String[messagesList.size()];
        for (int i = 0; i < messagesList.size(); i++) {
            subject[i] = messagesList.get(i).getSubject().toString();
            message[i] = messagesList.get(i).getMessage().toString();
            date[i] = messagesList.get(i).getDate_scheduled().toString();
            seen[i] = messagesList.get(i).getStatus().toString();
        }
        lva = new ListViewAdapter(getContext(), subject, message, date, seen);
        lv.setAdapter(lva);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showdialog.setContentView(R.layout.custom_dialog);
                showdialog.setTitle(messagesList.get(position).getSubject());
                //TextView sub = (TextView)showdialog.findViewById(R.id.dialogsubject);
                TextView msg = (TextView) showdialog.findViewById(R.id.dialogmessage);
                TextView dat = (TextView) showdialog.findViewById(R.id.dialogdate);
                TextView tim = (TextView) showdialog.findViewById(R.id.dialogtime);
                //sub.setText(messagesList.get(position).getSubject());
                msg.setText(messagesList.get(position).getMessage());
                tim.setText("Time : " + messagesList.get(position).getDate_scheduled().substring(11));
                dat.setText("Date : " + messagesList.get(position).getDate_scheduled().substring(0, 10));
                showdialog.setCancelable(true);
                showdialog.show();
                DatabaseHandler dsh = new DatabaseHandler(getActivity());
                try {
                    dsh.Open();
                    int seenMsg = dsh.seenMessage(Integer.parseInt(messagesList.get(position).getMessage_recipientID()), bundle.getInt("studentId", 0));
                    Log.d("MessagesExamsSeen", "" + seenMsg);
                } catch (SQLException e) {
                    Log.d("Message not seen", "");
                    e.printStackTrace();
                }
                dsh.close();
                lva.notifyDataSetChanged();

            }
        });
    } catch (SQLException e1) {
        e1.printStackTrace();
        Log.d("Error", " " + e1);
    }
    dh.close();
    return view;
}

这是我的Adapter类:

public class ListViewAdapter extends BaseAdapter {
private Context context;
private String[] subject;
private String[] message;
private String[] date_scheduled;
private String[] seen;
private static LayoutInflater inflater=null;

public ListViewAdapter(Context context, String[] subject, String[] message, String[] date, String[] seen){
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    this.subject = subject;
    this.message = message;
    this.date_scheduled = date;
    this.seen = seen;
}


@Override
public int getCount() {
    return message.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view =convertView;
    if(convertView==null) {
        view = inflater.inflate(R.layout.custom_listview, null);
    }
    RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.rl);
    TextView sub = (TextView)view.findViewById(R.id.subject);
    TextView msg = (TextView)view.findViewById(R.id.message);
    TextView date = (TextView)view.findViewById(R.id.date);
    if(seen[position].equals("seen")){
        rl.setBackgroundColor(Color.TRANSPARENT);
    }
    else if(seen[position].equals("unseen")){
        rl.setBackgroundColor(Color.parseColor("#FFDADADA"));
    }
    // Setting all values in listview
    sub.setText(subject[position]);
    if(message[position].length()>=30){
        msg.setText(message[position].substring(0,30)+"..................");
    }
    else{
        msg.setText(message[position]);
    }
    date.setText(date_scheduled[position].substring(0,10));


    return view;
}

}

有谁能告诉我这里我做错了什么?

5 个答案:

答案 0 :(得分:2)

使用Finally块并插入以下代码:

lva.notifyDataSetChanged();

答案 1 :(得分:0)

 Try this

put your 
public void onresume()
{
adapter.notifySetDataChanged();
}

答案 2 :(得分:0)

您正在更新数据库,但您没有从数据库中再次获取更新的值。您需要从DB获取更新的值,然后调用notifyDataSetChanged方法。

这将解决您所面临的问题。

解决方案1 ​​

它应该是这样的。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_exams, container, false);
lv = (ListView) view.findViewById(R.id.examList);
bundle = getArguments();
final Dialog showdialog = new Dialog(getActivity());
DatabaseHandler dh = new DatabaseHandler(getActivity());
try {
    getDataFromDB();
    lv.setAdapter(lva);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            showdialog.setContentView(R.layout.custom_dialog);
            showdialog.setTitle(messagesList.get(position).getSubject());
            //TextView sub = (TextView)showdialog.findViewById(R.id.dialogsubject);
            TextView msg = (TextView) showdialog.findViewById(R.id.dialogmessage);
            TextView dat = (TextView) showdialog.findViewById(R.id.dialogdate);
            TextView tim = (TextView) showdialog.findViewById(R.id.dialogtime);
            //sub.setText(messagesList.get(position).getSubject());
            msg.setText(messagesList.get(position).getMessage());
            tim.setText("Time : " + messagesList.get(position).getDate_scheduled().substring(11));
            dat.setText("Date : " + messagesList.get(position).getDate_scheduled().substring(0, 10));
            showdialog.setCancelable(true);
            showdialog.show();
            DatabaseHandler dsh = new DatabaseHandler(getActivity());
            try {
                dsh.Open();
                int seenMsg = dsh.seenMessage(Integer.parseInt(messagesList.get(position).getMessage_recipientID()), bundle.getInt("studentId", 0));
                Log.d("MessagesExamsSeen", "" + seenMsg);
            } catch (SQLException e) {
                Log.d("Message not seen", "");
                e.printStackTrace();
            }
            dsh.close();
            getDataFromDB
            lva.notifyDataSetChanged();

        }
    });
} catch (SQLException e1) {
    e1.printStackTrace();
    Log.d("Error", " " + e1);
}
dh.close();
return view;
}

public void getDataFromDB()
{
    dh.Open();
    messagesList = dh.getExams(bundle.getInt("studentId", 0));
    subject = new String[messagesList.size()];
    message = new String[messagesList.size()];
    date = new String[messagesList.size()];
    seen = new String[messagesList.size()];
    for (int i = 0; i < messagesList.size(); i++) 
        {
            subject[i] = messagesList.get(i).getSubject().toString();
            message[i] = messagesList.get(i).getMessage().toString();
            date[i] = messagesList.get(i).getDate_scheduled().toString();
            seen[i] = messagesList.get(i).getStatus().toString();
        }

    lva = new ListViewAdapter(getContext(), subject, message, date, seen);
}

解决方案2

添加

 seen[position] = "1"; // or whatever your new state should be.

之前

 lva.notifyDataSetChanged();

答案 3 :(得分:0)

首先,您检查数据库是否已更新或未正确更新。  在notifyDataSetChanged() getView()之前ListViewAdapter return view;之后调用nestingLabel方法。

答案 4 :(得分:0)

你必须查看适配器的构造函数,它在新实例中复制数组并使用这些新数组,适配器使用的新数组不会更新,所以当你们没有任何改变时,这是完全正常的。致电notifySetDataChanged