The adapter doesn't show the current element?

Viewed 53

The adapter shows only one item will JSON has more than that number how can the adapter shows all the JSON elements? i have loader and it is only shows one element and repeat it? the JSON ger elements doesn't show in the adapter I tried things but it doesn't work !! how can I show and take every single element from the response.

the image shows the elements

the image shows the elements

the adapter:

  public class shelfsAdapter extends ArrayAdapter < shelfs > {
      public shelfsAdapter(MainActivity context, ArrayList < shelfs > objects) {
          super(context, 0, objects);
      }@
      Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View listItem = convertView;
          if (listItem == null) {
              listItem = LayoutInflater.from(getContext())
                  .inflate(R.layout.item, parent, false);
          }
          shelfs current = getItem(position);
          TextView title = (TextView) listItem.findViewById(R.id.title);
          title.setText(current.getTitle());
          TextView author = (TextView) listItem.findViewById(R.id.author);
          author.setText(current.getAuthors());
          return listItem;
      }
  }

the loader:

    public class LoaderApp extends AsyncTaskLoader<List<shelfs>> {
      private String url;


      public LoaderApp( Context context,String mUrl) {
        super(context);
        url = mUrl;
      }

      @Override
      protected void onStartLoading() {
        Log.v("forceload","force close dossseeeeeee!!!");
        forceLoad();
      }

      @Override
      public  List<shelfs> loadInBackground(){
        if(url==null){return  null;}
        Log.v("get books","get books");
        List<shelfs> books = QueryUtils.fetchData(url);
        Log.v("get query","get query");
        Log.v("equals query","equals"+books.get(0)+""+books.get(2));
        //  books.add(new shelfs("books","books inback "));
        return books;
      }

the main Activity:

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<shelfs>> {
  private   shelfsAdapter adapter;
  private static final String url = "https://www.googleapis.com/books/v1/volumes?q={search%20terms}";
  private static final int ID=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     //final String url1="  https://www.googleapis.com/books/v1/volumes?q=android&maxResults=1\n";
         adapter = new shelfsAdapter(this, new ArrayList<shelfs>());
        ListView listView = (ListView) findViewById(R.id.list);
        Log.v("geturl fine","fine get it");
        listView.setAdapter(adapter);

        ConnectivityManager connMgr = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);

        // Get details on the currently active default data network
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // If there is a network connection, fetch data
        if (networkInfo != null && networkInfo.isConnected()) {
            // Get a reference to the LoaderManager, in order to interact with loaders.
            android.app.LoaderManager loaderManager = getLoaderManager();
            Log.v("loading ","loader inflater");
            // Initialize the loader. Pass in the int ID constant defined above and pass in null for
            // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
            // because this activity implements the LoaderCallbacks interface).
            getSupportLoaderManager().initLoader(ID, null, this);
        }}




    @NonNull
    @Override
    public Loader<List<shelfs>> onCreateLoader(int id, @Nullable Bundle args) {
        Log.v("get load","get load on create");
        Toast.makeText(this,"creat it just!!!",Toast.LENGTH_SHORT).show();
        adapter.add(new shelfs("oncreat","loadApp on create loader"));

        return new LoaderApp(this,url);


    }

    @Override
    public void onLoadFinished(@NonNull Loader<List<shelfs>> loader, List<shelfs> data) {
    if(data != null && !data.isEmpty() )
        Log.v("get load","get load on finished");
       // adapter.add(new shelfs("onloadfinish","addall??"));
        adapter.addAll(data);
        adapter.notifyDataSetChanged();

        // Log.v("data add","data"+data.get(0)+""+data.get(6));
       // Toast.makeText(this,"data",Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onLoaderReset(@NonNull Loader<List<shelfs>> loader) {
        Log.v("get load","get load on reset");
        adapter.clear();

    }

and the json class:

public class QueryUtils {
    public QueryUtils(){}

    public static List<shelfs> fetchData(String requestUrl)  {
        if (requestUrl ==null) {return null;}
        Log.v("fetchData work!","fetchData work!");
            URL uRl=getUrl(requestUrl);
        Log.v("geturl work!","geturl work!");
        String json=null;
        try {
             json=makeHttprequest(uRl);
            Log.v("makehttp work!","makehttp work!");
        }catch (IOException e){Log.v("error fetch","error fetch");}
        List<shelfs> makeit=ExtractJsonData(json);
        Log.v("extractdata work!","extractdata work!");
        return makeit;
    }

    public static URL getUrl(String urlhttp) {
        URL url=null;
        try{ url=new URL(urlhttp);}
        catch (MalformedURLException e){Log.v("url error","url error",e);}
        return url;
    }


    public static String readStream(InputStream inputStream) throws IOException {
        StringBuilder stringBuilder=new StringBuilder();
        if(inputStream!=null)
        { InputStreamReader isr=new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader reader=new BufferedReader(isr);
        String line=reader.readLine();
        while(line!=null){stringBuilder.append(line);
        line=reader.readLine();}}
        return stringBuilder.toString();
    }

    public static String makeHttprequest(URL url)throws IOException{
        String jsonResponse="";
        InputStream inputStream=null;
        HttpURLConnection urlConnection=null;
        try {
            urlConnection=(HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);
            urlConnection.connect();
            if(urlConnection.getResponseCode()==200)
            {inputStream=urlConnection.getInputStream();
            jsonResponse=readStream(inputStream);
                Log.e("connect error","connect");}}
            catch(IOException e){Log.e("connect error","connect error");}
            finally{
                if(urlConnection!=null){urlConnection.disconnect();}
                if(inputStream!=null){inputStream.close();}
            }
            return jsonResponse; }


    public static List<shelfs> ExtractJsonData(String jsonResponse){
        List<shelfs> putshelfs=new ArrayList<>();
        try {
            JSONObject base=new JSONObject(jsonResponse);
            JSONArray items=base.getJSONArray("items");
            shelfs firstShelfs=new shelfs("","");
            for(int i=0;i<items.length();i++)
            {
                JSONObject index = items.getJSONObject(i);
                JSONObject volume=index.getJSONObject("volumeInfo");
                String title=volume.getString("title");
                JSONArray authors=volume.getJSONArray("authors");
                String auth=authors.getString(0);
                 firstShelfs= new shelfs(title,auth);
                putshelfs.add(i,firstShelfs);

                Log.v("gggg","net get"+i+auth+title+"dom");}
            Log.v("dff","json take");
        } catch (JSONException e) {
            e.printStackTrace(); }

        return  putshelfs;}

}
0 Answers
Related