2017-03-10 47 views
0

我收到錯誤l.setOnClickListener(this);它說,在adapterview東西setOnclicklistner不能應用等安卓:setonclicklistener在適配器視圖不能應用於

import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Adapter; 
    import android.widget.AdapterView; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 
    ListView l; 
    String[] days={"Sunday","monday","tuesday","wedensday","thursday","friday","saturday"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     l=(ListView)findViewById(R.id.listView); 
     ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,days); 
     l.setAdapter(adapter); 
     l.setOnClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     TextView temp=(TextView)view; 
     Toast.makeText(this,""+temp.getText()+" "+position,Toast.LENGTH_LONG).show(); 

    } 
} 
+0

您onItemClick被調用或不 –

回答

1

您需要使用setOnItemClickListener,而不是setOnClickListener

l.setOnItemClickListener(this); 
+1

由於它的工作 –

0

試試這個,

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

     } 
    }); 

OR

l.setOnItemClickListener(this); 
+0

它的工作,但ü可以解釋爲什麼setOnClickListener didntl工作,但setOnItemClickListener的工作,是不是一樣 –

相關問題