我收到錯誤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();
}
}
您onItemClick被調用或不 –