自定义列表视图与阵列适配器不更新。它只在重新打开应用程序后更新

时间:2016-08-10 13:55:20

标签: android listview fragment adapter

RowAdapter.java

alignas

}

Alarms.java(片段)

package com.saransh.try3;

import android.content.Context;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.kyleduo.switchbutton.SwitchButton;
import com.vstechlab.easyfonts.EasyFonts;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Saransh01 on 27-07-2016.
 */
public class RowAdapter extends ArrayAdapter {
List list=new ArrayList<>();
public RowAdapter(Context context, int resource) {
    super(context, resource);
}
static class DataHandler{
    TextView date;
    TextView time;
    SwitchButton switchButton;
}
@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
    notifyDataSetChanged();
}

@Override
public void remove(Object object) {
    super.remove(object);
    list.remove(object);
}

@Override
public int getCount() {
    return this.list.size();
}

@Override
public Object getItem(int position) {
    return this.list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row=convertView;
    DataHandler handler;
    if(row==null){
        LayoutInflater inflater=(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=inflater.inflate(R.layout.row_layout,parent,false);
        handler=new DataHandler();
        handler.date=(TextView) row.findViewById(R.id.dateTextView);
        handler.time=(TextView)row.findViewById(R.id.timeTextView);
        handler.switchButton=(SwitchButton)row.findViewById(R.id.sb_use_listener);
        row.setTag(handler);
    }
    else {
        handler=(DataHandler) row.getTag();

    }
    DateTime dataprovider;
    dataprovider=(DateTime) this.getItem(position);
    handler.date.setText(String.valueOf(dataprovider.getHr()+":"+String.valueOf(dataprovider.getMin())));
    handler.date.setTypeface(EasyFonts.caviarDreamsBold(getContext()));
    handler.time.setText(String.valueOf(dataprovider.getDy()+"/"+String.valueOf(dataprovider.getMon()))+"/"+
            String.valueOf(dataprovider.getYr()));
    handler.time.setTypeface(EasyFonts.caviarDreamsBold(getContext()));
    handler.switchButton.setChecked(true);
    return row;
}

@Override
public void clear() {
    super.clear();
    list.clear();
}

DateTime.java

package com.saransh.try3;

import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alertdialogpro.AlertDialogPro;
import com.ogaclejapan.smarttablayout.utils.v4.Bundler;


/** 
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link interface
* to handle interaction events.
* Use the {@link Alarms#newInstance} factory method to
* create an instance of this fragment.
*/
public class Alarms extends Fragment {
private static final String KEY_PARAM = "key_param";
private ListView listView;
String date="Today";
String time="10:30PM";
private View view;
RowAdapter adapter2;
SQLiteDatabase sqLiteDatabase;
UserDbHelper userDbHelper;
Cursor cursor;
int a,b;


public static PageFragment newInstance(String param) {
    PageFragment f = new PageFragment();
    f.setArguments(arguments(param));
    return f;
}

public static Bundle arguments(String param) {
    return new Bundler()
            .putString(KEY_PARAM, param)
            .get();
}

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

    if(view==null){
        view=inflater.inflate(R.layout.fragment_alarms, container, false);
    }
   /* else {
            ViewGroup parent=(ViewGroup) view.getParent();
            parent.removeView(view);
    }*/



    adapter2=new RowAdapter(getActivity(),R.layout.row_layout);
    listView=(ListView)view.findViewById(R.id.list_view1);
    listView.setAdapter(adapter2);
   // listView.invalidate();
    adapter2.notifyDataSetChanged();
    listView.invalidateViews();
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            //String selectedFromList =(String) (listView.getItemAtPosition(i));
            char hr1,hr2,min1='0',min2;
            String hour12="";
            String minute12="";
            TextView text = (TextView) view.findViewById(R.id.dateTextView);
            String tEXT = text.getText().toString();
            hr1=tEXT.charAt(0);
            if (tEXT.charAt(1)!=':'){
                hr2=tEXT.charAt(1);
                //hour12=hr1+hr2;
                min1=tEXT.charAt(3);
                if (tEXT.length()==5)
                {
                    min2=tEXT.charAt(4);
                }
                else {
                    min2=min1;
                    min1='0';
                }
            }
            else {
                hr2=hr1;
                hr1='0';
                if (tEXT.length()==4)
                {
                    min2=tEXT.charAt(4);
                }
                else {
                    min2=min1;
                    min1='0';
                }

            }
            StringBuilder sb=new StringBuilder();
            sb.append(hr1);
            sb.append(hr2);
            hour12=sb.toString();
            StringBuilder bb=new StringBuilder();
            bb.append(min1);
            bb.append(min2);
            minute12=bb.toString();
            int foo1 = Integer.parseInt(hour12);
            int foo2 = Integer.parseInt(minute12);
            a=foo1;
            b=foo2;
            //Toast.makeText(getActivity().getApplicationContext(),hour12+"yo"+minute12, Toast.LENGTH_SHORT).show();
            AlertDialogPro.Builder builder = new AlertDialogPro.Builder(view.getContext());
            builder.setIcon(R.drawable.cancel_button)
            .setTitle("Do you want to delete the Alarm")
            .setPositiveButton("Yes",new ButtonClickedListener(""))
            .setNegativeButton("No",null).show();
            return true;
        }
    });
    //registerForContextMenu(listView);
    //DateTime dataProvider;
    //dataProvider=new DateTime();
    //adapter2.add(dataProvider);
    userDbHelper= new UserDbHelper(getActivity().getApplicationContext());
    sqLiteDatabase=userDbHelper.getReadableDatabase();
    cursor=userDbHelper.getInformation(sqLiteDatabase);
    if(cursor.moveToFirst()){

        do {
            int hr,min,sec,dy,mon,yr;
            hr=cursor.getInt(0);
            min=cursor.getInt(1);
            sec=cursor.getInt(2);
            dy=cursor.getInt(3);
            mon=cursor.getInt(4);
            yr=cursor.getInt(5);
            DateTime dataProvider=new DateTime(hr,min,sec,dy,mon,yr);
            adapter2.add(dataProvider);
            adapter2.notifyDataSetChanged();
            Log.e("adapter check",""+sec);
        }while(cursor.moveToNext());
    }




    //return inflater.inflate(R.layout.fragment_alarms, container, false);
    return  view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //String param = getArguments().getString(KEY_PARAM);

    // TextView pageText = (TextView) view.findViewById(R.id.page_text);
    //pageText.setText(param);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater menuInflater=getActivity().getMenuInflater();
    //menu.add(Menu.NONE, R.id.id_delete, Menu.NONE, "Menu A");
    menuInflater.inflate(R.menu.context,menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    return super.onContextItemSelected(item);
}

