FORUMS
Remove All Ads from XDA

Dealing with Custom ListView and Fragments

18 posts
Thanks Meter: 0
 
By grotteschie, Junior Member on 5th April 2014, 08:39 PM
Post Reply Subscribe to Thread Email Thread
Hi guys! Well, i am facing an issue when trying to develop my app. Let me explain what i have done:

1) Create a Login to connect to a MySQL Database to validate the user (Works great)
2) Create a Main activity
3) Create a Drawer, with a Drawer Adapter, to show the options of my dramer menu. (works great)
As you know the drawer works with fragments, so i have one fragment for each menu option.
4) Well, here starts my problem...

The first option on my menu is "Fixture" where i want to show a list of matchs with the differents results. That list, is on a MySql DB.
So, on my fragment (FixtureFragment) i have this code:

Code:
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.lxp.app.adapter.FixtureAdapter;
import com.lxp.app.model.FixtureItem;
import com.lxp.app.tools.JSONParser;
import com.lxp.app.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FixtureFragment extends Fragment {

    // Progress Dialog
    private ProgressDialog pDialog;

    // url to get all products list
    private static String url_fixture = "http://www.myweb.com/myFixtureData.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_FIXUTRE = "fixture";
    private static final String TAG_EQUIPO1 = "id_equipo_1";
    private static final String TAG_EQUIPO2 = "id_equipo_2";
    private static final String TAG_GEQUIPO1 = "goles_equipo_1";
    private static final String TAG_GEQUIPO2 = "goles_equipo_2";
    private static final String TAG_ID_PARTIDO = "id_partido";

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    // products JSONArray
    JSONArray partidos = null;

    ArrayList<HashMap<Integer, FixtureItem>> fixtureList;

    ListView lv;

    public FixtureFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Hashmap for ListView}
        fixtureList = new ArrayList<HashMap<Integer, FixtureItem>>();

        // Loading products in Background Thread
        new LoadFixture().execute();

        View fixtureView = inflater.inflate(R.layout.fragment_fixture, container, false);

        return fixtureView;
    }
    
    /**
     * Background Async Task to Load the fixture by making HTTP Request
     * */
    class LoadFixture extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Cargando el Fixture...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting the Fixture from url
         * */
        protected String doInBackground(String... args) {

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_fixture, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("Fixture: ", json.toString());

            FixtureItem objFixture = new FixtureItem();

            try {
                // Checking for SUCCESS TAG
                int success = 0;
                success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    partidos = json.getJSONArray(TAG_FIXUTRE);

                    // looping through All Products
                    for (int i = 0; i < partidos.length(); i++) {
                        JSONObject c = partidos.getJSONObject(i);

                        Log.e("Json = ", c.toString());
                        // Storing each json item in variable
                        String equipo1 = c.getString(TAG_EQUIPO1);
                        String equipo2 = c.getString(TAG_EQUIPO2);
                        String golesEq1 = c.getString(TAG_GEQUIPO1);
                        String golesEq2 = c.getString(TAG_GEQUIPO2);
                        Integer idPartido = c.getInt(TAG_ID_PARTIDO);
    
                        // creating new HashMap
                        HashMap<Integer, FixtureItem> map = new HashMap<Integer, FixtureItem>();

                        // adding each child node to HashMap key => value
                        objFixture.setGolesEquipo1(golesEq1);
                        objFixture.setGolesEquipo2(golesEq2);
                        objFixture.setIdequipo1(equipo1);
                        objFixture.setIdequipo2(equipo2);
                        objFixture.setIdPartido(idPartido);

                        map.put(idPartido, objFixture);

                        fixtureList.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();

            // updating UI from Background Thread
            lv.setAdapter(new FixtureAdapter(getActivity(), fixtureList));
        }
    }
}
Well, as you could see, i've created a hashmap to load my fixture data, and then i sent this data (fixtureList) to the custom adapter.

When running this code i am getting this error.
Code:
04-05 16:20:27.899  23500-23500/com.lxp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.lxp.app, PID: 23500
    java.lang.NullPointerException
            at com.lxp.app.drawer.FixtureFragment$LoadFixture.onPostExecute(FixtureFragment.java:175)
            at com.lxp.app.drawer.FixtureFragment$LoadFixture.onPostExecute(FixtureFragment.java:81)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
Line 175 --> lv.setAdapter(new FixtureAdapter(getActivity(), fixtureList));

These are the layouts files:

Fragment_Fixture.xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ListActivity" >

    <ListView
        android:id="@+id/fixture_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
fixture_item.xml

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/idpartido"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"
            android:textSize="17dip"
            android:textStyle="bold"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/golesequipo1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"
            android:textSize="17dip"
            android:textStyle="bold"
            android:text="0"/>

        <TextView
            android:id="@+id/golesequipo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"
            android:textSize="17dip"
            android:textStyle="bold"
            android:text="1"/>
    </LinearLayout>

    <LinearLayout  android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/idequipo1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"
            android:textSize="17dip"
            android:textStyle="bold"
            android:text="Argentinos"/>

        <TextView
            android:id="@+id/idequipo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="6dip"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:paddingBottom="6dip"
            android:textSize="17dip"
            android:textStyle="bold"
            android:text="Lanus"/>
    </LinearLayout>
