Android ListView onItemClick事件不起作用

时间:2014-08-12 22:50:44

标签: android android-listview onclick android-arrayadapter

当OnClick evet发生时,我试图将一些被阻止的短信显示为列表。我从SharedPreferences中获取要显示的内容并将它们放在一个数组中。然后我使用适配器来显示它们,但我收到错误:

ArrayAdapter<SMS> adapter = new ArrayAdapter<SMS>(this,android.R.layout.simple_list_item_1, smses);

lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

String item = ((TextView)view).getText().toString();

我按照http://www.ezzylearning.com/tutorial/handling-android-listview-onitemclick-event此网站上的教程进行了操作。我找不到问题所在,也没有错误通道的建议。

Button button = (Button)findViewById(R.id.showBlockedButton);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View view)
        {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            int total_blocked = preferences.getInt("blocked_size", 0);
            SMS[] smses = new SMS[total_blocked];
            for(int i = 0; i < total_blocked ;i++)
            {
                SMS sms = new SMS(preferences.getString("block_num_"+i, null),preferences.getString("block_body_"+i, null));
                sms.id = i;
                smses[i] = sms;
            }

            ListView lv = (ListView) findViewById(R.id.blockedSMSList);

            ArrayAdapter<SMS> adapter = new ArrayAdapter<SMS>(this,android.R.layout.simple_list_item_1, smses);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

                    String item = ((TextView)view).getText().toString();

                    Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();

                }
            });
        }
    });

很抱歉,如果我的解释不好。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试更改此行

ArrayAdapter < SMS > adapter = new ArrayAdapter < SMS > (this, android.R.layout.simple_list_item_1, smses);
中的

ArrayAdapter < SMS > adapter = new ArrayAdapter < SMS > (MyActivity.this, android.R.layout.simple_list_item_1, smses);