开发者

Not able to connect ArrayAdapter to a ListView

开发者 https://www.devze.com 2023-01-31 12:49 出处:网络
I\'m working on a college project, and I\'m struggling to connect an ArrayAdapter to a ListView. Everything seems perfect to me, and in fact I\'ve used this code earlier in another app (it\'s in fact

I'm working on a college project, and I'm struggling to connect an ArrayAdapter to a ListView. Everything seems perfect to me, and in fact I've used this code earlier in another app (it's in fact from the IBM tutorial on RSS Readers for Android).

The parsing is working perfectly, which I'm able to see in the Log. I'm also able to see in the Log that the List of RSSItems has the required data, but when I connect the ArrayAdapter to a list view and the List of RSSItem objects, and then to the listview using setAdapter, nothing appears in the app (as in, the ListView is still empty)

Anyone who can point out why this might be happening? I'm attaching the code below

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 implements OnClickListener, OnItemClickListener { private String TAG="TheSportsCampus"; private RSSFeed feed= null; private List _itemlist; /** Called when the activity is first created. */

@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).get开发者_如何学运维Title()+","+_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);
}

}

EDIT: I'm also attaching the layout in case there's an error that I haven't noticed

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


Made the most basic of basic mistakes, I didn't select the main LinearLayout's orientation as vertical. Embarrassed that this is here now.


(Could you fix the layout a bit? )

Are you sure you're not getting anything? It could be (happend to me a couple of times) that the listview is filled and ready to go, but it doesn't show because of space (e.g. some other view taking up space with fill_parent, or something like that).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号