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:
ed il codice java. Costanti.javaCodice PHP:<?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);
?>
CustomNewsAdapter.java:codice:public class Costanti { private static String UrlLink = "http://www.myapp.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; } }
News.javacodice: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; } } }
NewsActivity.java:codice: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; } }
NewsSelectedActivity.javacodice: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)); } }
E questo diciamo è l'output che ho quando vado a testare la mia applicazione sul mio cellulare:codice: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; } }
Screenshot_2015-09-20-16-54-15.jpgScreenshot_2015-09-20-16-54-21.jpgScreenshot_2015-09-20-16-54-28.jpg

LinkBack URL
About LinkBacks

Rispondi quotando