Android 安卓,can';t为TextView指定一个值
我确实通过http读取数据,但我无法将此数据分配给TextView 当我在xml文件中创建TextView时,数据被分配给normal。但是当我以编程方式创建TextView时,文本是空的Android 安卓,can';t为TextView指定一个值,android,database,http,Android,Database,Http,我确实通过http读取数据,但我无法将此数据分配给TextView 当我在xml文件中创建TextView时,数据被分配给normal。但是当我以编程方式创建TextView时,文本是空的 public class News extends Activity { TextView myText; TextView title1; String s = ""; @SuppressLint({ "NewApi", "ResourceAsColor" }) @Over
public class News extends Activity
{
TextView myText;
TextView title1;
String s = "";
@SuppressLint({ "NewApi", "ResourceAsColor" }) @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
StrictMode.enableDefaults();
int textId1 = 1;
int textId2 = 2;
title1 = new TextView(this);
title1.setText("Заголовок новости 1 - 2013.01.01\n\nСегодня состоится заседание зампредпал тра та та");
title1.setTextColor(getResources().getColor(R.color.colorWihte));
title1.setShadowLayer(2, 0, 0,getResources().getColor(R.color.colorBlack));
title1.setBackground(getResources().getDrawable(R.drawable.round_block));
title1.setId(textId1);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
title1.setLayoutParams(layoutParams1);
((ViewGroup) findViewById(R.id.newslayout)).addView(title1);
String result = "";
InputStream isr = null;
try
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://test.ivanov-vladimir.com/get_content.php");
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "HTTP connect error " + e.toString());
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8);
StringBuilder builder = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
builder.append(line + "\n");
}
isr.close();
result = builder.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Data converting error " + e.toString());
}
try
{
JSONArray jarray = new JSONArray(result);
for(int i = 0; i < jarray.length(); i++)
{
JSONObject json = jarray.getJSONObject(i);
s = "Title: " + json.getString("title") + "\n\nContent: " + json.getString("content");
}
}
catch(Exception e)
{
Log.e("log_tag", "Data parsing error " + e.toString());
}
title1.setText(s);
}
}
公共类新闻扩展活动
{
文本查看我的文本;
文本视图标题1;
字符串s=“”;
@SuppressLint({“NewApi”,“ResourceAsColor”})@Override
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
StrictMode.enableDefaults();
int textId1=1;
int textId2=2;
title1=新文本视图(本);
标题1.setText(“аааааааааааааааааааааааааааа;
title1.setTextColor(getResources().getColor(R.color.colorWihte));
title1.setShadowLayer(2,0,0,getResources().getColor(R.color.colorBlack));
标题1.挫折背景(getResources().getDrawable(R.drawable.round_block));
标题1.setId(textId1);
RelativeLayout.LayoutParams layoutParams1=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
layoutParams1.addRule(RelativeLayout.ALIGN\u PARENT\u TOP);
layoutParams1.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT);
layoutParams1.addRule(RelativeLayout.ALIGN_U父节点\u RIGHT);
标题1.设置布局参数(布局参数1);
((视图组)findViewById(R.id.newslayout)).addView(标题1);
字符串结果=”;
InputStream isr=null;
尝试
{
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(“http://test.ivanov-vladimir.com/get_content.php");
HttpResponse response=client.execute(post);
HttpEntity=response.getEntity();
isr=entity.getContent();
}
捕获(例外e)
{
e(“Log_标记”,“HTTP连接错误”+e.toString());
}
尝试
{
BufferedReader=新的BufferedReader(新的InputStreamReader(isr,“utf-8”),8);
StringBuilder=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
builder.append(第+行“\n”);
}
isr.close();
结果=builder.toString();
}
捕获(例外e)
{
Log.e(“Log_标记”,“数据转换错误”+e.toString());
}
尝试
{
JSONArray jarray=新JSONArray(结果);
for(int i=0;i
现在,我尝试将数组的最后一个值jarray赋给元素title1。
这段代码是我在YouTube上的视频教程中得到的
如果你转到一个url,就会打印出符号,因为数据库中的文本是西里尔文。首先,尝试避免在UI线程中进行长时间的网络操作,这会降低你的应用程序的速度 然后,尝试返回TextView值并记录它,以查看是否为TextView分配了值:
Log.v("TextView value", title1.getText().toString());
然后,您还可以尝试记录从Web服务下载的内容:
while((line = reader.readLine()) != null)
{
builder.append(line + "\n");
Log.v("line", line);
}
请向我们提供日志结果。
HttpResponse response=client.execute(post)
你应该使用线程
或异步任务
进行与网络相关的操作将你的代码用于网络请求,否则它将在android版本>=3.0上崩溃。谢谢,我忘了在AndroidManifest中添加INTERNET premission。不客气!忘记添加互联网许可确实是我经常犯的一个常见错误!如果这个答案对你有帮助,你能接受吗?谢谢