0

我有一个填充列表视图的活动。我在活动的 onCreate 方法中创建了列表视图。现在我想为列表实现一个 onclick 列表器。如何做到这一点?

这是我的列表视图活动课程:

public class PollStationActivity extends Activity {



    static String response_str=null;
    static String response_code=null;

    String ac_code;
    String ac_id;
    String ac_lat;
    String ac_lon;
    String ac_name;

    // Hashmap for ListView
            ArrayList<HashMap<String, String>> PSList = new ArrayList<HashMap<String, String>>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // no more this
         setContentView(R.layout.activity_poll_station);

         //sendPostRequest();

         //Log.i("I am here","1");

            final String status_code_from_prev= getIntent().getExtras().getString("status");

            //Log.i("ss",status_code_from_prev);

            ArrayList<PSItem> PSList = new ArrayList<PSItem>();


            DatabaseHelper db = new DatabaseHelper(PollStationActivity.this);
            ContentValues values = new ContentValues();

             try {
              db.createDataBase();

              PSList = db.select(status_code_from_prev); 

              //Log.i("Count : ", " " + PSList.isEmpty());

             } catch (IOException e) {
              e.printStackTrace();
               }

             db.close();


         String[] fromColumns = {"name"};
            int[] toViews = {R.id.list_label_name}; // The TextView in simple_list_item_1

            ListView listView1 = (ListView) findViewById(R.id.poll_list_listView);  
            List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

            Iterator<PSItem> i = PSList.iterator();

            while(i.hasNext())
            {
                HashMap<String, String> map = new HashMap<String, String>();
                PSItem objPSItem = i.next();

                map.put("name", objPSItem.PS_NAME);
                map.put("ps_Id", objPSItem.PS_ID);
                fillMaps.add(map);
            }





            // fill in the grid_item layout
            SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_layout, fromColumns, toViews);
            listView1.setAdapter(adapter);

    }
}

我应该把listview onclick listner放在哪里?

4

3 回答 3

1

如果您使用 ListView,那么您应该使用 OnItemClickListener。在您分配适配器后,我将添加侦听器。

我更喜欢使用以下解决方案来处理点击事件。

public class StartActivity extends Activity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_start);

        // Create adapter for the list view

        ListView lw = (ListView) findViewById(R.id.WordsListView);
        lw.setAdapter(adapter);
        lw.setOnItemClickListener(this);    
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    }
}
于 2013-02-17T13:48:22.490 回答
1

您可以在初始化后立即设置侦听器,例如:

ListView listView1 = (ListView) findViewById(R.id.poll_list_listView); 
    listView1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            //Handle the click
        }           
    });

即使您还没有为列表设置适配器,这意味着没有显示任何项目,只要您已经通过findViewById

于 2013-02-17T13:56:43.570 回答
0

您必须使用 AdapterView.OnItemClickListener。

http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

单击项目时将调用此处理程序,并为您提供单击项目的位置。

于 2013-02-17T13:48:40.667 回答