尝试从另一个活动中的MainActivity调用方法导致我出错

时间:2019-01-05 17:18:06

标签: java android android-recyclerview

我有Recycler ListView,它在MainActivity中显示,并且它的第一项被选中,我已经完成了单击以查找其他项的操作,但是最后一次单击仍然单击,并且当我尝试采用此Recycler视图在下一个活动中向我显示时不起作用。

它被选中的第一项,但我有一个单击方法,当单击图像时,该方法为选中状态,但是当我单击新图像时,该方法有效,但第一项保持为选中状态,因此对于其他图像继续进行。我只希望选择一张图像。

我不想写两次相同的代码。 如果我只为该方法提供一个类,可以随时使用它,那会很好吗? 回收者视图列表的ID在两个xml上都相同。 如果您对我的问题有任何建议,请告诉我。

这是RecyclerView的适配器。

public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.ViewHolder>{
    private int selectedItem;

    private ArrayList<Integer> mImages = new ArrayList<>();
    private ArrayList<String> mSearchUrl = new ArrayList<>();
    private Context mContext;

    public ListViewAdapter(ArrayList<Integer> images, ArrayList<String> SearchUrl, Context context) {
        mImages = images;
        mContext = context;
        mSearchUrl = SearchUrl;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.s_engine_item, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        selectedItem = 0;
        if (selectedItem == i) {
            viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
        }
        Glide.with(mContext).load(mImages.get(i))
                .into(viewHolder.image);
       viewHolder.searchUrl.setText(mSearchUrl.get(i));

       viewHolder.image.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               viewHolder.image.setBackgroundColor(Color.parseColor("#30000000"));
               selectedItem = i;


           }
       });

    }

    @Override
    public int getItemCount() {
        return mImages.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        ImageView image;
        TextView searchUrl;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.ivEngine);
        searchUrl = itemView.findViewById(R.id.ivEngineText);
    }
}
}

这是MainActivity.class中的方法

public void intSearch() {
    mImages.add(R.drawable.s_bing);
    mSearchUrl.add("https://www.bing.com/search?q=");

    mImages.add(R.drawable.s_google);
    mSearchUrl.add("https://www.google.com/search?q=");

    mImages.add(R.drawable.s_yahoo);
    mSearchUrl.add("www.yahoo.com");

    mImages.add(R.drawable.amazon_white256);
    mSearchUrl.add("www.amazon.com");

    mImages.add(R.drawable.amazon_white256);
    mSearchUrl.add("www.amazon.com");

    mImages.add(R.drawable.amazon_white256);
    mSearchUrl.add("www.amazon.com");

    initRecyclerView();
}
private void initRecyclerView() {
    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    RecyclerView recyclerView = findViewById(R.id.lvEngines);
    recyclerView.setLayoutManager(layoutManager);
    ListViewAdapter adapter = new ListViewAdapter(mImages, mSearchUrl, this);
    recyclerView.setAdapter(adapter);
}

这是转到其他活动的按钮。

btnSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String newEntry = searchPlugin.getText().toString();
            Cursor data = mDatabaseHelper.getData();
                AddHistory(newEntry);
                getFragmentRefreshListener().onRefresh();
                Intent intent = new Intent(MainActivity.this, ActivitySearchEngine.class);
                intent.putExtra("name", newEntry);
                intent.putExtra("test", mSearchUrl.get(0));
                startActivityForResult(intent, 2);

        }
    });

这是另一项活动

public class ActivitySearchEngine extends Activity implements 
SwipeRefreshLayout.OnRefreshListener {
public ImageView mHome;
public EditText searchPlugin;
public WebView webView;
Button btnSearch;
public ImageButton clearSearch, exitButton;
public ImageView favIcon;
public ProgressBar loadIcon;
String text;
SwipeRefreshLayout refreshLayout;
DatabaseHelper mDatabaseHelper;

private String selectedName;
private int selectedID;
private String selectedSearchUrl;
RecyclerView mListView;
MainActivity mainActivity = new MainActivity();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    mHome = findViewById(R.id.imgBtnHome);
    searchPlugin = findViewById(R.id.etSearch);
    webView = findViewById(R.id.webView);
    clearSearch = findViewById(R.id.btnClearSearch);
    btnSearch = findViewById(R.id.btnSearch);
    favIcon = findViewById(R.id.imgViewFavIcon);
    loadIcon = findViewById(R.id.progressBarIcon);
    exitButton = findViewById(R.id.imgBtnStopLoad);
    refreshLayout = findViewById(R.id.refreshLayout);
    mListView = findViewById(R.id.lvEngines);
    refreshLayout.setOnRefreshListener(this);
    mDatabaseHelper = new DatabaseHelper(this);
    mainActivity.intSearch(); // Here it is the error 
Activity.ActivitySearchEngine}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

    Intent receivedIntent = getIntent();
    selectedName = receivedIntent.getStringExtra("name");
    selectedID = receivedIntent.getIntExtra("id",-1); //NOTE: -1 is just the default value
    selectedSearchUrl = receivedIntent.getStringExtra("test");

    searchPlugin.setText(selectedName);
    loadIcon.setVisibility(View.VISIBLE);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("www.bing.com/search?=" + selectedName);

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
            favIcon.setImageBitmap(icon);
        }
    });
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            loadIcon.setVisibility(View.VISIBLE);
            favIcon.setVisibility(View.GONE);
        }

        public void onPageFinished(WebView view, String url) {
            try {
                if (loadIcon.getVisibility() == View.VISIBLE) {
                    loadIcon.setVisibility(View.GONE);
                    favIcon.setVisibility(View.VISIBLE);
                    btnSearch.setVisibility(View.GONE);
                    mHome.setVisibility(View.VISIBLE);
                    exitButton.setVisibility(View.GONE);
                    clearSearch.setVisibility(View.VISIBLE);
                    favIcon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onRefresh();
                        }
                    });
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    });



            searchPlugin.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            processButtonByTextLength();
        }
    });


    mHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchPlugin.setText(null);
            finish();
        }
    });

    clearSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchPlugin.setText("");
        }
    });

public void processButtonByTextLength() {
    String inputText = searchPlugin.getText().toString();
    if(inputText.length() > 0)        {
        btnSearch.setVisibility(View.VISIBLE);
        mHome.setVisibility(View.GONE);
        clearSearch.setVisibility(View.VISIBLE);
        favIcon.setVisibility(View.VISIBLE);
        loadIcon.setVisibility(View.GONE);
        exitButton.setVisibility(View.GONE);
    } else if(inputText.length() == 0) {
        btnSearch.setVisibility(View.GONE);
        mHome.setVisibility(View.VISIBLE);
        clearSearch.setVisibility(View.GONE);

    }
}

@Override
public void onRefresh() {
    webView.reload();
    refreshLayout.setRefreshing(false);
}

}

这是MainActivity.class上带有RecyclerView的照片 enter image description here

其他活动的照片 enter image description here

0 个答案:

没有答案