自定义列表视图 - 我在哪里设置我的点击处理程序

时间:2014-05-19 21:42:30

标签: android listview onitemclick

我有一个带有listview适配器类的自定义ltView,我有一个代码存根,它填充了我的lsit视图的4行。 xmls等看起来很好,活动自己构建好但我似乎无法插入处理程序而不会导致错误。

这是我到目前为止的代码。

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

填充列表

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

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

1 个答案:

答案 0 :(得分:2)

listview的点击处理程序可以这样工作:

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

并在您的xml布局中,不要忘记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"
                />