0

我已成功使用表格视图将 mysql 中的数据显示到我的 android 应用程序中,但我想将该信息显示到列表视图中,以便我可以使其可点击,然后它可以进入新活动。问题是我不知道该怎么做,我在这里发布 java 和 xml 代码

postoffer.java

public class PostedOffer extends Activity{String data = "";
TableLayout tl;
TableRow tr;


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

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

    final GetDataFromDB getdb = new GetDataFromDB();
    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.setCity(json_data.getString("city"));
            user.setSource(json_data.getString("source"));
            user.setDestination(json_data.getString("destination"));
            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 TextView to add to the row **/
    //City
    TextView city = new TextView(this);
    city.setText("City");
    city.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    city.setPadding(5, 5, 5, 5);
    city.setBackgroundColor(Color.RED);
    LinearLayout Ll = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(5, 5, 5, 5);
    //Ll.setPadding(10, 5, 5, 5);
    Ll.addView(city,params);
    tr.addView((View)Ll); // Adding textView to tablerow.

    // Source
    TextView place = new TextView(this);
    place.setText("Source");
    place.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    place.setPadding(5, 5, 5, 5);
    place.setBackgroundColor(Color.RED);
    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 5, 5, 5);
    //Ll.setPadding(10, 5, 5, 5);
    Ll.addView(place,params);
    tr.addView((View)Ll); // Adding textview to tablerow.

    // Destination
    TextView destination = new TextView(this);
    destination.setText("Destination");
    destination.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    destination.setPadding(5, 5, 5, 5);
    destination.setBackgroundColor(Color.RED);
    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 5, 5, 5);
    //Ll.setPadding(10, 5, 5, 5);
    Ll.addView(destination,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);

        // City
        TextView city = new TextView(this);
        city.setText(p.getCity());
        city.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        city.setPadding(5, 5, 5, 5);
        city.setBackgroundColor(Color.GRAY);
        LinearLayout Ll = new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 2, 2, 2);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(city,params);
        tr.addView((View)Ll); // Adding textView to tablerow.

        // Source
        TextView source = new TextView(this);
        source.setText(p.getSource());
        source.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        source.setPadding(5, 5, 5, 5);
        source.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 2, 2, 2);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(source,params);
        tr.addView((View)Ll); // Adding textview to tablerow.

        // Destination
        TextView destination = new TextView(this);
        destination.setText(p.getDestination());
        destination.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        destination.setPadding(5, 5, 5, 5);
        destination.setBackgroundColor(Color.GRAY);
        Ll = new LinearLayout(this);
        params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 2, 2, 2);
        //Ll.setPadding(10, 5, 5, 5);
        Ll.addView(destination,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));
    }
}}class GetDataFromDB {

public String getDataFromDB() {
    try {
        String email=LoginActivity.get();
        HttpGet httppost;
        HttpClient httpclient;
        httpclient = new DefaultHttpClient();
        httppost = new HttpGet("http://10.0.2.2/ersindia/GetUsers.php?param="+email); // 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";
    }
}}class Users {

String city;
String source;
String destination;

public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}
public String getSource() {
    return source;
}
public void setSource(String source) {
    this.source = source;
}
public String getDestination() {
    return destination;
}
public void setDestination(String destination) {
    this.destination = destination;
}}

postoffer.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#3b3b3b"
android:padding="10dip" >    
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Your Offers"
    android:textSize="30dip"
    android:gravity="center" />

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none"
android:layout_below="@+id/textView1">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:stretchColumns="1,0,0"
        android:id="@+id/maintable" >
    </TableLayout>
</ScrollView>

4

1 回答 1

1

只需取一个Listview并将其分配给列表视图。

    for (int i = 0; i < jArray.length(); i++) {  
     //  just get all the values in the array

    }
 adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1,yourArray);
 listview.setAdapter(adapter); 
于 2013-03-05T10:17:29.843 回答