Load ListView via an API

0

When I run the app, the ListView does not appear on the screen. Is the volley not making 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());
}
}

My 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:

  

[{"id": 1, "publication_time": "2015-05-20", "title": "Lab 2 opened", "reference": " Lorem Ipsum is simply a text simulation of the typographic and print industry, and has been used since the beginning of the 20th century. 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>
    
asked by anonymous 28.05.2015 / 21:19

0 answers