无法从Android中的ListView链接URL

问题描述 投票:0回答:1

您好,我想单击ListView并从ListView中打开URL。

我可以使用ListView上的设置方法设置URL地址,但是当我尝试使用Toast消息显示URL时,它显示了不同的URL。

我使用for循环在ListView news_ListView上添加多个项目

如果有任何建议,请帮帮我。

private NewsListAdapter newsListAdapter;
private ArrayList<NewsContents> newsContents_View;
private String title_String;
private String body_String;
private String source_String;
private String source_iconUrl_String;
private String image_Url_String;
private String url_String;
private int column_num;
private int num;
private int num_plus;
private int num_plus_plus;
private View mfooter;
 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ListView news_ListView = Objects.requireNonNull(getView()).findViewById(R.id.news_ListView);
    newsContents_View = new ArrayList<>();
    newsListAdapter = new NewsListAdapter(Objects.requireNonNull(getActivity()).getApplicationContext());
    newsListAdapter.setNewsContents(newsContents_View);
    news_ListView.setAdapter(newsListAdapter);
    news_ListView.addFooterView(getfooter());

    num = 0;
    num_plus = num + 5;
    //Set newsList
    for (column_num = num; column_num < num_plus; column_num++){
        //Set news to listview according to the column num
        Set_Content_to_NewsList(column_num);
    }

    news_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(),""+url_String,Toast.LENGTH_SHORT).show();
        }
    });
}
private void Set_Content_to_NewsList(final int column_num){

    HttpRequest_News httpRequest_news = new HttpRequest_News(Objects.requireNonNull(getActivity()).getApplicationContext());
    httpRequest_news.news_From_API(news_Url + news_Url1 + API_KEY, column_num, new HttpRequest_News.ApiCallback() {
        @Override
        public void onOkHttpResponse(String title, String body, String source, String source_iconurl, String img_url,String url) {
            title_String = title;
            body_String = body;
            source_String = source;
            source_iconUrl_String = source_iconurl;
            image_Url_String = img_url;
            url_String = url;
            setItem(column_num,title_String,body_String,source_String,source_iconUrl_String,image_Url_String,url_String);
        }
        @Override
        public void onOkHttpFailure(Exception exception) {}
    });
}

private void setItem(int column_num,String title_String, String body_String, String source_String,String source_iconUrl_String, String image_Url_String, String url_String){
    NewsContents newsContents = new NewsContents();
    newsContents.setId(column_num);
    newsContents.setTitle_Name(title_String);
    newsContents.setBody_Name(body_String);
    newsContents.setSource_Name(source_String);
    newsContents.setSource_icon_imgUrl(source_iconUrl_String);
    newsContents.setImgUrl(image_Url_String);
    newsContents.setUrl(url_String);
    newsContents_View.add(newsContents);
    newsListAdapter.notifyDataSetChanged();
}

这是HttpRequestNews类:

public class HttpRequest_News {

private Context context;

public HttpRequest_News(Context current){
    this.context = current;
}

public void news_From_API(String url_news, final int index, final ApiCallback apiCallback){

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url_news)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            apiCallback.onOkHttpFailure(e);
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            final String jsonStr = Objects.requireNonNull(response.body()).string();
            final String title_Str = jsonarray_news_extract_Title(jsonStr, index);
            final String body_Str = jsonarray_news_extract_Body(jsonStr,index);
            final String source_Str = jsonarray_news_extract_Source(jsonStr,index);
            final String source_icon_url_Str = jsonarray_news_extract_icon_Source(jsonStr,index);
            final String image_url_Str = jsonarray_news_extract_ImgUrl(jsonStr,index);
            final String url_Str = jsonarray_news_extract_Url(jsonStr,index);

            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
                    apiCallback.onOkHttpResponse(title_Str,body_Str,source_Str,source_icon_url_Str,image_url_Str,url_Str);
                }
            });
        }
    });
}

