2016-03-22 81 views
0

我嘗試從我的mysql數據庫(phpmyadmin)中獲取我的android應用程序中的信息。 我使用php文件連接數據庫,例如,和其他一些你不需要看到的文件。使用片段獲取mysql數據庫信息Android

我在mainActivity下面的代碼:

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); 

     } 

    } 

隨着虛空ParseJSON,我顯示在講臺上使用內部的OnCreate此代碼調用講臺上了一個新片段的信息:

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(); 
    } 

但是,如果指揮台是活動而不是片段,則此代碼有效。然而,講臺是一個片段,並將這段代碼放在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; 

    } 
} 

所以你能幫助我適應這個部分的片段工作?

如果您需要查看其他文件,請告訴我。

回答

0

試試這種方法:

添加您的片段裏面這個代碼

public static PodiumFragment newInstance(String mJsonData) { 

    PodiumFragment mFragment = new PodiumFragment(); 

    Bundle args = new Bundle(); 
    args.putString("JSON_DATA", mJsonData); 
    mFragment.setArguments(args); 

    return mFragment; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    if (getArguments() != null) { 
     if(getArguments().containsKey("JSON_DATA")){ 
      JSON_STRING = getArguments().getString("JSON_DATA"); 
     } 
    } 

    //YOUR CODE 

} 

在創建片段添加此行:

Fragment fragment = PodiumFragment.newInstance(JSON_STRING); 


FragmentManager mFragmentManager = getSupportFragmentManager(); 
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction(); 
mFragmentTransaction.replace(R.id.podium_frament, fragment); 
mFragmentTransaction.commit(); 
+0

我今天晚上會檢查一下,謝謝 – Jey10

+0

你能更準確地解釋一下我需要用你的代碼替換哪些代碼嗎?你談論OnCreate,但我們必須使用帶片段的OnCreateView? – Jey10

+0

你應該檢查一個片段的工作方式:http://developer.android.com/intl/es/guide/components/fragments.html –

0

我覺得你可以使用: contactAdapter = new ContactAdapter(getActivity(),R.layout.row_layout);

在您的onCreate平臺上片段。

+0

我會測試這個晚上謝謝 – Jey10

相關問題