Loading Listview via an API

Asked

Viewed 215 times

0

When I run the app, Listview is not displayed on the screen. Is Volley not doing the request? or am I doing the wrong JSON Parsing?

Main Activity:

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar ab = getActionBar();
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));

    // TABS
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        Tab tab1 = ab.newTab();
        tab1.setText("Notícias");
        tab1.setTabListener(new NavegacaoTabs(new Fragment1()));
        ab.addTab(tab1, false);

        Tab tab2 = ab.newTab();
        tab2.setText("Estágio");
        tab2.setTabListener(new NavegacaoTabs(new Fragment2()));
        ab.addTab(tab2, false);

        Tab tab3 = ab.newTab();
        tab3.setText("Cursos");
        tab3.setTabListener(new NavegacaoTabs(new Fragment3()));
        ab.addTab(tab3, false);

    if(savedInstanceState != null) {
        int indiceTab = savedInstanceState.getInt("indiceTab");
        getActionBar().setSelectedNavigationItem(indiceTab);
    } else {
        getActionBar().setSelectedNavigationItem(0);
    }       
}

private class NavegacaoTabs implements ActionBar.TabListener {
    private Fragment frag;

    public NavegacaoTabs(Fragment frag){
        this.frag = frag;
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
        Log.i("Script", "onTabReselected()");
    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
        fts.replace(R.id.fragmentContainer, frag);
        fts.commit();
    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
        FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
        fts.remove(frag);
        fts.commit();
    }
}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putInt("indiceTab", getActionBar().getSelectedNavigationIndex());
}
}

Meu Fragment:

public class Fragment1 extends ListFragment {
List<Notice> result = new ArrayList<Notice>();
NoticeAdapter adpt;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.list_notice, container, false);
    ListView lView = (ListView) view.findViewById(android.R.id.list);

    JsonArrayRequest jReq = new JsonArrayRequest("http://192.168.1.101:3000/notices",
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            Notice notice = new Notice();
                            notice.setId(response.getJSONObject(i).getInt("id"));
                            notice.setPicture(response.getJSONObject(i).getString("picture"));
                            notice.setPublicationTime(response.getJSONObject(i).getString("publication_time"));
                            notice.setReducedDescription(response.getJSONObject(i).getString("reduced_description"));
                            notice.setReference(response.getJSONObject(i).getString("reference"));
                            notice.setTitle(response.getJSONObject(i).getString("title"));
                            result.add(notice);
                        } catch (JSONException e) {
                            Toast.makeText(getActivity(), "Falha com a conexão de internet", Toast.LENGTH_LONG).show();
                        }
                    }
                    adpt.setItemList(result);
                    adpt.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
            });
    Volley.newRequestQueue(getActivity().getApplicationContext()).add(jReq);
    adpt = new NoticeAdapter(result, this.getActivity());
    lView.setAdapter(adpt); 

    return view;
}
}

My Adapter:

public class NoticeAdapter extends BaseAdapter {

List<Notice> itemList;
LayoutInflater inflater;
Context context;

public NoticeAdapter(List<Notice> itemList, Context context) {
    this.itemList = itemList;
    this.context = context;       
}

public int getCount() {
    if (itemList != null)
        return itemList.size();
    return 0;
}

public Notice getItem(int position) {
    if (itemList != null)
        return itemList.get(position);
    return null;
}

public long getItemId(int position) {
    if (itemList != null)
        return itemList.get(position).hashCode();
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;

    if(view == null) {           
        view = inflater.inflate(R.layout.list_notice, null);
        holder = new ViewHolder();
        view.setTag(holder);

        holder.tvId = (TextView) view.findViewById(R.id.title);
        holder.tvDesc = (TextView) view.findViewById(R.id.description);
        holder.tvPtime = (TextView) view.findViewById(R.id.publication_time);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.tvId.setText(itemList.get(position).getTitle());
    holder.tvDesc.setText(itemList.get(position).getDescription());
    holder.tvPtime.setText(itemList.get(position).getPublicationTime());

    return view;
}

private static class ViewHolder {
    TextView tvId;
    TextView tvDesc;
    TextView tvPtime;
}

public List<Notice> getItemList() {
    return itemList;
}

public void setItemList(List<Notice> itemList) {
    this.itemList = itemList;
}

}

JSON file returned from:

[{"id":1,"publication_time":"2015-05-20","title":"Laboratory 2 opened","Reference":"http://www.fatec.gov.br","picture":"/usr/bin/locales/fatec.png","Description":"Lorem Ipsum is simply a simulation of the typesetting and printing industry, and has been used since the 16th century, when an unknown printer picked up a tray of guys and shuffled them to make a book of types models." ,"reduced_description":"Lorem ipsum dolor, sit Amet"},{"id":2,"publication_time":"2015-05-20","title":"Laboratory 1 inaugurated","":"http://www.fatec.gov.br/1","picture":"/usr/bin/locales/fatec.png","Description":"Lorem Ipsum is simply a simulation of text from the printing and printing industry, and has been used since the 16th century","reduced_description":"Lorem ipsum dolor, sit Amet"}]

Itemlayout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/publication_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Listnotice xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />

</LinearLayout>
  • The code seems ok to me. Nevertheless, the class documentation ListFragment suggests to do different: You must use Listfragment.setListAdapter() to Associate the list with an Adapter. Do not directly call Listview.setAdapter() or Else Important initialization will be skipped. Similarly, it is recommended to use what the class ListFragment offers (I’m talking about the method getListView()) to access the list, otherwise the fragment may be a simple Fragment even.

  • How you are creating/displaying the Fragment? It is displayed?

  • It displays text with the project name, only

  • I’m running the app by my phone, and using wifi (the api is on another machine on the same network), I think that’s not the problem, right?

  • Could it be something in Mainactivity? I used as inherited class Fragment same, I took Listfragment

  • The text he was displaying was in the Mainactivity Layout, I’m sorry. I’ve already removed it. Now nothing appears, he was supposed to render the list.

  • I figured out what it was by not appearing the list, I needed to add the permission tag to internet. I added but got an error now, this one: java.lang.Nullpointerexception: Attempt to invoke virtual method 'android.view.View android.view.Layoutinflater.inflate(int, android.view.Viewgroup)' on a null Object Reference

  • It was an Adapter getview method error. view = Inflater.inflate(R.layout.list_notice, null);

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.