</LinearLayout>
My fixtureitem class

Code:
public class FixtureItem {
    private Integer idPartido;
    private String idequipo1;
    private String idequipo2;
    private String golesEquipo1;
    private String golesEquipo2;

    public Integer getIdPartido() {
        return idPartido;
    }

    public void setIdPartido(Integer idPartido) {
        this.idPartido = idPartido;
    }
    public String getIdequipo1() {
        return idequipo1;
    }

    public String getIdequipo2() {
        return idequipo2;
    }

    public String getGolesEquipo1() {
        return golesEquipo1;
    }

    public String getGolesEquipo2() {
        return golesEquipo2;
    }

    public void setIdequipo1(String idequipo1) {
        this.idequipo1 = idequipo1;
    }

    public void setIdequipo2(String idequipo2) {
        this.idequipo2 = idequipo2;
    }

    public void setGolesEquipo1(String golesEquipo1) {
        this.golesEquipo1 = golesEquipo1;
    }

    public void setGolesEquipo2(String golesEquipo2) {
        this.golesEquipo2 = golesEquipo2;
    }
}
and my fixture adapter

Code:
public class FixtureAdapter extends BaseAdapter {

    private ArrayList listData;
    private LayoutInflater layoutInflater;

    public FixtureAdapter(Context context, ArrayList listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.fixture_item, null);
            holder = new ViewHolder();
            holder.idequipo1 = (TextView) convertView.findViewById(R.id.idequipo1);
            holder.idequipo2 = (TextView) convertView.findViewById(R.id.idequipo2);
            holder.golesequipo1 = (TextView) convertView.findViewById(R.id.golesequipo1);
            holder.golesequipo2 = (TextView) convertView.findViewById(R.id.golesequipo2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        FixtureItem fixtureItem = (FixtureItem) listData.get(position);

        holder.idequipo1.setText(fixtureItem.getGolesEquipo1());
        holder.idequipo2.setText(fixtureItem.getGolesEquipo2());
        holder.golesequipo1.setText(fixtureItem.getGolesEquipo1());
        holder.golesequipo2.setText(fixtureItem.getGolesEquipo1());

        return convertView;
    }

    static class ViewHolder {
        TextView idequipo1;
        TextView idequipo2;
        TextView golesequipo1;
        TextView golesequipo2;
    }

}
I think i am getting wrong because i couldnt find an example of loading data in an asyncronous way for a list inside a fragment, i know is too expecific this example, but i tried differents ways to do it and i couldnt solve it.

If an experienced dev could give me a hand, i will be glad. Or if you have examples of an app working with "Drawer/Fragment/Custom List inside Fragments/Data Loaded from a MySql for the Custom List" it will be great.

If you need more data, just let me know.
 
 
24th April 2014, 04:49 PM |#2  
Junior Member
Thanks Meter: 0
 
More
Hey i made two apps with remote mysql database . May be i can help .

Sent from my SM-G900T using Tapatalk
25th April 2014, 02:32 PM |#3  
amank22's Avatar
Junior Member
Flag greater noida
Thanks Meter: 6
 
More
Cool Change
Try changing this:

Code:
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Hashmap for ListView}
        fixtureList = new ArrayList<HashMap<Integer, FixtureItem>>();

        View fixtureView = inflater.inflate(R.layout.fragment_fixture, container, false);

       // Loading products in Background Thread
        new LoadFixture().execute();

       
        return fixtureView;
    }
from

Code:
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Hashmap for ListView}
        fixtureList = new ArrayList<HashMap<Integer, FixtureItem>>();
 
        // Loading products in Background Thread
        new LoadFixture().execute();

       
        View fixtureView = inflater.inflate(R.layout.fragment_fixture, container, false);

      
        return fixtureView;
    }
Last edited by amank22; 25th April 2014 at 02:37 PM. Reason: typing error
19th September 2014, 07:25 PM |#4  
Junior Member
Thanks Meter: 0
 
More
Unhappy Same problem
I have the same problem, not able to solve?
19th September 2014, 09:59 PM |#5  
Junior Member
Thanks Meter: 0
 
More
Smile
Resolved!!

Missed make reference to listView after inflate places:

Quote:

lv = (ListView) view.findViewById(R.id.fixture_list);

Hugs,
Léo
1st October 2015, 07:27 AM |#6  
Junior Member
Flag jalandhar
Thanks Meter: 6
 
More
can anybody help me im trying to display the results in the fragment from customlistview adapter ,there is no error at all but its not also displaying the results..
Post Reply Subscribe to Thread

Tags
asyncronous, custom adapter, drawer, fragment, listview

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes