不可转换的类型;无法将'android.content.Context'强制转换为'com.example.xxxxx.PendingListFragment'

时间:2019-06-29 19:04:30

标签: android android-fragments onclicklistener

我尝试使用CursorLoader在列表视图上设置按钮。但这给了我错误消息

  

不可转换的类型;无法将“ android.content.Context”转换为“ com.example.xxxxx.PendingListFragment”。

我是android新手,请回答。

@Override
public void bindView(View view, final Context context, Cursor cursor) {

    final Button signUpButton = view.findViewById(R.id.sign_up_button);

    TextView nameTextView = view.findViewById(R.id.name);
    final int id = cursor.getColumnIndex(InfoContract.InfoEntry._ID);
    int nameColumnIndex = 
    cursor.getColumnIndex(InfoContract.InfoEntry.COLUMN_NAME); 

    String name = cursor.getString(nameColumnIndex);
    String phoneNumber = cursor.getString(phoneNumberColumnIndex);
    final String col = cursor.getString(id);

    nameTextView.setText(name);
    signUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


                PendingListFragment pendingListFragment =  (PendingListFragment)context;
                pendingListFragment.upDateStatus(Integer.valueOf(col), InfoContract.InfoEntry.STATUS_SIGN_UP);

        }
    });
}

PendingListFragment的代码在这里

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.xxxxx.data.InfoContract;

public class PendingListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int INFO_LOADER = 0;
    PendingListCursorAdapter pendingListCursorAdapter;
    Cursor cursor;
    private ListView mInfoListView;
    private View mEmptyView;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstantState) {
        View view = inflater.inflate(R.layout.fragment_pending_list, container, false);
        mInfoListView = view.findViewById(R.id.pending_list_view);
        mEmptyView = view.findViewById(R.id.empty_view);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    }

    public void onViewCreated(@NonNull View view, Bundle savedInstantState) {
        super.onViewCreated(view, savedInstantState);
        getActivity().setTitle("Pending List");
        pendingListCursorAdapter = new PendingListCursorAdapter(getActivity(), null);
        mInfoListView.setAdapter(pendingListCursorAdapter);

        getLoaderManager().initLoader(INFO_LOADER, null, PendingListFragment.this);


        mInfoListView.setEmptyView(mEmptyView);

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
                InfoContract.InfoEntry._ID,
                InfoContract.InfoEntry.COLUMN_NAME,
                InfoContract.InfoEntry.COLUMN_MOBILE_NUMBER,
                InfoContract.InfoEntry.COLUMN_DATE,
                InfoContract.InfoEntry.COLUMN_STATUS
        };
        return new CursorLoader(getContext(),
                InfoContract.InfoEntry.CONTENT_URI,
                projection,
                null,
                null,
                null);
    }
    public void upDateStatus(Integer id, int status) {
        ContentValues values = new ContentValues();
        values.put(InfoContract.InfoEntry.COLUMN_STATUS, status);
        Uri upDateUri = ContentUris.withAppendedId(InfoContract.InfoEntry.CONTENT_URI, id);
        int rowsAffected = getActivity().getContentResolver().update(upDateUri, values, null, null);
    }


    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
        pendingListCursorAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        pendingListCursorAdapter.swapCursor(null);

    }
}
`



when button is clicked it need to update database.

2 个答案:

答案 0 :(得分:0)

PendingListFragment pendingListFragment = new PendingListFragment();初始化

答案 1 :(得分:0)

抛出该错误是因为您试图将应用程序的当前上下文键入强制转换 PendingListFragment ,这是错误的,并且因为您只需要调用PendingListFragment的方法即可您需要做的就是创建它的对象,可以使用 new 关键字在Java中实现。

为此,只需编写

PendingListFragment pendingListFragment = new PendingListFragment(); //this will create new object of your PendingListFragmnet which will allow you to access it's method.
pendingListFragment.upDateStatus(Integer.valueOf(col), InfoContract.InfoEntry.STATUS_SIGN_UP);

还要确保您的upDateStatus方法是公开的。