私のAndroidデータベースにあるmy mysqlデータベース(phpmyadmin)から情報を取得しようとしています。 私はphpファイルを使用してdbなどを接続し、必要のない他のファイルを表示します。フラグメントを使用してmysqlデータベース情報を取得する
私は私のmainActivityに次のコードを持っている:
String JSON_STRING;
JSONObject jsonObject;
JSONArray jsonArray;
ContactAdapter contactAdapter;
ListView listView;
listView = (ListView) getView().findViewById(R.id.listview);
contactAdapter = new ContactAdapter(this.getContext(),R.layout.row_layout);
listView.setAdapter(contactAdapter);
JSON_STRING=getActivity().getIntent().getExtras().getString("JSON_DATA");
try {
jsonObject = new JSONObject(JSON_STRING);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String id_post,id_user_post, place_post, date_post;
while(count < jsonArray.length()){
JSONObject JO = jsonArray.getJSONObject(count);
id_user_post = JO.getString("id_user_post");
place_post = JO.getString("place_post");
date_post = JO.getString("date_post");
Contacts contacts = new Contacts(id_user_post, place_post, date_post);
contactAdapter.add(contacts);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
:ボイドParseJSONで
String JSON_STRING;
JSONObject jsonObject;
JSONArray jsonArray;
ContactAdapter contactAdapter;
ListView listView;
public void getJSON(View View)
{
new BackgroundTask().execute();
}
class BackgroundTask extends AsyncTask<Void, Void, String>
{
String json_url;
@Override
protected void onPreExecute() {
json_url = "http://firejackal.fr/script.php";
}
@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 onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
public void parseJSON(View view) {
if (JSON_STRING == null) {
Toast.makeText(getApplicationContext(), "First get JSON", Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(this, Podium.class);
intent.putExtra("JSON_DATA", JSON_STRING);
startActivity(intent);
}
}
を、私は表彰台にOnCreate関数の内部で、このコードを使用して表彰台と呼ばれる新しいフラグメントに関する情報を表示します
しかし、このコードは、表彰台がアクティビティでありフラグメントではない場合に機能します。しかし表彰台は断片であり、このコードをonCreateViewに入れても機能しません。
私はあなたにcontacts.javaを与える:
public class Contacts {
private String id_user_post, place_post, date_post;
public Contacts(String id_user_post, String place_post, String date_post){
this.setId_user_post(id_user_post);
this.setPlace_post(place_post);
this.setDate_post(date_post);
}
public String getId_user_post() {
return id_user_post;
}
public void setId_user_post(String id_user_post) {
this.id_user_post = id_user_post;
}
public String getPlace_post() {
return place_post;
}
public void setPlace_post(String place_post) {
this.place_post = place_post;
}
public String getDate_post() {
return date_post;
}
public void setDate_post(String date_post) {
this.date_post = date_post;
}
}
そしてcontactAdapter.java:
public class ContactAdapter extends ArrayAdapter {
List list = new ArrayList();
public ContactAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Contacts 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;
ContactHolder contactHolder;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.tx_id_user_post = (TextView)row.findViewById(R.id.tx_id_user_post);
contactHolder.tx_place_post = (TextView)row.findViewById(R.id.tx_place_post);
contactHolder.tx_date_post = (TextView)row.findViewById(R.id.tx_date_post);
row.setTag(contactHolder);
}else {
contactHolder = (ContactHolder)row.getTag();
}
Contacts contacts = (Contacts)this.getItem(position);
contactHolder.tx_id_user_post.setText(contacts.getId_user_post());
contactHolder.tx_place_post.setText(contacts.getPlace_post());
contactHolder.tx_date_post.setText(contacts.getDate_post());
return row;
}
static class ContactHolder{
TextView tx_id_user_post, tx_place_post, tx_date_post;
}
}
だから私はフラグメントで動作するようにこの部分を適応させることができますか?
他のファイルを参照する必要がある場合は、私に教えてください。
この夕方に感謝します。 – Jey10
あなたのコードに置き換える必要があるコードをより正確に説明できますか?あなたはOnCreateについて話しますが、フラグメントでOnCreateViewを使用する必要がありますか? – Jey10
フラグメントの仕組みを確認してください:http://developer.android.com/intl/es/guide/components/fragments.html –