0

好的,有一段时间我一直在从数据库中获取“名称”和“位置”并将它们列在一个表中。一个接一个地使用循环,直到全部显示,总共有 22 个。

例如。

name 1 | location 1
name 2 | location 2 
name 3 | location 3

我想要的是不加载背景中的信息,因为我已经是,但只在需要时选择数据,所以我可以有图片 txt 然后是数据。

例如。

{Picture}                          {Picture}
{txt}{name 1}                      {txt}{name 2}

而不是将所有内容一个接一个地列出以使页面看起来更智能。顺便说一句,这是我的一级方程式应用程序的结果页面。

GetDataFromDB2.java ------

package com.test.test;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class GetDataFromDB2 {

public String getDataFromDB() {
    try {

        HttpPost httppost;
        HttpClient httpclient;
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost(
                "http://192.168.1.69/GetResults.php"); // change this to your URL.....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost,
                responseHandler);

        return response.trim();

    } catch (Exception e) {
        System.out.println("ERROR : " + e.getMessage());
        return "error";
    }
}
}

这是显示的Results.java

package com.test.test;



import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.   graphics.   Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class Results extends Activity {

String data = "";
TableLayout tl;
TableRow tr;
TextView label;

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

    tl = (TableLayout) findViewById(R.id.maintable);

    final GetDataFromDB2 getdb = new GetDataFromDB2();
    new Thread(new Runnable() {
        public void run() {
            data = getdb.getDataFromDB();
            System.out.println(data);

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    ArrayList<Users> users = parseJSON(data);
                    addData(users);                     
                }
            });

        }
    }).start();
}

public ArrayList<Users> parseJSON(String result) {
    ArrayList<Users> users = new ArrayList<Users>();
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            Users user = new Users();
            user.setId(json_data.getInt("idresults"));
            user.setName(json_data.getString("driver"));
            user.setPlace(json_data.getString("local"));
            users.add(user);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());  
    }
    return users;
}


void addHeader(){
    /** Create a TableRow dynamically **/
    tr = new TableRow(this);

    /** Creating a TextView1 to add to the row **/
    label = new TextView(this);
    label.setText("DRIVERS");
    label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    label.setPadding(20, 5, 5, 5);
    label.setBackgroundColor(Color.argb(255, 25, 25, 25));
    label.setTextColor(Color.argb(-19, 246, 255, 0));
    LinearLayout Ll = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 0, 0);
    //Ll.setPadding(10, 5, 5, 5);
    Ll.addView(label,params);
    tr.addView((View)Ll); // Adding textView to tablerow.

    /** Creating textview2 Button **/
    TextView place = new TextView(this);
    place.setText("GRAND PRIX");
    place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    place.setPadding(20, 5, 5, 5);
    place.setBackgroundColor(Color.argb(255, 25, 25, 25));
    place.setTextColor(Color.argb(-19, 246, 255, 0));
    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 0, 0, 0);
    //Ll.setPadding(10, 5, 5, 5);
    Ll.addView(place,params);
    tr.addView((View)Ll); // Adding textview to tablerow.

     // Add the TableRow to the TableLayout
    tl.addView(tr, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
}

@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {

    addHeader();

    for (Iterator i = users.iterator(); i.hasNext();) {

        Users p = (Users) i.next();

        /** Create a TableRow dynamically **/
        tr = new TableRow(this);

        /** Creating a TextView to add to the row **/
        label = new TextView(this);
        label.setText(p.getName());
        label.setId(p.getId());
        label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        label.setPadding(20, 5, 5, 5);
        label.setBackgroundColor(Color.argb(255, 25, 25, 25));
        label.setTextColor(Color.argb(255, 0, 198, 255));
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 1, 1, 0);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(label,params);
        tr.addView((View)Ll); // Adding textView to tablerow.

        /** Creating Qty Button **/
        TextView place = new TextView(this);
        place.setText(p.getPlace());
        place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        place.setPadding(20, 5, 5, 5);
        place.setBackgroundColor(Color.argb(255, 25, 25, 25));
        place.setTextColor(Color.argb(255, 0, 198, 255));
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 1, 1, 0);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(place,params);
        tr.addView((View)Ll); // Adding textview to tablerow.

         // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }
}
}

如果你能弄明白,你会看到它创建了自己的表格并进行了布局。我想要的只是在页面中的某些时间使用数据。ps 每个数据库条目作为它们的“id”例如

ID - 1. name 1. location 1.   id - 2. name 2. locations 2  the id is just 1 - 22 would it be easyer using the id ?
4

0 回答 0