1

I have a custom ltView with a listview adapter class and i have a code stub which populates 4 rows of my lsit view. The xmls etc are looking good and the activity builds itself ok but i cant seem to insert a handler without causing an error.

Here is the code I have so far.

GamesSummary

public class GamesSummary extends ListActivity {
    private ProgressDialog m_ProgressDialog = null;
    private ArrayList<MyGamesList> myGamesArrayList = null;
    private MyGamesAdapter myGames_adapter;
    private Runnable viewGames;

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

        myGamesArrayList = new ArrayList<MyGamesList>();
        this.myGames_adapter = new MyGamesAdapter(this, R.layout.row,
                myGamesArrayList);
        setListAdapter(this.myGames_adapter);

        // set Item click listener for list view

        viewGames = new Runnable() {
            @Override
            public void run() {
                getMyGames();
            }
        };
        // Start the 'waiting' dialog thread
        Thread thread = new Thread(null, viewGames, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(GamesSummary.this,
                "Please wait...", "Retrieving data ...", true);
    }

MyGamesAdapter

    private class MyGamesAdapter extends ArrayAdapter<MyGamesList> {

        private ArrayList<MyGamesList> items;

        public MyGamesAdapter(Context context, int textViewResourceId,
                ArrayList<MyGamesList> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            MyGamesList o = items.get(position);
            if (o != null) {

            }
            return v;
        }
    }

Populating the list

    private void getMyGames() {
        /*
         * Add the users current games using the myGames class. example below of
         * stubb code use
         */
        try {
            myGamesArrayList = new ArrayList<MyGamesList>();
            MyGamesList myGame1 = new MyGamesList();
            myGame1.setLeagueName("game1");
            myGame1.setLeaguePoints("36");
            myGame1.setLeaguePos("26");
            myGame1.setLeagueRound("5");
            MyGamesList myGame2 = new MyGamesList();
            myGame2.setLeagueName("game2");
            myGame2.setLeaguePoints("36");
            myGame2.setLeaguePos("26");
            myGame2.setLeagueRound("5");
            myGamesArrayList.add(myGame1);
            myGamesArrayList.add(myGame2);
            Thread.sleep(2000);
            Log.i("ARRAY", "" + myGamesArrayList.size());
        } catch (Exception e) {
            Log.e("BACKGROUND_PROC", e.getMessage());
        }
        runOnUiThread(returnRes);
    }

The returnRes Runnable

    private Runnable returnRes = new Runnable() {
        @Override
        public void run() {
            if (myGamesArrayList != null && myGamesArrayList.size() > 0) {
                myGames_adapter.notifyDataSetChanged();
                for (int i = 0; i < myGamesArrayList.size(); i++)
                    myGames_adapter.add(myGamesArrayList.get(i));
            }
            m_ProgressDialog.dismiss();
            myGames_adapter.notifyDataSetChanged();
        }
    };
}
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
Fearghal
  • 10,569
  • 17
  • 55
  • 97

1 Answers1

2

A click handler for your listview would work like this:

getListView().setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});

and in your xml layout, don't forget the id:

             <ListView
                android:id="@android:id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:dividerHeight="0dp"
                android:divider="#ffffff"
                android:clickable="true"
                android:cacheColorHint="#00000000"
                android:fadingEdge="none"
                android:fastScrollEnabled="false"
                android:footerDividersEnabled="false"
                android:headerDividersEnabled="false"
                android:smoothScrollbar="true"
                />
Tíbó
  • 1,188
  • 13
  • 28
  • Alternatively, inside getView, add v.setOnClickListener before the return statement. – dannyroa May 19 '14 at 22:02
  • @dannyroa, if you have interactive elements inside the item renderer, you may opt for that solution, but this approach (using the `AdapterView`'s item click handler is the best. – rekaszeru May 19 '14 at 22:06
  • I get an error at runtime - your content must have a listview whose id is 'android.R.id.list' – Fearghal May 19 '14 at 22:51
  • Thats it - thanks a mill. My problem wasnt that my onclick handler was bad code, it was that i removed the 'android:' from the name and it started failing at runtime. Duh! Thanks again – Fearghal May 19 '14 at 22:58