我有以下布局XML list_item_layout.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中,设计如下:
这是从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加载它时,活动只是空白。
我尝试了很多东西。我确保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);
}
}
}
答案 0 :(得分:1)
完成异步任务后,您必须进行一些更改。 在onPostExecute中添加以下代码:
@Override
protected void onPostExecute(Void aVoid) {
MyArrayAdapter myArrayAdapter = new MyArrayAdapter(this, getData(),true);
ListView l = (ListView) findViewById(R.id.listView);
l.setAdapter(myArrayAdapter);
}
您的活动如下所示:
//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();
}