gpt4 book ai didi

java - Android Activity 上没有显示任何内容

转载 作者:行者123 更新时间:2023-12-01 11:15:15 25 4
gpt4 key购买 nike

我有以下名为 list_item_layout.xml 的布局 XML:

<ImageView
android:layout_width="80dp"
android:layout_height="40dp"
android:id="@+id/imageView" />

<TextView
android:layout_width="100dp"
android:layout_height="@dimen/String_height"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_alignBottom="@+id/imageView" />

<TextView
android:layout_width="60dp"
android:layout_height="@dimen/String_height"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView" />

<TextView
android:layout_width="60dp"
android:layout_height="@dimen/String_height"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView2" />

<TextView
android:layout_width="100dp"
android:layout_height="@dimen/String_height"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3" />

这是我的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.drexel.jacky.weather" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


<uses-permission android:name="android.permission.INTERNET" />
</manifest>

在 GUI 中的设计是这样的:

enter image description here

这是从 Weather Underground 获取数据的 Java 代码(位于用于网络的 doInBackground 线程中):

        String key= "someKey";

String sURL = "http://api.wunderground.com/api/someKey/geolookup/q/autoip.json";

// Connect to the URL
URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();


// Convert to a JSON object to print data
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject(); // may be Json Array if it's an array, or other type if a primitive



// Get some data elements and print them)
String city = rootobj.get("location").getAsJsonObject().get("city").getAsString();
String sURL1 = "http://api.wunderground.com/api/" + key + "/hourly/q/PA/"+city+ ".json";
URL url1 = new URL(sURL1);
HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
request1.connect();
JsonParser jp1 = new JsonParser();
JsonElement root1 = jp1.parse(new InputStreamReader((InputStream) request1.getContent()));
JsonObject rootobj1 = root1.getAsJsonObject(); // may be Json Array if it's an array, or other type if a primitive

String hour;
String date;
String humidity;
String image_url;
int l = rootobj1.get("hourly_forecast").getAsJsonArray().size();
for (int i = 0; i < l; i++){
hour = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("FCTTIME").getAsJsonObject().get("hour").getAsString();
date = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("FCTTIME").getAsJsonObject().get("weekday_name").getAsString();
humidity = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("temp").getAsJsonObject().get("english").getAsString();
image_url = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("icon_url").getAsString();
Weather weather = new Weather(city,hour,date,humidity,image_url);
data.add(i, weather);

Log.d("Hour: ", hour);
Log.d("Date: ", date);

//Insert data into the database
Sql_Open_Helper sql_open_helper = new Sql_Open_Helper(this.context);
SQLiteDatabase db = sql_open_helper.getWritableDatabase();
if(this.c) {
sql_open_helper.remake(db);
this.c = false;
}

ContentValues values = new ContentValues();
values.put("Temp", humidity);
values.put("Hour", hour);
values.put("Date", date);
values.put("City", city);

// Inserting Row
db.insert("Weather_Table", null, values);
db.close(); // Closing database connection
}

我一直在尝试使用以下布局让字符串显示在我的 Note 3(分辨率为 1080x1920)上,但完全没有取得任何进展。

当我编译应用程序并使用 Android Studio 加载它时, Activity 只是空白。

我尝试了很多事情。我确保 JSON 解析库确实返回了小时、图像 URL、温度等数据,并且它确实有效(我首先将该库创建为命令行应用程序)。

但是,数据并没有显示出来。奇怪的是,我在编译时根本没有看到任何错误。

有比我更有 Android 经验的人可以帮我调试这个吗?

编辑:

以下是我的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Create weather list for handling database
Sql_Open_Helper sql_open_helper = new Sql_Open_Helper(this);
SQLiteDatabase db = sql_open_helper.getReadableDatabase();
int hour = sql_open_helper.getHour(db); //Geting the last hour


//Geting current hour for checking with in the last hour
int current_hour = 14;
//DateFormat Hhour = new SimpleDateFormat("kk:00 a");
//int current_hour = Integer.parseInt(Hhour.format(Calendar.getInstance().getTime()).split(":",2)[0]);


//Compare the last hour with the current hour
if(current_hour <= hour) {
ArrayList<Weather> weather_list= sql_open_helper.addWeatherToList(db);
MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, weather_list,false);
ListView l = (ListView) findViewById(R.id.listView);
l.setAdapter(myArrayAdapter);
}


else {
Bg_task bg_task = new Bg_task(this);
MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, bg_task.getData(),true);
ListView l = (ListView) findViewById(R.id.listView);
l.setAdapter(myArrayAdapter);
bg_task.execute();
}
}

这是我为适配器编写的类MyArrayAdapter:

public class MyArrayAdapter extends ArrayAdapter<Weather> {

private Context context;
private ArrayList<Weather> array_weather;
private boolean b;
private boolean c;

public MyArrayAdapter(Context context, ArrayList<Weather> objects,boolean b) {
super(context,R.layout.list_item_layout, objects);
this.context = context;
array_weather = objects;
this.b = b;
this.c = true;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_layout,null);
}

TextView textView1 = (TextView) convertView.findViewById(R.id.textView);
TextView textView2 = (TextView) convertView.findViewById(R.id.textView2);
TextView textView3 = (TextView) convertView.findViewById(R.id.textView3);
TextView textView4 = (TextView) convertView.findViewById(R.id.textView4);
textView1.setText(array_weather.get(position).getCity());
textView2.setText(array_weather.get(position).getHuminity());
textView3.setText(array_weather.get(position).getHour());
textView4.setText(array_weather.get(position).getDate());
if(b){
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);//Display Image
new DownloadImageTask(imageView).execute(array_weather.get(position).getImage_url());
}
return convertView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
//e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}

}
}

最佳答案

完成异步任务后,您必须进行一些更改。在 onPostExecute 添加以下代码:

@Override
protected void onPostExecute(Void aVoid) {
MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, getData(),true);
ListView l = (ListView) findViewById(R.id.listView);
l.setAdapter(myArrayAdapter);
}

您的 Activity 如下所示:

//Compare the last hour with the current hour
if(current_hour <= hour) {
ArrayList<Weather> weather_list= sql_open_helper.addWeatherToList(db);
MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, weather_list,false);
ListView l = (ListView) findViewById(R.id.listView);
l.setAdapter(myArrayAdapter);
}


else {
Bg_task bg_task = new Bg_task(this);
bg_task.execute();
}

关于java - Android Activity 上没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31909545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com