rs_pt Posted February 1, 2016 at 03:45 AM Report Share #592854 Posted February 1, 2016 at 03:45 AM Boas, estou a desenvolver uma app android no Android Studio e necessito de fazer ligação a uma BD existente de maneira a executar queries de select, delete, insert e update. Acontece que tenho visto varios tutoriais tanto com mysql (utilizando XAMPP) ou SQLite e nenhum funcionou. Sei tambem qe tenho de usar HttpUrlConnection, mas mesmo assim , nada que tenha funcionado. Alguem me pode ajudar? Ou pelo menos sugerir um tutorial devidamente atualizado e funcional? Obrigado desde ja. Link to comment Share on other sites More sharing options...
Guest Posted February 1, 2016 at 03:01 PM Report Share #592879 Posted February 1, 2016 at 03:01 PM Boas, Já viste estes tutoriais? Aqui e aqui. Consulta também a resposta na fonte de onde adquiri estes tutoriais. Cumps. Link to comment Share on other sites More sharing options...
rs_pt Posted February 1, 2016 at 09:41 PM Author Report Share #592909 Posted February 1, 2016 at 09:41 PM Obrigado pela resposta, mas acabei por conseguir utilizando este tutorial: http://easyway2in.blogspot.pt/2015/07/android-mysql-database-connect.html Agora tenho de passar dados de uma tabela para um ListView e implementar alguns queries para gestao da bd, em principio nao terei mais problemas, vamos ver. Obrigado mais uma vez, cumps 👍 Link to comment Share on other sites More sharing options...
rs_pt Posted February 2, 2016 at 05:27 AM Author Report Share #592914 Posted February 2, 2016 at 05:27 AM Estou agora a ter um problema a passar o conteudo de um JSONObject para um list view. Acontece que so me passa o 1o de 4 posiçoes desse array. o codigo a ser usado: class selecionar_cinema import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class SelecionarCinema extends AppCompatActivity { Context contexto_atual = this; String json_string; JSONObject jsonObject; JSONArray jsonArray; CinemasAdapter cinemasAdapter; ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_selecionar_cinema); Intent intentecracompra = getIntent(); new BackgroundListaCinema() .execute(); } class BackgroundListaCinema extends AsyncTask<Void,Void,String> { String json_url; String JSON_STRING; @Override protected String doInBackground(Void... params) { try { URL url = new URL(json_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); while ((JSON_STRING = bufferedReader.readLine()) != null) { stringBuilder.append(JSON_STRING + "\n"); } bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return stringBuilder.toString().trim(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { json_url= " http://192.168.0.104:1234/htdocs/json_get_data.php"; } @Override protected void onprogressUpdate(Void... values) { super.onprogressUpdate(values); } @Override protected void onPostExecute(String result) { TextView textView = (TextView) findViewById(R.id.teste); textView.setText(result); cinemasAdapter = new CinemasAdapter(contexto_atual,R.layout.row_layout); listview = (ListView) findViewById(R.id.listView); //json_string = result; try { jsonObject = new JSONObject(result); jsonArray = jsonObject.getJSONArray("result"); int count = 0; int id; String nome; while(count<jsonObject.length()) { JSONObject JO = jsonArray.getJSONObject(count); id = JO.getInt("id"); nome=JO.getString("nome"); Cinemas cinemas = new Cinemas(id,nome); cinemasAdapter.add(cinemas); listview.setAdapter(cinemasAdapter); count++; } } catch (JSONException e) { e.printStackTrace(); } } } } Class CinemasAdapter public class CinemasAdapter extends ArrayAdapter { List list = new ArrayList(); public CinemasAdapter(Context context, int resource) { super(context, resource); } public void add(Cinemas object) { super.add(object); list.add(object); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row; row=convertView; CinemasHolder cinemasHolder; if(row==null) { LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = layoutInflater.inflate(R.layout.row_layout,parent,false); cinemasHolder = new CinemasHolder(); cinemasHolder.tx_cinema= (TextView) row.findViewById(R.id.tx_cinema); row.setTag(cinemasHolder); } else { cinemasHolder = (CinemasHolder)row.getTag(); } Cinemas cinemas = (Cinemas)this.getItem(position); cinemasHolder.tx_cinema.setText(cinemas.getCinema()); return row; } static class CinemasHolder { TextView tx_cinema; } } Class Cinemas public class Cinemas { private int id; private String cinema; public Cinemas(int id, String cinema ) { this.setId(id); this.setCinema(cinema); } public String getCinema() { return cinema; } public void setCinema(String cinema) { this.cinema = cinema; } public int getId() { return id; } public void setId(int id) { this.id = id; } } ficheiros xml: Selecionar cinemas.xml <?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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="pt.ulp.se.moviefeed.SelecionarCinema"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Selecione um Cinema" android:id="@+id/textView4" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/teste" android:layout_below="@+id/textView4" android:layout_alignParentStart="true" /> <ListView android:layout_width="fill_parent" android:layout_height="match_parent" android:id="@+id/listView" android:layout_below="@+id/teste" android:layout_alignParentStart="true" android:layout_marginTop="73dp" /> </RelativeLayout> row_layout.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="75dp"> <TextView android:layout_height="match_parent" android:layout_width="150dp" android:id="@+id/tx_cinema" android:layout_alignParentLeft="true" android:text="Cinemas" android:gravity="center" android:textAppearance="?android:textAppearanceLarge" /> </RelativeLayout> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now