将远程数据库中的按钮和按钮文本添加到listview中

时间:2015-05-21 00:45:26

标签: java android mysql listview

我有一个SQL表,其中以下架构远程在线存储。

ID  | Teams | Home | Draw | Away 
1      "A-B"   1.3   3.2    4.5

我的目的是在ListView中打印表的内容,每个记录都有以下格式的单个ListRow。

"Teams" Button(button text = content of Home) Button(button text = content of Draw) Button(button text = content of Away)
"A-B"   Button(1.3)    Button(3.2)   Button(4.5)

我创建了以下类,它打印出了团队但是我不知道如何为每一行添加三个按钮,并将表中的值作为按钮文本。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
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.os.AsyncTask;
import android.os.Bundle;
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;
import android.widget.TextView;


public class  AllGameslistActivity extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> gamesList;

    // url to get all games list
    private static String url_all_games = "*********";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_GAMELIST = "gamelist";
    private static final String TAG_GID = "gid";
    private static final String TAG_TEAMS = "Teams";
    private static final String TAG_HOME = "HomeWin";
    private static final String TAG_DRAW = "Draw";
    private static final String TAG_AWAY = "AwayWin";

    // games JSONArray
    JSONArray allgames = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_bets);

        // Hashmap for ListView
        gamesList = new ArrayList<HashMap<String, String>>();

        // Loading games in Background Thread
        new LoadAllGames().execute();

        // Get listview
        ListView lv = getListView();

        // on seleting single ga
        // launching Edit Product Screen
      //  lv.setOnItemClickListener(new OnItemClickListener() {


    /**
     * Background Async Task to Load all game by making HTTP Request
     * */
    class LoadAllGames extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AllGameslistActivity.this);
            pDialog.setMessage("Loading Games. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting All games from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_games, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Games: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Games
                    allgames = json.getJSONArray(TAG_GAMELIST);

                    // looping through All Products
                    for (int i = 0; i < allgames.length(); i++) {
                        JSONObject c = allgames.getJSONObject(i);

                        // Storing each json item in variable
                        String gid = c.getString(TAG_GID);
                        String Teams = c.getString(TAG_TEAMS);
                        String Home = c.getString(TAG_HOME);
                        String Draw = c.getString(TAG_DRAW);
                        String Away = c.getString(TAG_AWAY);
                        Double HomeDouble = Double.parseDouble(Home);
                        Double DrawDouble = Double.parseDouble(Draw);
                        Double AwayDouble = Double.parseDouble(Away);
                        Game x = new Game(Teams,HomeDouble,DrawDouble,AwayDouble);


                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_GID, gid );
                        map.put(TAG_TEAMS,Teams);



                        // adding HashList to ArrayList
                        gamesList.add(map);
                    }


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

            return "";
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            AllGameslistActivity.this, gamesList,
                            R.layout.list_item, new String[] { TAG_GID,
                            TAG_TEAMS},
                            new int[] { R.id.gid, R.id.Teams });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }


}

0 个答案:

没有答案