我尝试在我的Android应用程序中从我的mysql数据库(phpmyadmin)获取信息。 我使用php文件来连接数据库,以及一些你不需要查看的其他文件。
我的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);
}
}
使用void ParseJSON,我在讲台上的OnCreate中使用此代码显示一个名为podium的新片段的信息:
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 :(得分: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();
答案 1 :(得分:0)
我想你可以用: contactAdapter = new ContactAdapter(getActivity(),R.layout.row_layout);
在你的onCreate in podiumFragment。