我正在为狗爱好者创建一个应用程序(大学项目),我试图创建一个显示狗相关新闻的综合新闻源,并在点击时将用户带到网站。我做了一个虚拟应用程序来测试系统,它运行得很好。一旦我将它切换到我当前的Dog应用程序,它就完全停止了工作。问题是我用来显示文章列表的活动绝对没有显示任何内容。
这里有2个活动,一个是Links Activity,一个是newsItem Activity。 Links Activity是页面使用的主要活动,并以某种方式在页面上显示文本。 newsItem Activity充当getter并通过JSON文件来查找数组中的某些单词并将它们解析回应用程序。我还使用localHost来托管JSON文件。
如果你们能在这里帮助我,那将是不可思议的!我碰到了一堵完整的砖墙。
Links_Activity类连接到activity_links.xml文件
newsItem类连接到Article.xml文件
Links_Activity Class
package uk.ac.napier.doggo;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Links_Activity extends AppCompatActivity {
private List<newsItem> newsFeed = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_links);
engine();
addClickListener();
}
private void engine() {
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"http://10.0.2.2/news.json",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray newsItems = response.getJSONArray("newsItems");
for (int i = 0; i < newsItems.length(); i++) {
JSONObject temp = newsItems.getJSONObject(i);
String image = temp.getString("image");
String title = temp.getString("title");
String time = temp.getString("time");
String date = temp.getString("date");
String content = temp.getString("content");
String link = temp.getString("link");
newsFeed.add(new newsItem(title, content, date, time, link, image));
}
} catch(JSONException e){
Log.i("myTag", e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());
}
});
myReq.setRetryPolicy(new DefaultRetryPolicy(
30*1000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(myReq);
ArrayAdapter<newsItem> adapter = new customAdapter();
ListView newsItems = (ListView) (findViewById(R.id.newsItems));
newsItems.setAdapter(adapter);
}
private void addClickListener() {
ListView newsItems = (ListView) (findViewById(R.id.newsItems));
newsItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
newsItem currentItem = newsFeed.get(position);
Intent i = new Intent (Intent.ACTION_VIEW);
i.setData(Uri.parse(currentItem.getUrl()));
startActivity(i);
}
});
}
private class customAdapter extends ArrayAdapter<newsItem> {
public customAdapter() {
super(Links_Activity.this, R.layout.article, newsFeed);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.article, parent, false);
}
newsItem currentItem = newsFeed.get(position);
ImageView newsImage = (ImageView) convertView.findViewById(R.id.leftIco);
TextView heading = (TextView) convertView.findViewById(R.id.heading);
TextView desc = (TextView) convertView.findViewById(R.id.desc);
heading.setText(currentItem.getNewsHeading());
desc.setText(currentItem.getNewsDesc());
Picasso.with(Links_Activity.this).load(currentItem.getImageURL()).into(newsImage);
return convertView;
}
}
public void Back(View view) {
Intent startBackActivity = new Intent(this, Menu_Activity.class);
startActivity(startBackActivity);
}
}
activity_links.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="News Feed"
android:textSize="25sp"
android:textAlignment="center"
android:id="@+id/logo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TextView
android:text="Articles Courtesy of Dogster.com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:id="@+id/newsItems"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
newsItem类
package uk.ac.napier.doggo;
import java.util.PriorityQueue;
/**
* Created by MarkB on 10/03/2017.
*/
public class newsItem {
private String newsHeading;
private String newsDesc;
private String newsDescSmall;
private String time;
private String date;
private String url;
private String imageURL;
public newsItem(String newsHeading, String newsDesc, String date, String time, String url, String imageURL) {
this.newsHeading = newsHeading;
this.newsDesc = newsDesc;
this.time = time;
this.date = date;
this.url = url;
this.imageURL = imageURL;
this.newsDescSmall = this.newsDesc.substring(0, 50) + "...";
}
public String getNewsHeading() {
return newsHeading;
}
public String getNewsDesc() {
return newsDesc;
}
public String getTime() {
return time;
}
public String getDate() {
return date;
}
public String getUrl() {
return url;
}
public String getImageURL() {
return imageURL;
}
}
article.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="100dp"
android:id="@+id/leftIco"
android:src="@mipmap/ic_launcher"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Heading Text"
android:id="@+id/heading"
android:scrollHorizontally="false"
android:maxLines="100"
android:ellipsize="none"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/leftIco"
android:layout_toEndOf="@+id/leftIco"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description"
android:id="@+id/desc"
android:lineSpacingExtra="-3dp"
android:layout_marginBottom="26dp"
android:layout_alignBottom="@+id/leftIco"
android:layout_alignLeft="@+id/heading"
android:layout_alignStart="@+id/heading"/>
</RelativeLayout>
AndroidManifest.xml供参考
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="uk.ac.napier.doggo">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Home_Screen_Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Details_Activity" />
<activity android:name=".Menu_Activity" />
<activity android:name=".Reminders_Activity" />
<activity android:name=".Health_Tracker_Activity" />
<activity android:name=".Routes_Activity" />
<activity android:name=".Route_Complete_Activity" />
<activity android:name=".History_Activity" />
<activity android:name=".Links_Activity" >
</activity>
<activity android:name=".TimePopUp_Activity"
android:theme="@style/AppTheme.CustomTheme" >
</activity>
</application>
</manifest>
最后是news.JSON文件
{"newsItems":[
{
"image": "http://www.dogster.com/wp-content/uploads/2017/03/16806949_152063731974308_6992949846293798966_n-Cropped.jpg",
"title": "Artist Arien Smith Imagines Disney Princesses With Service Dogs",
"time": "10:00am ",
"date": "22 Jun ",
"content": "So far, the artist has drawn Cinderella and Snow White with service dogs, with the hope of raising awareness and money for his own helper.",
"link": "http://www.dogster.com/lifestyle/artist-arien-smith-imagines-disney-princesses-with-service-dogs"
},
{
"image": "http://www.dogster.com/wp-content/uploads/2016/06/Stalking-predatory-hero.png",
"title": "Is Your Dog a Bully?",
"time":"12:00am ",
"date":"Jun 12",
"content": "Dogs who bully not only start fights, but they can turn timid dogs into bullies themselves. Let's look at the signs of bullying and how to help your dog stop this behavior.",
"link": "http://www.dogster.com/lifestyle/is-your-dog-a-bully"
}
]
}
答案 0 :(得分:1)
您从未致电adapter.notifyDataSetChanged()
newsFeed.add(new newsItem(title, content, date, time, link, image));
} // end loop
// notify the adapter to display the new data
adapter.notifyDataSetChanged()
} catch(JSONException e){
Log.i("myTag", e.toString());
}
但是,你有一个ArrayAdapter,所以只需添加,而不是Arraylist然后你不需要通知
// See here
adapter.add(new newsItem(title, content, date, time, link, image));
} // end loop
} catch(JSONException e){
Log.i("myTag", e.toString());
}
应该对您的代码进行一些重新安排。
public class LinksActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener, Response.ErrorListener {
private List<NewsItem> newsFeed = new ArrayList<>();
private ArrayAdapter<NewsItem> adapter;
// Moved the Volley response to top-level
private Response.Listener<JSONObject> newsListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray newsItems = response.getJSONArray("newsItems");
for (int i = 0; i < newsItems.length(); i++) {
JSONObject temp = newsItems.getJSONObject(i);
String image = temp.getString("image");
String title = temp.getString("title");
String time = temp.getString("time");
String date = temp.getString("date");
String content = temp.getString("content");
String link = temp.getString("link");
newsFeed.add(new newsItem(title, content, date, time, link, image));
}
// Important!
adapter.notifyDataSetChanged();
} catch(JSONException e){
Log.i("myTag", e.toString());
}
}
};
// This is Volley's error listener over the entire Activity
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_links);
// Set and define the List and Adapter here
ListView newsItems = (ListView) findViewById(R.id.newsItems);
newsItems.setOnItemClickListener(this);
adapter = new CustomAdapter();
newsItems.setAdapter(adapter);
engine();
}
private void engine() {
RequestQueue queue = Volley.newRequestQueue(this);
// Now this method is much 'cleaner'
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"http://10.0.2.2/news.json",
null,
newsListener, this);
myReq.setRetryPolicy(new DefaultRetryPolicy(
30*1000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(myReq);
}
// The Activity itself handles the clicking
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsItem currentItem = newsFeed.get(position);
Intent i = new Intent (Intent.ACTION_VIEW);
i.setData(Uri.parse(currentItem.getUrl()));
startActivity(i);
}
}
答案 1 :(得分:0)
试试这个:
将数组项传递给适配器:
customAdapter adapter = new customAdapter(this, newsFeed);
ListView newsItems = (ListView) (findViewById(R.id.newsItems));
newsItems.setAdapter(adapter);
现在在适配器类中使用:
private List<newsItem> newsFeed_list ;
public customAdapter(Context context, ArrayList<newsItem> items) {
super(context, items);
this.newsFeed_list = items;
}
使用以下方式获取对象:
newsItem currentItem = newsFeed_list.get(position);