Необратимые типы;не может привести 'android.content.Context' к 'com.example.xxxxx.PendingListFragment' - PullRequest
0 голосов
/ 29 июня 2019

Я пытаюсь установить кнопку при просмотре списка, используя 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 голосов
/ 29 июня 2019

Ошибка возникает из-за того, что вы пытаетесь ввести приведение текущего контекста приложения к PendingListFragment , что неверно, и поскольку вам просто нужно вызвать метод вашего PendingListFragment всем вамнужно создать его объект, который можно получить в Java, используя ключевое слово new .

Для этого просто напишите

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 является общедоступным.

0 голосов
/ 29 июня 2019

PendingListFragment pendingListFragment = new PendingListFragment(); инициализировать как это

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...