Adapter View始终在Fragment中返回null

时间:2018-05-09 23:50:37

标签: android listview searchview custom-adapter

我在片段中有一个自定义适配器,并设置为列表视图。一切正常,直到我想在顶部实现搜索栏并相应地设置适配器。一切都很顺利但是当我搜索列表并单击列表中搜索到的项目时,它会得到错误的索引。我也明白为什么,因为在我的代码中,它从“usdtCoinList”获取索引。

从过去的5-6小时开始,我尝试了不同的东西,但是adapterView.getItemAtPosition()方法或者adapterView.getParent()。getItem(i)总是返回null。你能看一下代码吗?

package com.example.ammarkhan.instagramclone.Search;

import android.content.Context;
import android.content.Intent;
import android.graphics.ColorSpace;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
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.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ammarkhan.instagramclone.R;
import com.example.ammarkhan.instagramclone.SingleCoinMarketActivity;
import com.miguelcatalan.materialsearchview.MaterialSearchView;
import com.squareup.picasso.Picasso;

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

public class USDTMarketFragment extends Fragment implements MarketDataInterfaceHandler {

    List<MarketDataHandler> usdtCoins;
    MarketDataFetchAPI marketDataFetchAPI;
    MaterialSearchView materialSearchView;
    CustomAdapter customAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        setHasOptionsMenu(true);

        marketDataFetchAPI = new MarketDataFetchAPI(getContext(),this);

        marketDataFetchAPI.executeLoopjCall();
        View view = inflater.inflate(R.layout.fragment_usdtmarket,container,false);




        return view;
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
       materialSearchView = (MaterialSearchView) getActivity().findViewById(R.id.searchView);

        inflater.inflate(R.menu.menu_item,menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);
        materialSearchView.setMenuItem(menuItem);
//        materialSearchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
//            @Override
//            public void onSearchViewShown() {
//                Log.i("TExT","Inside Shown");
//            }
//
//            @Override
//            public void onSearchViewClosed() {
////                Log.i("TExT","Inside Closed");
//                ListView listView  = getView().findViewById(R.id.usdtMarketCoins);
////               customAdapter = new CustomAdapter();
////                listView.setAdapter(customAdapter);
//            }
//        });



//        materialSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String query) {
//                return false;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String newText) {
//
//                Log.i("TExT",newText);
//                List<String> lstFound = new ArrayList<>();
//                if(newText !=null && !newText.isEmpty()){
//                    for (MarketDataHandler item:usdtCoins) {
//                        if(item.currencyName.contains(newText.toUpperCase()) || item.currencyName.contains(newText.toLowerCase())){
//                            lstFound.add(item.currencyName);
//                        }
//                    }
//
//                    ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_expandable_list_item_1,lstFound);
//
//                    ListView listView  = getView().findViewById(R.id.usdtMarketCoins);
//                    listView.setAdapter(arrayAdapter);
//
//                }else{
////                    ListView listView  = getView().findViewById(R.id.usdtMarketCoins);
////                    customAdapter = new CustomAdapter();
////                    listView.setAdapter(customAdapter);
//                }
//                return true;
//            }
//        });



    }



    public void taskCompleted(List<MarketDataHandler> btcMarketCoins, List<MarketDataHandler> ethMarketCoins, final List<MarketDataHandler> usdtMarketCoins){

        usdtCoins = usdtMarketCoins;


        ListView listView = (ListView) getView().findViewById(R.id.usdtMarketCoins);

        customAdapter = new CustomAdapter();

        listView.setAdapter(customAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getActivity(), usdtCoins.get(i).getCurrencyName(), Toast.LENGTH_SHORT).show();

                if(adapterView.getCount() == 0){
                    Log.i("ADAPTER COUNT ", "THE ADAPTER COUNT IS 0");
                }


                Log.i("DATA FROM ADAPTR VIEW", String.valueOf(adapterView.getAdapter().getItem(i)));

                Log.i("ADAPTER COUNT",String.valueOf(adapterView.getCount() +"and ID is " + adapterView.getId()));

//                Log.i("ADAPTER VIEW ITEM", adapterView.getItemAtPosition(i).toString());
                Intent intent = new Intent(getActivity(), SingleCoinMarketActivity.class);

                intent.putExtra("currencyName", usdtMarketCoins.get(i).getCurrencyName());
                intent.putExtra("baseMarket", "USDT");
//                intent.putExtra("currencyAmountAvailable", btcMarketCoins.get(i).getCurrencyAmount());
                startActivity(intent);

            }
        });


        Log.i("USDT COIN Size", Integer.toString( usdtCoins.size()));


    }


    class CustomAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return usdtCoins.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = getLayoutInflater().inflate(R.layout.fragment_usdt_market_customlist,null);
            TextView currencyName = (TextView) view.findViewById(R.id.marketCurrencyName);
            ImageView imageView = (ImageView) view.findViewById(R.id.marketCurrencyLogo);


            currencyName.setText(usdtCoins.get(i).getCurrencyName());

            if(usdtCoins.get(i).getImageURL().equalsIgnoreCase("https://bittrex.com/Content/img/symbols/BTC.png")){
                imageView.setImageResource(R.drawable.ic_bitcoin);
            } else{
                Picasso.get().load(usdtCoins.get(i).getImageURL()).resize(80,80).into(imageView);
            }


            Log.i("usdt Coins Size", Integer.toString( usdtCoins.size()));
            return view;
        }
    }



}

当我更改以下行时:

 intent.putExtra("currencyName", usdtMarketCoins.get(i).getCurrencyName());

为:

intent.putExtra("currencyName", adapterView.getItemIdAtPosition(i));

它将抛出一个空指针异常。

1 个答案:

答案 0 :(得分:0)

由于你的CustomAdapter,它会抛出NPE。

getItem(int i)始终返回null。这就是问题。另外,更改getItemId(int i)方法。它总是返回0.

这是固定代码:

    @Override
    public Object getItem(int i) {
        return usdtCoins.get(i);
    }

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