如何获取动态设置为 TextView 的文本?
how to get the text that was set dynamically to a TextView?
在我的 MainActivity.java 中,我已经为 TextView 设置了一些文本,如何获取我设置到 TextView 的文本
我的文本视图 -
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
异步任务-
public class fetchdata extends AsyncTask<Void, Void, Void> {
private String data = "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://blablabla.com/hello.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
data = bufferedReader.readLine();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.show.setText(this.data);
}
}
主要活动 -
private static TextView show;
......显示是静态的
show = findViewById(R.id.show);
String data = show.getText().toString();
但是数据是空的而不是我得到 "this is the text I want to get"
class fetchdata extends AsyncTask<Void, Void, String> {
private String data = "";
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://blablabla.com/hello.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
return data = bufferedReader.readLine();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
show.setText(aVoid);
}
}
考虑到下面两行是在一起的,
show = findViewById(R.id.show);
String data = show.getText().toString();
我猜它们在 onCreate
中,从而导致 data
.
为空
您需要等待 AsyncTask
完成并 TextView
在 onPostExecute
中设置它的值,然后再尝试在 UI 中获取它线。 (你可以使用 callback)
在我的 MainActivity.java 中,我已经为 TextView 设置了一些文本,如何获取我设置到 TextView 的文本
我的文本视图 -
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible" />
异步任务-
public class fetchdata extends AsyncTask<Void, Void, Void> {
private String data = "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://blablabla.com/hello.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
data = bufferedReader.readLine();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.show.setText(this.data);
}
}
主要活动 -
private static TextView show;
......显示是静态的
show = findViewById(R.id.show);
String data = show.getText().toString();
但是数据是空的而不是我得到 "this is the text I want to get"
class fetchdata extends AsyncTask<Void, Void, String> {
private String data = "";
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://blablabla.com/hello.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
return data = bufferedReader.readLine();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
show.setText(aVoid);
}
}
考虑到下面两行是在一起的,
show = findViewById(R.id.show);
String data = show.getText().toString();
我猜它们在 onCreate
中,从而导致 data
.
您需要等待 AsyncTask
完成并 TextView
在 onPostExecute
中设置它的值,然后再尝试在 UI 中获取它线。 (你可以使用 callback)