Android中动态填充表格内容的问题
我的Android版本是:4.2Android中动态填充表格内容的问题,android,android-tablelayout,Android,Android Tablelayout,我的Android版本是:4.2 private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{ protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { InputStream in = enti
private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
protected ArrayList<String> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("my URL");
String text = null;
String result = null;
ArrayList<String> title= new ArrayList<String>();
String url="";
String desc="";
try {
//HttpResponse response = httpClient.execute(httpGet, localContext);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
JSONArray ja = new JSONArray(text) ;
int n = ja.length();
for (int i = 0; i < n; i++) {
JSONObject jo = ja.getJSONObject(i);
title.add(jo.getString("Title"));
url= jo.getString("URL");
desc= jo.getString("Description");
}
} catch (Exception e) {
e.getLocalizedMessage();
}
return title;
}
protected void onPostExecute(ArrayList<String> results) {
if (results!=null) {
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<results.size();i++){
TableRow TR= new TableRow(MainActivity.this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
TextView tv1 =(TextView)findViewById(R.id.textView1);
TR.addView(tv1);
Object objPlace[] = new Object[results.size()] ;
objPlace = results.toArray();
tv1.setText("");
for (int j = 0; j < results.size(); j++){
tv1.append("DATA:\n" + results.get(j) + "\n");
}
tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
}
}
我从json动态填充表布局。我尝试了以下代码。但它没有显示任何内容。我的Xml包含滚动视图、表格布局和文本视图
private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
protected ArrayList<String> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("my URL");
String text = null;
String result = null;
ArrayList<String> title= new ArrayList<String>();
String url="";
String desc="";
try {
//HttpResponse response = httpClient.execute(httpGet, localContext);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
JSONArray ja = new JSONArray(text) ;
int n = ja.length();
for (int i = 0; i < n; i++) {
JSONObject jo = ja.getJSONObject(i);
title.add(jo.getString("Title"));
url= jo.getString("URL");
desc= jo.getString("Description");
}
} catch (Exception e) {
e.getLocalizedMessage();
}
return title;
}
protected void onPostExecute(ArrayList<String> results) {
if (results!=null) {
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<results.size();i++){
TableRow TR= new TableRow(MainActivity.this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
TextView tv1 =(TextView)findViewById(R.id.textView1);
TR.addView(tv1);
Object objPlace[] = new Object[results.size()] ;
objPlace = results.toArray();
tv1.setText("");
for (int j = 0; j < results.size(); j++){
tv1.append("DATA:\n" + results.get(j) + "\n");
}
tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
}
}
我的.java代码是:
private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
protected ArrayList<String> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("my URL");
String text = null;
String result = null;
ArrayList<String> title= new ArrayList<String>();
String url="";
String desc="";
try {
//HttpResponse response = httpClient.execute(httpGet, localContext);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
JSONArray ja = new JSONArray(text) ;
int n = ja.length();
for (int i = 0; i < n; i++) {
JSONObject jo = ja.getJSONObject(i);
title.add(jo.getString("Title"));
url= jo.getString("URL");
desc= jo.getString("Description");
}
} catch (Exception e) {
e.getLocalizedMessage();
}
return title;
}
protected void onPostExecute(ArrayList<String> results) {
if (results!=null) {
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<results.size();i++){
TableRow TR= new TableRow(MainActivity.this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
TextView tv1 =(TextView)findViewById(R.id.textView1);
TR.addView(tv1);
Object objPlace[] = new Object[results.size()] ;
objPlace = results.toArray();
tv1.setText("");
for (int j = 0; j < results.size(); j++){
tv1.append("DATA:\n" + results.get(j) + "\n");
}
tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
}
}
私有类LongRunningGetIO扩展异步任务{
受保护字符串GetAscioContentRomentity(HttpEntity实体)引发IllegalStateException、IOException{
InputStream in=entity.getContent();
StringBuffer out=新的StringBuffer();
int n=1;
而(n>0){
字节[]b=新字节[4096];
n=英寸读数(b);
如果(n>0)out.append(新字符串(b,0,n));
}
return out.toString();
}
受保护的ArrayList doInBackground(无效…参数){
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpGet-HttpGet=newhttpget(“我的URL”);
字符串文本=空;
字符串结果=null;
ArrayList title=新的ArrayList();
字符串url=“”;
字符串desc=“”;
试一试{
//HttpResponse response=httpClient.execute(httpGet,localContext);
HttpResponse response=httpClient.execute(httpGet,localContext);
HttpEntity=response.getEntity();
text=getAsciContentRomentity(实体);
JSONArray ja=新JSONArray(文本);
int n=ja.length();
对于(int i=0;i 对于(int i=0;i>>>>>>>>>>>>>>>>AndroidRuntime START>>AndroidRuntime START您是否评估了internet上的json?在我看来,这似乎不是表视图的问题。例外
private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
protected ArrayList<String> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("my URL");
String text = null;
String result = null;
ArrayList<String> title= new ArrayList<String>();
String url="";
String desc="";
try {
//HttpResponse response = httpClient.execute(httpGet, localContext);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
JSONArray ja = new JSONArray(text) ;
int n = ja.length();
for (int i = 0; i < n; i++) {
JSONObject jo = ja.getJSONObject(i);
title.add(jo.getString("Title"));
url= jo.getString("URL");
desc= jo.getString("Description");
}
} catch (Exception e) {
e.getLocalizedMessage();
}
return title;
}
protected void onPostExecute(ArrayList<String> results) {
if (results!=null) {
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<results.size();i++){
TableRow TR= new TableRow(MainActivity.this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
TextView tv1 =(TextView)findViewById(R.id.textView1);
TR.addView(tv1);
Object objPlace[] = new Object[results.size()] ;
objPlace = results.toArray();
tv1.setText("");
for (int j = 0; j < results.size(); j++){
tv1.append("DATA:\n" + results.get(j) + "\n");
}
tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
}
}
request time failed: java.net.SocketException: Address family not supported by protocol
表示您的请求失败。请尝试在onPostExecute中设置断点以检查结果是否正常。我不确定,但请检查此链接。我忘记在android manifest.xml文件中添加internet权限。添加该行后,它将显示“应用程序意外停止”现在logcat是:01-17 19:11:12.705:I/Process(313):发送信号。PID:313 SIG:9 01-17 19:11:12.734:E/JavaBinder(59):!!!失败的BINDER事务!!!01-17 19:11:12.734:E/JavaBinder(59):!!!失败的BINDER事务!!!01-17 19:11:12.753:I/WindowManager(59):赢得死亡:窗口{44117118 com.example.tabledemo/com.example.tabledemo.main活动暂停=false}01-17 19:11:12.763:I/ActivityManager(59):进程com.example.tabledemo(pid 313)已死亡。01-17 19:06:25.213:E/AndroidRuntime(305):在dalvik.system.NativeStart.main(本机方法)抱歉,我确实没有“绑定器事务失败”错误的经验。有些人在本链接中讨论了与字节数组相关的问题
private class LongRunningGetIO extends AsyncTask <Void, Void, ArrayList<String>>{
protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n>0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n>0) out.append(new String(b, 0, n));
}
return out.toString();
}
protected ArrayList<String> doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("my URL");
String text = null;
String result = null;
ArrayList<String> title= new ArrayList<String>();
String url="";
String desc="";
try {
//HttpResponse response = httpClient.execute(httpGet, localContext);
HttpResponse response = httpClient.execute(httpGet, localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
JSONArray ja = new JSONArray(text) ;
int n = ja.length();
for (int i = 0; i < n; i++) {
JSONObject jo = ja.getJSONObject(i);
title.add(jo.getString("Title"));
url= jo.getString("URL");
desc= jo.getString("Description");
}
} catch (Exception e) {
e.getLocalizedMessage();
}
return title;
}
protected void onPostExecute(ArrayList<String> results) {
if (results!=null) {
Table=(TableLayout)findViewById(R.id.Table);
TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0f);
//fill the table with Table rows dynamicly
for(int i=0;i<results.size();i++){
TableRow TR= new TableRow(MainActivity.this);
TR.setLayoutParams(tableRowParams);
Table.addView(TR);
TextView tv1 =(TextView)findViewById(R.id.textView1);
TR.addView(tv1);
Object objPlace[] = new Object[results.size()] ;
objPlace = results.toArray();
tv1.setText("");
for (int j = 0; j < results.size(); j++){
tv1.append("DATA:\n" + results.get(j) + "\n");
}
tv1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,1.0f));
}
}
}
}