public void onSelect(){
    Intent intent=new Intent(getActivity(),SetAlarm.class);
    startActivity(intent);
}
private class ButtonClickedListener implements DialogInterface.OnClickListener {
    private CharSequence mShowWhenClicked;

    public ButtonClickedListener(CharSequence showWhenClicked) {
        mShowWhenClicked = showWhenClicked;
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
        //userDbHelper=new UserDbHelper(context2);
        //sqLiteDatabase=userDbHelper.getWritableDatabase();
        //userDbHelper.addInformations(hr,min,sec,dy,mon,yr,sqLiteDatabase);
        //Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_LONG);
        //userDbHelper.close();
        //adp.notifyDataSetChanged();
        userDbHelper=new UserDbHelper(getActivity().getApplicationContext());
        sqLiteDatabase=userDbHelper.getReadableDatabase();
        userDbHelper.deleteInformation(a,b,sqLiteDatabase);
        int a1,a2,a3,a4,a5,a6;
        DateTime dataProviderex=new DateTime(23,16,0,8,7,2016);

        adapter2.remove(dataProviderex);
        adapter2.notifyDataSetChanged();
        //onChange();

        //adapter2.remove();
        //adapter2.notifyDataSetChanged();
        //listView.invalidateViews();

    }
    public void onChange(){
        adapter2.notifyDataSetChanged();
        //adapter2.notifyAll();
    }
}
}

我尝试过使用notifyOnDataSetChanged(); 那没起效。然后我尝试了inavlidateViews(),但没有成功。 然后正如你在代码中看到的那样,我试图将自己的DateTime值放在已存储在数据库中以检查它是否删除它们。但仍然没有。

在单个活动中,我有一个由查看分页器运行的3个片段。我希望每当我删除(告诉我删除后的自动更新我会复制它添加) 我需要列表视图自动更新。请帮忙。

0 个答案:

没有答案