Autore Topic: Problema nello "scaricamento/caricamento" dei dati di un data base on line  (Letto 327 volte)

Offline Trinkwasser

  • Nuovo arrivato
  • *
  • Post: 32
  • Respect: 0
    • Mostra profilo
Ho un problema con il mio progetto, ho il mio database on line su altervista già riempito e salvato. Ma quando cerco di recuperare i dati e di andarli ad inserire nella listview carica solo il primo elemento della tabella e gli ultimi due. Inoltre quando cercherà di caricare l'altra riga della tabella anche qui non riesce a ricarla. La tabella è questa:
e questo è il suo contenuto:
utilizzo questo script php per caricarla:
Codice (PHP): [Seleziona]
<?php
        $host='localhost';
        $uname='root';   //Username per accedere nel database
        $pwd='';   //Password del database
        $db="my_notizieappandroid";   //Nome del database
   
                $select=$_REQUEST['id'];
        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
        mysql_select_db($db,$con) or die("db selection failed");
         
        $r=mysql_query($select,$con);

        while($row=mysql_fetch_array($r))
        {
                $flag[]=$row;
        }
         
        print(json_encode($flag));
        mysql_close($con);
?>
ed infine il codice java.
Costanti.java
Codice (Java): [Seleziona]
public class Costanti {

    private static String UrlLink = "http://www.notizieappandroid.altervista.org/";
    private static String PHPShow = "visualizza.php";
    private static String TABLE_NOTIZIE = "news";
    private static String KAY_ID = "ID";
    private static String KAY_TITLE = "title";
    private static String KAY_DESCRIPTION = "description";
    private static String KAY_BODY = "body";
    private static String KAY_SOURCE = "source";
    private static String KAY_DATE = "date";


    public static String getPHPShow() {
        return PHPShow;
    }

    public static String getKayDate() { return  KAY_DATE; }

    public static String getKaySource() {
        return KAY_SOURCE;
    }

    public static String getKayBody() {
        return KAY_BODY;
    }

    public static String getKayDescription() {
        return KAY_DESCRIPTION;
    }

    public static String getKayTitle() {
        return KAY_TITLE;
    }

    public static String getKayID() {
        return KAY_ID;
    }

    public static String getTableNotizie() {
        return TABLE_NOTIZIE;
    }

    public static String getUrlLink() {
        return UrlLink;
    }
}
CustomNewsAdapter.java:
Codice (Java): [Seleziona]
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by alessandro on 15/09/15.
 */

public class CustomNewsAdapter extends ArrayAdapter<News> {

    private List<News> Object;
    private static final int TYPE_RIGHT = 1;

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    public CustomNewsAdapter(Context C, List<News> Object){
        super(C, R.layout.news_row, Object);
        this.Object = Object;
    }

    @Override
    public View getView(int P, View convertV, ViewGroup parent){
        ViewHolder vHolder;
        News person = Object.get(P);
        if (convertV == null) {
            convertV = LayoutInflater.from(getContext()).inflate(R.layout.news_row,null);
            TextView title = (TextView) convertV.findViewById(R.id.title_news);
            TextView description = (TextView) convertV.findViewById(R.id.description_news);
            vHolder = new ViewHolder(title, description);
            convertV.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) convertV.getTag();
        }
        vHolder.getTitle().setText(person.getTitle());
        vHolder.getDescription().setText(person.getDescription());

        return convertV;
    }

    public class ViewHolder {
        TextView title;
        TextView description;
        public ViewHolder(TextView title, TextView description){
            this.title = title;
            this.description = description;
        }

        public TextView getDescription() {
            return description;
        }

        public TextView getTitle() {
            return title;
        }
    }
}
News.java
Codice (Java): [Seleziona]
public class News {

    private String title;
    private String description;
    private String source;
    private String body;
    private String date;
    private int ID;

    public News(int ID, String title, String description, String body, String source, String date) {
        this.ID = ID;
        this.title = title;
        this.description = description;
        this.body = body;
        this.source = source;
        this.date = date;
    }

    public String getDate() { return date; }

    public String getSource() {
        return source;
    }

    public String getBody() {
        return body;
    }

