无法将ArrayAdapter连接到ListView

时间:2010-12-19 13:04:36

标签: android android-listview

我正在开展大学项目,我正在努力将ArrayAdapter连接到ListView。对我来说一切似乎都很完美,事实上我之前在另一个应用程序中使用过此代码(事实上,它来自IBM的RSS Readers for Android教程)。

解析工作完美,我可以在日志中看到。我还能够在Log中看到RSSItems列表具有所需的数据,但是当我将ArrayAdapter连接到列表视图和List of RSSItem对象,然后使用setAdapter连接到listview时,app中没有任何内容(例如,ListView仍为空)

任何可以指出为什么会发生这种情况的人?我附上下面的代码

import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener;

public class HomePage extends Activity实现OnClickListener,OnItemClickListener {     private String TAG =“TheSportsCampus”;     私人RSSFeed feed = null;     私人名单_itemlist;     /** 在第一次创建活动时调用。 * /

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

}

编辑:我还附上了布局,以防出现我没有注意到的错误

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

2 个答案:

答案 0 :(得分:1)

犯了最基本的错误,我没有选择主要的LinearLayout的方向为垂直方向。很尴尬,现在就在这里。

答案 1 :(得分:0)

(你可以稍微修改一下布局吗?)

你确定没有得到任何东西吗?它可能(发生在我身上几次)列表视图已填充并准备就绪,但由于空间不显示(例如,某些其他视图占用了fill_parent的空间,或类似的东西)。