如何将json数据检索到android xml

时间:2015-04-03 04:17:43

标签: android mysql xml json

我正在开发一个Android项目,并且放弃了下面的教程http://www.mybringback.com/android-sdk/12924/android-tutorial-using-remote-databases-php-and-mysql-part-1/

通过上述教程,我可以通过JSON将数据检索到列表活动,并且php代码工作正常,但问题在于我在哪里创建了一个表单我想要将数据检索到只有简单文本视图的简单XML,但我无法更改代码。 。 以下是从JSON检索数据以列出活动的代码。



package com.example.policadda;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Mycomplaints extends ListActivity {

	private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();

 
    private static final String READ_COMPLAINTS_URL = "http://divaexample.honor.es/mycomplaints.php";
    
  
    private static final String TAG_TITLE = "title";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_SUBJECT = "subject";
 
    


    private JSONArray mComments = null;

    private ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   
        setContentView(R.layout.my_complaints);   
    }
    
    @Override
    protected void onResume() {
 
    	super.onResume();

    	new LoadComments().execute();
    }

	public void addComment(View v)
    {
        Intent i = new Intent(Mycomplaints.this, Complaints.class);
        startActivity(i);
    }

   
	public void updateJSONdata() {

  
    	
        mCommentList = new ArrayList<HashMap<String, String>>();
        
 
        JSONParser jParser = new JSONParser();
        
        JSONObject json = jParser.getJSONFromUrl(READ_COMPLAINTS_URL);
        
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Mycomplaints.this);
            String post_username = sp.getString("username", "diva");
    
        try {
              List<NameValuePair> params = new ArrayList<NameValuePair>();
			   params.add(new BasicNameValuePair("username", post_username));
               Log.d("request!", "starting");

			     JSONObject json1 = jsonParser.makeHttpRequest(
			    		 READ_COMPLAINTS_URL, "POST", params);
      
            mComments = json1.getJSONArray(TAG_POSTS);

        
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);


                String title = c.getString(TAG_TITLE);
                String content = c.getString(TAG_SUBJECT);
                String username = c.getString(TAG_USERNAME);
                

          
                HashMap<String, String> map = new HashMap<String, String>();
              
                map.put(TAG_TITLE, title);
                map.put(TAG_SUBJECT, content);
                map.put(TAG_USERNAME, username);
             
           
                mCommentList.add(map);
                
                
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * Inserts the parsed data into our listview
     */
	private void updateList() {
	// hear data has been retrived to and list acitvity
	// But i need title message and username to be retrived to an text view but i am unable to do it
		
		ListAdapter adapter = new SimpleAdapter(this, mCommentList,
				R.layout.single_post, new String[] { TAG_TITLE, TAG_SUBJECT,
						TAG_USERNAME }, new int[] { R.id.title, R.id.message,
						R.id.username });

		setListAdapter(adapter);
		

		ListView lv = getListView();	
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

		

			}
		});
	}       

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

    	@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Mycomplaints.this);
			pDialog.setMessage("Loading complaints...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}
        @Override
        protected Boolean doInBackground(Void... arg0) {
        
        	  updateJSONdata();

            return null;

        }


        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();

            updateList();
        }
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案