我在xml中创建了一个表来显示一些信息。但是文本没有加载到表格中。我知道文本有一个值,因为它显示在我的日志中。
我的xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<ImageView android:id="@+id/breweryImage"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="10dip"/>
</LinearLayout >
</FrameLayout>
<TableLayout
android:id="@+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:shrinkColumns="*"
android:stretchColumns="*">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/tableStatTitles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/year"
android:text="Year Est."
android:gravity="center"
android:textStyle = "bold"
android:textSize="10sp"
android:layout_weight="1"
></TextView>
<TextView
android:id="@+id/bAvg"
android:text="Avg"
android:gravity="center"
android:textStyle = "bold"
android:textSize="10sp"
android:layout_weight="1"
></TextView>
</TableRow>
<TableRow
android:id="@+id/tableStat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="@+id/yearEnter"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>
<TextView
android:id="@+id/avgEnter"
android:text=""
android:gravity="center"
android:textSize="15sp"
android:layout_width="wrap_content"
></TextView>
</TableRow>
</LinearLayout>
</LinearLayout >
</FrameLayout>
</TableLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/beerDescriptionTitle2"
android:textStyle = "bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:text="Description:"
android:padding="5dip"
></TextView>
<TextView
android:id="@+id/breweryDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:padding="5dip"
></TextView>
</LinearLayout >
</FrameLayout>
</LinearLayout>
</ScrollView>
我正在加载数据的异步任务如下所示:
public class GetBreweryDataJSON extends AsyncTask<String, Void, String> {
Context c;
private ProgressDialog Dialog;
public GetBreweryDataJSON(Context context)
{
c = context;
Dialog = new ProgressDialog(c);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Getting brewery information");
Dialog.setTitle("Loading");
Dialog.setCancelable(false);
Dialog.show();
}
protected void onPostExecute(String result){
//decode json here
try{
Log.d("brewery", "in try");
JSONObject o = new JSONObject(result);
Log.d("brewery", result);
//String name = getName(o);
//String description = getDescription(o);
//String year = getYear(o);
//String icon = getIcon(o);
String name = o.getString("name");
String description = o.getString("description");
String year = o.getString("year");
String icon = o.getString("picture");
String avg = o .getString("avg");
//set image
ImageView im1 = (ImageView) ((Activity) c).findViewById(R.id.breweryImage);
ImageDownloadTask imageD = new ImageDownloadTask(im1);
imageD.execute(icon);
//create text views
TextView breweryYear = (TextView) ((Activity) c).findViewById(R.id.yearEnter);
TextView breweryAvg = (TextView) ((Activity) c).findViewById(R.id.avgEnter);
TextView breweryDescription = (TextView) ((Activity) c).findViewById(R.id.breweryDescription);
String yearPrint = "";
year = yearPrint + year;
//set text
breweryYear.setText(year);
Log.d("brewery" , year);
Log.d("brewery" , avg);
breweryDescription.setText(description);
breweryAvg.setText(avg);
}
catch(Exception e){
}
Dialog.dismiss();
}
public String getName(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("name");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getIcon(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getJSONObject("images").getString("large");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getDescription(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("description");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String getYear(JSONObject json){
String holder;
try{
holder = json.getJSONObject("data").getString("established");
} catch (JSONException e) {
holder = "N/A";
}
return holder;
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
以下是我所获得的截图:
答案 0 :(得分:1)
看起来我忘记在XML中打开和关闭表标签。我有行,但从不启动表。