从适配器类内的Fragment类运行方法

时间:2016-08-09 22:50:00

标签: android

我希望能够从HistoryAdapter类的HistoryFragment类中运行一个方法。我已经评论了我想要运行此方法的位置。如果你能看到我正试图做的另一种方式我全都耳朵。我希望文本视图显示" No History Available"当项目列表为空时(没有显示数据)。很想听听你的意见! 感谢

这是我的适配器类,用于创建自定义列表视图:

package uk.co.rascagneres.clocking_app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Jacques on 08/08/2016.
 */
public class HistoryAdapter extends BaseAdapter implements ListAdapter{
    private  ArrayList<String> list = new ArrayList<String>();
    private Context context;
    public ArrayList<String> idArray = new ArrayList<String>();
    DatabaseHandler db;


    public HistoryAdapter (ArrayList<String> list, Context context, ArrayList<String> idArray, DatabaseHandler db){
        this.list = list;
        this.context = context;
        this.idArray = idArray;
        this.db = db;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        View view = convertView;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.history_list, null);
        }
        final View innerView = view;
        TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
        listItemText.setText(list.get(position));

        Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);

        deleteBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                db.deleteRowID(Integer.parseInt(idArray.get(position)));
                list.remove(position);
                notifyDataSetChanged();
                idArray.remove(position);
                //RUN setText() in HistoryFragment from here!
            }
        });
        return view;
    }
}

这是我的HistoryFragment类:

package uk.co.rascagneres.clocking_app;

import android.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

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

/**
 * Created by Jacques on 03/08/2016.
 */
public class HistoryFragment extends Fragment {
    DatabaseHandler db;
    View myView;
    ArrayList<String> historyArray = new ArrayList<String>();
    ArrayList<String> idArray = new ArrayList<String>();



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.history_layout, container, false);
        db = new DatabaseHandler(getActivity());

        ArrayList<String> list = insertData();

        HistoryAdapter adapter = new HistoryAdapter(list, getActivity(), idArray, db);

        ListView lView = (ListView)myView.findViewById(R.id.listView);
        lView.setAdapter(adapter);
        return myView;
    }

    public ArrayList<String> insertData(){
        Cursor c = db.getAllData();
        ArrayList<String> list = new ArrayList<String>();
        while(c.moveToNext()) {
            int id = c.getInt(0);
            long in = c.getLong(1);
            long out = c.getLong(2);
            String outTime;

            if(out == 0){
                outTime = "N/A";
            }else{
                outTime = ClockingFragment.getDate(out, "HH:mm");
            }

            String date = ClockingFragment.getDate(in, "EEE, MMM d");
            String inTime = ClockingFragment.getDate(in, "HH:mm");

            historyArray.add(date + "\n Clocked In: " + inTime + "\n Clocked Out: " + outTime + "\n");
            idArray.add(String.valueOf(id));
        }
        TextView histAvail = (TextView)myView.findViewById(R.id.noHist);
        if (historyArray.isEmpty()){
            histAvail.setText("No History Available!");
        }else {
            histAvail.setText("");
        }
        return historyArray;
    }

    public ArrayList<String> getIdArray(){
        System.out.println(idArray);
        return idArray;
    }

    public void setText(){
        TextView histAvail = (TextView)myView.findViewById(R.id.noHist);
        histAvail.setText("No History Available!");
    }


}

1 个答案:

答案 0 :(得分:1)

更新: 创建一个界面。像这样:

<强> HistoryAdapterListener.java

public interface HistoryAdapterListener {
    void callSetText();
}

适配器中为HistoryAdapterListenerContext创建对象。(就在DatabaseHandler db的下方)

private HistoryAdapterListener mHistoryAdapterListener;
private Context mContext;

并在构造函数中设置它们。我们将传递HistoryAdapterListener参数的HistoryFragment实例(请参阅如何更新片段以实现下面的接口)。

public HistoryAdapter (ArrayList<String> list, Context context, ArrayList<String> idArray, DatabaseHandler db, HistoryAdapterListener listener){
        this.list = list;
        this.context = context;
        this.idArray = idArray;
        this.db = db;
        //here my codes begin
        mContext = context;
        mHistoryAdapterListener = listener;
        //here it is ended
    }

我们仍在您的适配器中。我们还没有完成。让我们调用接口方法。

 @Override
    public View getView(final int position, View convertView, ViewGroup parent){
        ...
        deleteBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                ...
                //RUN setText() in HistoryFragment from here!
                if(mHistoryAdapterListener != null){
                mHistoryAdapterListener.callSetText();
                }
            }
        });
        return view;
    }

现在。我们在您的适配器中调用接口方法。我们将接口对象设置为创建者上下文的引用。所以现在实现你的片段的接口,所以当接口方法将调用时,你的工具方法将触发。 在片段中,首先实现界面。

public class HistoryFragment extends Fragment implements HistoryAdapterListener{ 
... 
}

执行此操作后,Android Studio会警告您覆盖已实现类的方法。 所以在你的片段中再次覆盖实现的接口方法。

@Override
public void callSetText(){
setText();
}

那就是它!

编辑:抱歉。我没有使用IDE来获得这个答案。有一些错误。我更新了。再去看看。