Android 无法将ArrayAdapter连接到ListView
我正在做一个大学项目,我正在努力将ArrayAdapter连接到ListView。对我来说,一切似乎都很完美,事实上,我之前在另一个应用程序中使用过这段代码(事实上,它来自IBM关于Android RSS阅读器的教程) 解析工作正常,我可以在日志中看到。我还可以在日志中看到RSSItem列表包含所需的数据,但当我将ArrayAdapter连接到列表视图和RSSItem对象列表,然后使用setAdapter连接到listview时,应用程序中没有显示任何内容(如中所示,listview仍然为空) 有谁能指出为什么会发生这种情况?我在下面附上代码 导入java.util.List; 导入android.app.Activity; 导入android.content.Intent; 导入android.os.Bundle; 导入android.util.Log; 导入android.view.view; 导入android.view.view.OnClickListener; 导入android.widget.AdapterView; 导入android.widget.ArrayAdapter; 导入android.widget.ImageButton; 导入android.widget.ListView; 导入android.widget.AdapterView.OnItemClickListener 公共类主页扩展活动实现OnClickListener、OnItemClickListener { 私有字符串TAG=“TheSportsCampus”; 专用RSSFeed feed=null; 私有列表_itemlist; /**在首次创建活动时调用*/Android 无法将ArrayAdapter连接到ListView,android,android-listview,Android,Android Listview,我正在做一个大学项目,我正在努力将ArrayAdapter连接到ListView。对我来说,一切似乎都很完美,事实上,我之前在另一个应用程序中使用过这段代码(事实上,它来自IBM关于Android RSS阅读器的教程) 解析工作正常,我可以在日志中看到。我还可以在日志中看到RSSItem列表包含所需的数据,但当我将ArrayAdapter连接到列表视图和RSSItem对象列表,然后使用setAdapter连接到listview时,应用程序中没有显示任何内容(如中所示,listview仍然为空)
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(TAG, "onCreate Started");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "Layout expanded");
ImageButton cricket=(ImageButton) findViewById(R.id.ImageButtonCricket);
ImageButton football=(ImageButton) findViewById(R.id.ImageButtonFootball);
ImageButton tennis=(ImageButton) findViewById(R.id.ImageButtonTennis);
ImageButton motorsports=(ImageButton) findViewById(R.id.ImageButtonMotorsports);
ImageButton other=(ImageButton) findViewById(R.id.ImageButtonOther);
ImageButton newsbytes=(ImageButton) findViewById(R.id.ImageButtonNewsBytes);
ImageButton live=(ImageButton) findViewById(R.id.ImageButtonLive);
ImageButton calendar=(ImageButton) findViewById(R.id.ImageButtonCalendar);
Log.d(TAG, "ImageButtons invoked in code");
cricket.setOnClickListener(this);
football.setOnClickListener(this);
tennis.setOnClickListener(this);
motorsports.setOnClickListener(this);
other.setOnClickListener(this);
newsbytes.setOnClickListener(this);
Log.d(TAG, "ImageButtons ClickListeners created");
live.setOnClickListener(this);
/* calendar.setOnClickListener(this);
*/
}
@Override
public void onClick(View v)
{
Log.d(TAG, "ImageButton clicked");
String option="http://www.appsculture.com/tsc/";
switch (v.getId())
{
case R.id.ImageButtonCricket: option=option.concat("cricket/cricket.xml");
break;
case R.id.ImageButtonFootball: option=option.concat("football/football.xml");
break;
case R.id.ImageButtonTennis:option=option.concat("tennis/tennis.xml");
break;
case R.id.ImageButtonMotorsports: option=option.concat("motorsports/motorsports.xml");
break;
case R.id.ImageButtonOther: option=option.concat("others/others.xml");
break;
case R.id.ImageButtonNewsBytes:option=option.concat("news/news.xml");
break;
case R.id.ImageButtonLive: option="http://www.thesportscampus.com/feed/rss?format=feed";
break;
case R.id.ImageButtonCalendar:
break;
}
Log.d(TAG, option);
feed= new RSSFeed();
feed=feed.getFeed(option);
Log.d(TAG, "feed.getFeed() executed and returned");
UpdateDisplay();
}
private void UpdateDisplay()
{
Log.d(TAG, "Function UpdateDisplay() launched");
_itemlist=feed.getAllItems();
Log.d(TAG,"Received itemlist with itemcount= "+_itemlist.size());
Log.d(TAG,"List of articles: <"+_itemlist.get(0)+","+_itemlist.get(1).getTitle()+","+_itemlist.get(2).getTitle()+","+_itemlist.get(3).getTitle()+">");
if (feed == null)
Log.d(TAG,"FEED is NULL!");
ArrayAdapter<RSSItem> adapter = new
ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,_itemlist);
Log.d(TAG, "RSSItems added to ArrayAdapter");
ListView mainList=(ListView)findViewById(R.id.ListViewMain);
mainList.setAdapter(adapter);
Log.d(TAG, "ArrayAdapter added to mainList");
// mainList.setSelection(0);
Log.d(TAG, "mainList Selection done");
//mainList.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView parent, View v, int position, long id)
{
//Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");
Intent itemintent = new Intent(this,ShowDescription.class);
Bundle b = new Bundle();
b.putString("title", feed.getItem(position).getTitle());
b.putString("description", feed.getItem(position).getDescription());
b.putString("link", feed.getItem(position).getLink());
b.putString("cmsurl", feed.getItem(position).getCmsURL());
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivity(itemintent);
}
@覆盖
创建时的公共void(Bundle savedInstanceState)
{
Log.d(标记“onCreate已启动”);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(标签“布局扩展”);
ImageButton cricket=(ImageButton)findViewById(R.id.ImageButtonCricket);
ImageButton football=(ImageButton)findViewById(R.id.ImageButtonFootball);
ImageButton网球=(ImageButton)findViewById(R.id.ImageButtonTennis);
ImageButton motorsports=(ImageButton)findViewById(R.id.ImageButtonMotorsports);
ImageButton other=(ImageButton)findViewById(R.id.ImageButtonOther);
ImageButton newsbytes=(ImageButton)findViewById(R.id.ImageButtonNewsBytes);
ImageButton live=(ImageButton)findViewById(R.id.ImageButtonLive);
ImageButton日历=(ImageButton)findViewById(R.id.ImageButtonCalendar);
Log.d(标记,“代码中调用的ImageButtons”);
蟋蟀。setOnClickListener(这个);
football.setOnClickListener(这个);
网球。setOnClickListener(这个);
motorsports.setOnClickListener(此);
其他.setOnClickListener(此);
newsbytes.setOnClickListener(此);
d(标记“ImageButtons ClickListeners created”);
live.setOnClickListener(this);
/*calendar.setOnClickListener(此);
*/
}
@凌驾
公共void onClick(视图v)
{
Log.d(标记“ImageButton clicked”);
字符串选项=”http://www.appsculture.com/tsc/";
开关(v.getId())
{
case R.id.ImageButtonCricket:option=option.concat(“cricket/cricket.xml”);
打破
case R.id.ImageButtonFootball:option=option.concat(“football/football.xml”);
打破
case R.id.imagebuttonennis:option=option.concat(“netsines/netsing.xml”);
打破
case R.id.ImageButtonMotorsports:option=option.concat(“motorsports/motorsports.xml”);
打破
case R.id.ImageButtonOther:option=option.concat(“others/others.xml”);
打破
case R.id.ImageButtonNewsBytes:option=option.concat(“news/news.xml”);
打破
case R.id.ImageButtonLive:选项=”http://www.thesportscampus.com/feed/rss?format=feed";
打破
案例R.id.ImageButtonCalendar:
打破
}
Log.d(标签、选项);
feed=新的RSSFeed();
feed=feed.getFeed(可选);
d(标记“feed.getFeed()已执行并返回”);
UpdateDisplay();
}
私有void UpdateDisplay()
{
d(标记“函数UpdateDisplay()已启动”);
_itemlist=feed.getAllItems();
Log.d(标记“已接收的itemlist with itemcount=“+_itemlist.size()”);
Log.d(标签“物品清单”);
if(feed==null)
Log.d(标记“FEED为NULL!”);
ArrayAdapter适配器=新
ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,\u itemlist);
Log.d(标签“添加到ArrayAdapter的RSSItems”);
ListView mainList=(ListView)findViewById(R.id.ListViewMain);
mainList.setAdapter(适配器);
Log.d(标签“ArrayAdapter添加到主列表”);
//mainList.setSelection(0);
Log.d(标记“主列表选择完成”);
//mainList.setOnItemClickListener(此);
}
@凌驾
public void onItemClick(AdapterView父视图、视图v、整型位置、长id)
{
//Log.i(标记“单击的项”[“+feed.getItem(position.getTitle()+”]);
Intent itemintent=新的Intent(这个,showsdescription.class);
Bundle b=新Bundle();
b、 putString(“title”,feed.getItem(position.getTitle());
b、 putString(“description”,feed.getItem(position.getDescription());
b、 putString(“link”,feed.getItem(position.getLink());
b、 putString(“cmsurl”,feed.getItem(position.getCmsURL());
itemintent.putExtra(“android.intent.extra.intent”,b);
startActivity(itemintent);
}
}
编辑:我还附加了布局,以防有我没有注意到的错误
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@color/Black"><HorizontalScrollView android:layout_width="fill_parent" android:background="@color/Black" android:layout_gravity="center_horizontal" android:scrollbars="none" android:layout_height="wrap_content"><LinearLayout android:layout_height="wrap_content" android:orientation="horizontal" android:background="@color/Black" android:layout_width="fill_parent"><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/cricket" android:id="@+id/ImageButtonCricket" android:layout_weight="1"></ImageButton><ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/football" android:id="@+id/ImageButtonFootball" android:layout_weight="1"></ImageButton><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tennis" android:id="@+id/ImageButtonTennis" android:layout_weight="1"></ImageButton><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/motorsports" android:id="@+id/ImageButtonMotorsports" android:layout_weight="1"></ImageButton><ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/other" android:id="@+id/ImageButtonOther" android:layout_weight="1"></ImageButton><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/newsbytes" android:id="@+id/ImageButtonNewsBytes" android:layout_weight="1"></ImageButton><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/live" android:id="@+id/ImageButtonLive" android:layout_weight="1"></ImageButton><ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ImageButtonCalendar" android:background="@drawable/calendar" android:layout_weight="1"></ImageButton></LinearLayout></HorizontalScrollView><ListView android:id="@+id/ListViewMain" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"></ListView></LinearLayout>
(你能稍微修改一下布局吗?)
你确定你什么都没得到吗?可能(我偶尔会遇到)listview已填充并准备就绪,但由于空间原因(例如,某些其他视图占用了填充父视图的空间,或类似的情况)它没有显示。犯了最基本的基本错误,我没有选择主线布局的方向为垂直。尴尬的是,现在出现了这种情况。在我看来,问题发生在这里:ArrayAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,\u itemlist);Log.d(标签“添加到ArrayAdapter的RSSItems”);ListView mainList=(ListView)findViewById(R.id.ListViewMain);mainList.setAdapter(适配器);修正你的代码格式。这是不可能读的。我已经发布了停车代码