JADI Saya membuat aplikasi untuk pecinta anjing (proyek Universitas) dan saya mencoba membuat umpan berita terintegrasi yang menampilkan berita terkait anjing dan ketika diklik akan membawa pengguna ke situs web. Saya membuat aplikasi dummy untuk menguji sistem dan itu bekerja dengan sempurna. segera setelah saya mengalihkannya ke aplikasi Anjing saya saat ini, aplikasi itu benar-benar berhenti berfungsi. Masalahnya adalah Aktivitas yang saya gunakan untuk menampilkan daftar artikel sama sekali tidak menunjukkan apa-apa.

Ada 2 aktivitas di sini, Aktivitas Tautan dan Aktivitas newsItem. Aktivitas Tautan adalah aktivitas utama yang digunakan halaman dan menampilkan teks dengan cara tertentu di halaman. NewsItem Activity bertindak sebagai pengambil dan bekerja melalui file JSON untuk menemukan kata-kata tertentu dalam array dan menguraikannya kembali ke aplikasi. Saya juga menggunakan localHost untuk meng-host file JSON.

Jika kalian dapat membantu saya di sini, itu akan luar biasa! Saya telah menabrak dinding bata yang lengkap.

Kelas Links_Activity terhubung ke file activity_links.xml

Kelas newsItem terhubung ke file Article.xml

Kelas Links_Activity

   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>

Kelas Item berita

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;
    }
}

artikel.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 untuk referensi

<?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>

Akhirnya file 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"
}
]
}
-1
Mark Barton 11 Maret 2017, 17:43

2 jawaban

Jawaban Terbaik

Anda tidak pernah menelepon 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());
}

Namun, Anda memiliki ArrayAdapter, jadi tambahkan saja, bukan Arraylist maka Anda tidak perlu memberi tahu

        // See here
        adapter.add(new newsItem(title, content, date, time, link, image));
    }  // end loop
} catch(JSONException e){
    Log.i("myTag", e.toString());
}

Beberapa pengaturan ulang kode Anda yang seharusnya berfungsi.

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
OneCricketeer 11 Maret 2017, 20:48

Coba ini:

Meneruskan item array ke adaptor:

customAdapter adapter = new customAdapter(this, newsFeed);
ListView  newsItems = (ListView) (findViewById(R.id.newsItems));
newsItems.setAdapter(adapter);

Sekarang di kelas adaptor gunakan:

private List<newsItem> newsFeed_list ;

   public customAdapter(Context context, ArrayList<newsItem> items) {
        super(context, items);
        this.newsFeed_list = items;
    }

Dan dapatkan objek menggunakan:

newsItem currentItem = newsFeed_list.get(position);
0
rafsanahmad007 11 Maret 2017, 15:25