Не удалось подключить ArrayAdapter к ListView

Я работаю над проектом колледжа и пытаюсь подключить ArrayAdapter к ListView. Мне все кажется идеальным, и на самом деле я использовал этот код ранее в другом приложении (на самом деле это из учебника IBM по RSS Readers для Android).

Анализ работает отлично, что я вижу в журнале. Я также вижу в журнале, что в списке RSSItems есть необходимые данные, но когда я подключаю ArrayAdapter к списку и списку объектов RSSItem, а затем к списку с помощью setAdapter, в приложении ничего не появляется. (например, 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;

открытый класс HomePage расширяет действие, реализует OnClickListener, OnItemClickListener { private String TAG="TheSportsCampus"; частный канал RSSFeed = ноль; частный список _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>

person Raveesh    schedule 19.12.2010    source источник
comment
Проблема, на мой взгляд, происходит здесь: ArrayAdapter‹RSSItem› adapter = new ArrayAdapter‹RSSItem›(this,android.R.layout.simple_list_item_1,_itemlist); Log.d(TAG, RSSItems добавлены в ArrayAdapter); ListView mainList=(ListView)findViewById(R.id.ListViewMain); mainList.setAdapter (адаптер);   -  person Raveesh    schedule 19.12.2010
comment
Исправьте форматирование кода. Это невозможно читать.   -  person Macarse    schedule 19.12.2010


Ответы (2)


Сделал самую основную из основных ошибок, я не выбрал основную ориентацию LinearLayout как вертикальную. Стыдно, что это сейчас здесь.

person Raveesh    schedule 19.12.2010

(Не могли бы вы немного исправить макет?)

Вы уверены, что ничего не получаете? Может случиться (со мной случилось пару раз), что представление списка заполнено и готово к работе, но оно не отображается из-за места (например, какое-то другое представление, занимающее место с fill_parent, или что-то в этом роде).

person Nanne    schedule 19.12.2010
comment
Я разместил код для макета. Я не вижу в этом причины, поскольку по вертикали почти нет представлений. - person Raveesh; 19.12.2010