gpt4 book ai didi

android - 如何加载这些元素来填充 android 中的列表?

转载 作者:行者123 更新时间:2023-11-29 22:21:33 27 4
gpt4 key购买 nike

我正在使用它从网页中获取元素列表。 http://www.gamespy.com/index/release.html

// Get all the game elements
Elements games = doc.select("div.FP_Up_TextWrap b");

// Create new ArrayList
ArrayList<String> gameList = new ArrayList<String>();

// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();

while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}

这将返回我想要的所有标签。但它返回带有标题的完整标签我只想要标题发布日期和 Img sr。我想将其返回到 ListView 。我该怎么做呢?我可能完全错了,所以你们可能想查看 HTML 页面源代码。

编辑 - 给我 NullPointer 错误

  /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

outputTextView = (TextView)findViewById(R.id.outputTextView);

ListView list = (ListView)findViewById(R.id.list);
ArrayList<String> gameList = new ArrayList<String>();


Document doc = null;
try {
doc = Jsoup.connect("http://www.gamespy.com/index/release.html").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get all td's that are a child of a row - each game has 4 of these
Elements games = doc.select("tr > td.indexList1, tr > td.indexList2");
// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}

String[] items = new String[gameList.size()];
gameList.toArray(items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);

// error points here
list.setAdapter(adapter);
}


}

编辑 - 列表布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/outputTextView"
/>
<ListView android:layout_height="wrap_content" android:id="@+id/list" android:layout_width="match_parent"></ListView>
</LinearLayout>

最佳答案

您将无法使用此代码“返回 ListView ”,但您可以创建自己的 ListView 子类,然后您可以按照相同的技术返回该子类:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView listView1 = (ListView) findViewById(R.id.listView1);

ArrayList<String> gameList = new ArrayList<String>();

// Iterator over those elements
ListIterator<Element> postIt = games.listIterator();
while (postIt.hasNext()) {
// Add the game text to the ArrayList
gameList.add(postIt.next().text());
}

String[] items = new String[gameList.size()];
gameList.toArray(items);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.some_list_item_view, items);

listView1.setAdapter(adapter);
}

最终归结为为列表设置列表适配器。如果您需要创建自己的列表适配器,您可以子类化并实现 ListAdapter 以对其他对象类型进行数据绑定(bind),但已经有一个预定义的适配器 (ArrayAdapter) 用于处理 ListView/ListActivity 数据绑定(bind)的简单字符串集。

更新 - 发帖人要求提供更多实现细节:

给定:

public class GameMeta {
private String m_title;
private java.util.Date m_releaseDate;

public GameMeta() {
}

public String getTitle(){
return m_title;
}

public void setTitle(String value) {
m_title = value;
}

public java.util.Date getReleaseDate(){
return m_releaseDate;
}

public void setReleaseDate(java.util.Date releaseDate){
m_releaseDate = releaseDate;
}
}

您可以创建适配器:

public class GamesMetaAdapter implements Adapter extends ListAdapter {
private ArrayList<GameMeta> m_list = new ArrayList<GameMeta>();
private Context m_context = null;

public GamesMetaAdapter(Context context) {
m_context = context;
}

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

@Override
public Object getItem(int position){
if(position < m_list.size()){
return m_list.getAt(position);
}
return null;
}

@Override
public long getItemId(int position) {
return (long)position;
}

@Override
public int getItemViewType(int position) {
return IGNORE_ITEM_VIEW_TYPE;
}

@Override
public View getView (int position, View convertView, ViewGroup parent) {
TextView simpleView = new TextView(this.m_context);
String viewText = this.m_items[position].getTitle() + ", Released " + this.m_items[position].getReleaseDate().toString();

simpleView.setText(this.m_items[position].getTitle());

if(TextView.class.isInstance(convertView)) {
convertView = simpleView;
}

return simpleView;
}


@Override
public int getViewTypeCount() {
return 1;
}

@Override
public boolean hasStableIds(){
return false;
}

@Override
public boolean isEmpty() {
return m_list.size() == 0;
}

@Override
public void registerDataSetObserver (DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver (DataSetObserver observer) {

}

/* LIST ADAPTER MEMBERS */
@Override
public boolean areAllItemsEnabled() {
return true;
}

@Override
public boolean isEnabled(int position) {
return true;
}
}

我这里没有 Java 编译器,所以如果这不能直接复制/粘贴到您的项目中,请不要给我打电话,但它应该远远超过 90%。

快乐编码。

B

关于android - 如何加载这些元素来填充 android 中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7065756/

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