2013-03-05 42 views
0

我已經成功地使用表視圖顯示數據從mysql到我的android應用程序,但我想將這些信息顯示到listview中,以便我可以使它可點擊然後它可以轉到新的活動。問題是我不知道該怎麼辦,我在這裏發佈的Java和XML代碼android中的tableview和listview

postedoffer.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; 
}} 

postedoffer.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> 

+0

檢查我的答案 http://stackoverflow.com/questions/15065549/show-next-row-in-listview-when-previous-rows-checkbox-is-checked/15065703#15065703 – 2013-03-05 10:17:50

回答

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);