gpt4 book ai didi

java - 如何扩展 ArrayAdapter 以打印已解析的 JSON?

转载 作者:行者123 更新时间:2023-11-30 02:04:19 25 4
gpt4 key购买 nike

我已经尝试并阅读了很多答案,但无法弄清楚如何正确编码...

新闻适配器.java

public class NewsAdapter extends ArrayAdapter{
private static final String DEBUG_TAG = "NEWSADAPTER";
private LayoutInflater inflater;

public NewsAdapter(Activity activity, ArrayList<String> items){
super(activity, R.layout.news_feed, items);
Log.d(DEBUG_TAG, "This is came from the world i know of" + items);
inflater = activity.getWindow().getLayoutInflater();
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.news_feed, parent, false);
}
}

新闻提要.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView android:id="@+id/title"
android:text="@string/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="19sp" />

</LinearLayout>

上面代码的输出是...

enter image description here

如何获取items值以及如何设置Text?

最佳答案

这样做:

public class NewsAdapter extends ArrayAdapter{
private static final String DEBUG_TAG = "NEWSADAPTER";
private LayoutInflater inflater;
ArrayList<String> items;

public NewsAdapter(Activity activity, ArrayList<String> items){

inflater = activity.getWindow().getLayoutInflater();
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

View v = convertView
if(v == null)
v = inflater.inflate(R.layout.news_feed, parent, false);
TextView txtTitle = (TextView) v.findViewById(R.id.title);
txtTitle.setText(items.get(position));
return v;
}
}

关于java - 如何扩展 ArrayAdapter 以打印已解析的 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951744/

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