    public String getDescription() {
        return description;
    }

    public String getTitle() {
        return title;
    }
}
NewsActivity.java:
Codice (Java): [Seleziona]
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class NewsActivity extends AppCompatActivity {

    Intent intent;

    private ProgressBar progressBar;
    private ListView listView;
    private TextView textView;
    private List<News> newses;

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

        progressBar = (ProgressBar) findViewById(R.id.progress);
        listView = (ListView) findViewById(R.id.list_view_news);
        textView = (TextView) findViewById(R.id.text);

        newses = new ArrayList<News>();
        QuerySelect();

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {

                News item = (News) newses.get(position);

                intent = new Intent(getApplicationContext(), NewsSelectedActivity.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("description", item.getDescription());
                intent.putExtra("body", item.getBody());
                intent.putExtra("source", item.getSource());
                intent.putExtra("date", item.getSource());

                startActivity(intent);
            }
        });
    }

    private void QuerySelect() {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        String Select = "SELECT * FROM news";
        params.put("id", Select);
        client.post(Costanti.getUrlLink() + Costanti.getPHPShow(), params, new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                progressBar.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String result = "";
                try {
                    result = new String(responseBody, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                JSONArray jsonArray = null;
                if (!result.equals("null")) {
                    try {
                        jsonArray = new JSONArray(result);
                        parsingJsonArray(jsonArray);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(NewsActivity.this, "Connessione Fallita!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void parsingJsonArray(JSONArray jsonArray) {
        try {
            for (int i=0; i<jsonArray.length(); i++) {
                JSONObject json_data = jsonArray.getJSONObject(i);
                newses.add(new News(json_data.getInt(Costanti.getKayID()), json_data.getString(Costanti.getKayTitle()),
                        json_data.getString(Costanti.getKayDescription()), json_data.getString(Costanti.getKayBody()),
                        json_data.getString(Costanti.getKaySource()), json_data.getString(Costanti.getKayDate())));
            }

        }catch (JSONException e) {
            e.printStackTrace();
        }
        listView.setAdapter(new CustomNewsAdapter(NewsActivity.this, newses));
    }
}
NewsSelectedActivity.java
Codice (Java): [Seleziona]
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.TextView;

/**
 * Created by alessandro on 16/09/15.
 */


public class NewsSelectedActivity extends AppCompatActivity {

    TextView textViewTitle;
    TextView textViewDescription;
    TextView textViewBody;
    TextView textViewSource;
    TextView textViewDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_selected);
        textViewTitle = (TextView) findViewById(R.id.title_news_selected);
        textViewDescription = (TextView) findViewById(R.id.description_news_selected);
        textViewBody = (TextView) findViewById(R.id.body_news_selected);
        textViewSource = (TextView) findViewById(R.id.source_news_selected);
        textViewDate = (TextView) findViewById(R.id.date_news_selected);


        String headline = "";
        String details = "";
        String text = "";
        String source = "";
        String date = "";

        Intent intent = getIntent();
        if (null != intent) {
            headline = intent.getStringExtra("title");
            details = intent.getStringExtra("description");
            text = intent.getStringExtra("body");
            source = intent.getStringExtra("source");
            date = intent.getStringExtra("date");
        }

        textViewTitle.setText(headline);
        textViewDescription.setText(details);
        textViewBody.setText(text);
        textViewSource.setText(source);
        textViewSource.setText(date);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}
Ed il codice XML dei vari layout.
activity_news.xml
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <TextView
       android:layout_margin="10sp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/text"
       android:text="News:"/>
    <ListView
       android:layout_below="@+id/text"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/list_view_news"
       android:layout_marginTop="10sp"/>

    <ProgressBar
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/progress"
       android:visibility="invisible"
       android:layout_centerVertical="true"
       android:layout_centerHorizontal="true" />

</RelativeLayout>
activity_news_selected.xml
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".NewsSelectedActivity" >

    <TextView
       android:text="Title"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:id="@+id/title_news_selected"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true" />
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:textColor="#6b71f1"
       android:text="Description"
       android:id="@+id/description_news_selected"
       android:layout_below="@+id/title_news_selected"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true" />
    <TextView
       android:text="Body"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:textColor="#6b71f1"
       android:id="@+id/body_news_selected"
       android:layout_below="@+id/description_news_selected"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
    </TextView>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textAppearance="?android:attr/textAppearanceSmall"
       android:textColor="#6b71f1"
       android:text="Source"
       android:id="@+id/source_news_selected"
       android:layout_below="@+id/body_news_selected"
       android:layout_alignParentRight="true"
       android:layout_alignParentEnd="true" />
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textAppearance="?android:attr/textAppearanceSmall"
       android:textColor="#6b71f1"
       android:text="Date"
       android:id="@+id/date_news_selected"
       android:layout_below="@+id/source_news_selected"
       android:layout_alignLeft="@+id/source_news_selected"
       android:layout_alignStart="@+id/source_news_selected" />

</RelativeLayout>
news_row.xml
Codice (XML): [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10sp"
   android:padding="5dip">

    <TextView
       android:text="Title"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:id="@+id/title_news"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
    <TextView
       android:text="Description"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:textColor="#6b71f1"
       android:id="@+id/description_news"
       android:layout_below="@+id/title_news"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
    </TextView>

</RelativeLayout>
Questo è il logcat dopo che ho testato l'app.
Codice (Locomotive Basic): [Seleziona]
[code=text]09-19 21:20:23.565    3996-3996/? I/art﹕ Late-enabling -Xcheck:jni
09-19 21:20:24.530    3996-4020/com.example.alessandro.celiachia D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-19 21:20:24.550    3996-3996/com.example.alessandro.celiachia D/Atlas﹕ Validating map...
09-19 21:20:24.700    3996-4020/com.example.alessandro.celiachia D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
09-19 21:20:24.710    3996-4020/com.example.alessandro.celiachia D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
09-19 21:20:24.760    3996-4020/com.example.alessandro.celiachia D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
09-19 21:20:24.815    3996-4020/com.example.alessandro.celiachia E/﹕ Device driver API match
    Device driver API version: 29
    User space API version: 29
09-19 21:20:24.820    3996-4020/com.example.alessandro.celiachia E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Tue Jul 22 19:59:34 KST 2014
09-19 21:20:24.825    3996-4020/com.example.alessandro.celiachia I/OpenGLRenderer﹕ Initialized EGL, version 1.4
09-19 21:20:24.825    3996-4020/com.example.alessandro.celiachia D/OpenGLRenderer﹕ Enabling debug mode 0
09-19 21:20:25.115    3996-4006/com.example.alessandro.celiachia W/art﹕ Suspending all threads took: 10.752ms
09-19 21:20:25.235    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@20f43295 time:31455606
09-19 21:20:34.535    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_launch_request id:com.example.alessandro.celiachia time:31464906
09-19 21:20:34.955    3996-3996/com.example.alessandro.celiachia V/AsyncHttpRH﹕ Progress 491 from 1 (49100%)
09-19 21:20:35.110    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@17ed3bb0 time:31465480
09-19 21:20:36.435    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_launch_request id:com.example.alessandro.celiachia time:31466809
09-19 21:20:36.765    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2ed3866a time:31467138
09-19 21:20:36.825    3996-4020/com.example.alessandro.celiachia D/OpenGLRenderer﹕ endAllStagingAnimators on 0x48813bb8 (ListView) with handle 0x46671988
09-19 21:20:38.995    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@17ed3bb0 time:31469369
09-19 21:20:40.030    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_launch_request id:com.example.alessandro.celiachia time:31470401
09-19 21:20:40.450    3996-4020/com.example.alessandro.celiachia D/OpenGLRenderer﹕ endAllStagingAnimators on 0x48813bb8 (ListView) with handle 0x488e94c8
09-19 21:20:40.455    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@1745328 time:31470825
09-19 21:20:41.805    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@17ed3bb0 time:31472178
09-19 21:20:42.680    3996-3996/com.example.alessandro.celiachia I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@20f43295 time:31473054
[/code]

Non riesco proprio a capire questi errori, spero in un vostro aiuto come sempre! Grazie per la lunga attenzione.