private String jsonarray_news_extract_Title(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("title");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

private String jsonarray_news_extract_Body(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("body");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

private String jsonarray_news_extract_Source(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("source");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

private String jsonarray_news_extract_icon_Source(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        JSONObject jsonObject1 = jsonArray.getJSONObject(index).getJSONObject("source_info");
        return jsonObject1.getString("img");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

private String jsonarray_news_extract_ImgUrl(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("imageurl");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

private String jsonarray_news_extract_Url(String jsonString_News, int index){
    try {
        JSONObject jsonObject = new JSONObject(jsonString_News);
        JSONArray jsonArray = jsonObject.getJSONArray("Data");
        return jsonArray.getJSONObject(index).getString("url");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

public interface ApiCallback{
    void onOkHttpResponse(String title, String body, String source, String source_iconUrl, String img_url, String url);
    void onOkHttpFailure(Exception exception);
}}

这是NewsContents类:

public class NewsContents {
private long id;
private String Title_Name;
private String Body_Name;
private String Url;
private String Source_Name;
private String imgUrl;
private String source_icon_imgUrl;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getTitle_Name() {
    return Title_Name;
}

public void setTitle_Name(String title_Name) {
    this.Title_Name = title_Name;
}
public String getSource_Name() {
    return Source_Name;
}

public void setSource_Name(String source_Name) {
    this.Source_Name = source_Name;
}

public String getUrl() {
    return Url;
}

public void setUrl(String url) {
    this.Url = url;
}

public String getBody_Name() {
    return Body_Name;
}

public void setBody_Name(String body_Name) {
    Body_Name = body_Name;
}

public String getImgUrl() {
    return imgUrl;
}

public void setImgUrl(String imgUrl) {
    this.imgUrl = imgUrl;
}

public String getSource_icon_imgUrl() {
    return source_icon_imgUrl;
}

public void setSource_icon_imgUrl(String source_icon_imgUrl) {
    this.source_icon_imgUrl = source_icon_imgUrl;
}}

这是listView的适配器

public class NewsListAdapter extends BaseAdapter {

private Context context;
private LayoutInflater layoutInflater;
private ArrayList<NewsContents> newsContents;

public NewsListAdapter(Context current){
    this.context = current;
    this.layoutInflater = (LayoutInflater)current.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public void setNewsContents(ArrayList<NewsContents> newsContents){
    this.newsContents = newsContents;
}

@Override
public int getCount() {
    return newsContents.size();
}

@Override
public Object getItem(int position) {
    return newsContents.get(position);
}

@Override
public long getItemId(int position) {
    return newsContents.get(position).getId();
}

@SuppressLint({"ViewHolder", "InflateParams"})
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = layoutInflater.inflate(R.layout.news_row,parent,false);
    ((TextView)convertView.findViewById(R.id.News_Title)).setText(newsContents.get(position).getTitle_Name());
    ((TextView)convertView.findViewById(R.id.news_body)).setText(newsContents.get(position).getBody_Name());
    ((TextView)convertView.findViewById(R.id.news_sourceName)).setText(newsContents.get(position).getSource_Name());
    ((TextView)convertView.findViewById(R.id.url_text)).setText(newsContents.get(position).getUrl());
    Picasso.
            get().
            load(newsContents.get(position).getImgUrl()).
            into((ImageView) convertView.findViewById(R.id.news_img));
    Picasso.
            get().
            load(newsContents.get(position).getSource_icon_imgUrl()).
            into((ImageView) convertView.findViewById(R.id.news_icon_img));

    return convertView;
}}

感谢您阅读代码!

enter image description here

java android android-listview
1个回答
0
投票

您的吐司消息来自“ url_String”。触发“ onOkHttpResponse()”时,将更新“ url_String”。触发“ Set_Content_to_NewsList()”时,将调用“ onOkHttpResponse()”。“ Set_Content_to_NewsList()”被触发5次。吐司消息将始终获得最新的“ url_String”,而不是其他任何行。一种快速的解决方法是将每个“ url_String”存储在数组中,并使用“ position”作为此数组的索引。

© www.soinside.com 2019 - 2024. All rights reserved.