0

I just follow this tutorial: HERE to retrieve products from DB, but when I run my app I get blank page, I try to fix the problem but with any result.

NOTE: I have create add product form and it work, the product inserted to DB but in the interface that I have apply the tutorial to display my products stay always blank.

this is my code:
AjouterProduit.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AjouterProduit extends AppCompatActivity implements Download_data.download_complete {

    public ListView list;
    public ArrayList<Countries> countries = new ArrayList<Countries>();
    public ListAdapter adapter;

    EditText gamme,produit,desc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ajouterproduit);

        list = (ListView) findViewById(R.id.list);
        adapter = new ListAdapter(this);
        list.setAdapter(adapter);

       Download_data download_data = new Download_data((Download_data.download_complete) this);
       download_data.download_data_from_link("http://192.168.148.1/Ajout.php");
       // download_data.download_data_from_link("http://www.kaleidosblog.com/tutorial/tutorial.json");


        gamme =(EditText)findViewById(R.id.editText);
        produit=(EditText)findViewById(R.id.editText2);
        desc=(EditText)findViewById(R.id.editText3);
    }


    public void Ajout(View view){
        String ga,pro,des;
        ga=gamme.getText().toString();
        pro=produit.getText().toString();
        des=desc.getText().toString();


        String type = "ajout";

        work3 backgroundWorker = new work3(this);

        backgroundWorker.execute(type,ga, pro ,des);


    }

    @Override
    public void get_data(String data) {

    }

    public class Countries {

        private String DescriptionProduit;
        //public String NomProduit;
        private String NomProduit;


        {
            String data = null;
            JSONArray data_array= null;
            try {
                data_array = new JSONArray(data);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

            for (int i = 0 ; i < data_array.length() ; i++)
            {
                JSONObject obj= null;
                try {
                    obj = new JSONObject(data_array.get(i).toString());
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

                Countries add=new Countries(getNomProduit(), getDescriptionProduit());
                try {
                    add.setNomProduit(obj.getString("NomProduit"));
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }
                try {
                    add.setDescriptionProduit(obj.getString("DescriptionProduit"));
                } catch (JSONException e1) {
                    e1.printStackTrace();
                }

                countries.add(add);

            }

            adapter.notifyDataSetChanged();

        }

        private Countries(String nomProduit, String descriptionProduit) {
            setNomProduit(nomProduit);
            setDescriptionProduit(descriptionProduit);
        }

        public String getNomProduit() {
            return NomProduit;
        }

        public void setNomProduit(String nomProduit) {
            NomProduit = nomProduit;
        }

        public String getDescriptionProduit() {
            return DescriptionProduit;
        }

        public void setDescriptionProduit(String descriptionProduit) {
            DescriptionProduit = descriptionProduit;
        }
    }
}

Download_data.java

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Download_data implements Runnable  {

    public download_complete caller;

    public interface download_complete
    {
        public void get_data(String data);
    }

    Download_data(download_complete caller) {
        this.caller = caller;
    }

    public String link;
    public void download_data_from_link(String link)
    {
        this.link = link;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        threadMsg(download(this.link));
    }

    private void threadMsg(String msg) {

        if (!msg.equals(null) && !msg.equals("")) {
            Message msgObj = handler.obtainMessage();
            Bundle b = new Bundle();
            b.putString("message", msg);
            msgObj.setData(b);
            handler.sendMessage(msgObj);
        }
    }


    private final Handler handler = new Handler() {

        public void handleMessage(Message msg) {

            String Response = msg.getData().getString("message");

            caller.get_data(Response);

        }
    };




    public static String download(String url) {
        URL website;
        StringBuilder response = null;
        try {
            website = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) website.openConnection();
            connection.setRequestProperty("charset", "utf-8");

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream()));

            response = new StringBuilder();
            String inputLine;

            while ((inputLine = in.readLine()) != null)
                response.append(inputLine);

            in.close();

        } catch (Exception  e) {
            return "";
        }


        return response.toString();
    }


}

ListAdapter.java

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;

public class ListAdapter extends BaseAdapter {

    AjouterProduit main;

    ListAdapter(AjouterProduit main)
    {
        this.main = main;
    }

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

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

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

    static class ViewHolderItem {
        TextView NomProduit;
        TextView DescriptionProduit;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolderItem holder = new ViewHolderItem();
        if (convertView == null) {
            LayoutInflater inflater;
            inflater = (LayoutInflater) main.getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.cell, null);

            holder.NomProduit = (TextView) convertView.findViewById(R.id.NomProduit);
            holder.DescriptionProduit = (TextView) convertView.findViewById(R.id.DescriptionProduit);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolderItem) convertView.getTag();
        }


        holder.NomProduit.setText(this.main.countries.get(position).getNomProduit());
        holder.DescriptionProduit.setText(this.main.countries.get(position).getDescriptionProduit());

        return convertView;
    }

}

cell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!--nom-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/NomProduit" />
<!--description-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/DescriptionProduit" />
</RelativeLayout>

list.xml

<RelativeLayout 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" >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"
        />
</RelativeLayout>

ajouterproduit.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Nom gamme"
        android:id="@+id/textView9"
        android:textStyle="bold" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Nom produit"
        android:id="@+id/textView10" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Description produit"
        android:id="@+id/textViewD" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText3" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ajouter produit"
        android:id="@+id/Ajout"
        android:layout_gravity="center_horizontal"
        android:onClick="Ajout"/>
</LinearLayout>

Ajout.php

<?php

$NomGamme=$_POST["NomGamme"];
$NomProduit=$_POST["NomProduit"];
$DescriptionProduit=$_POST["DescriptionProduit"];

//echo "Produit bien ajouter";
if($id=mysql_connect("localhost","root","") ) {

    if(mysql_select_db("applicationcolorado"))  



$query= "INSERT INTO `applicationcolorado`.`produitgamme` (`NomGamme`, `NomProduit`, `DescriptionProduit`)
 VALUES ('$NomGamme', '$NomProduit', '$DescriptionProduit')";



    if (mysql_query($query))

    {echo "Produit bien ajouter";}

else {
    echo "false";

    }

mysql_close($id);}

        ?>

Errors that I get Line 29

1 Answer 1

1

There is no ListView with the id list in your ajouterproduit.xml layout. In your AjouterProduit activity, you have these lines:

setContentView(R.layout.ajouterproduit);
list = (ListView) findViewById(R.id.list);

The first line set's this activity's layout to ajouterproduit.xml and the second one tries to find a ListView with the id list in this layout which doesn't exist so list is set to null.

Sign up to request clarification or add additional context in comments.

4 Comments

so what changes should I do ? change first line to this setContentView(R.layout.list);
@HANZOSALAH You should add a ListView element in the ajouterproduit.xml layout and set its ID to list. You can use the one form the list.xml layout.
but I want to display products in a separated layout (list.xml) in my case what should I do. Please help me I am beginner, It's the first time with android apps. NOTE I didn't understand what you mean with this ' _ You can use the one form the list.xml layout._ '
@HANZOSALAH If you want to display only the products, you need to create a new activity (a new java class) and open it when the product are loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.