由于代码问题,这将会很长。所以我有一个关于OnItemclickListener和OnclickListener的问题,这里我有两个代码,每个都有一个工作,所以是否有任何偏好使用其中之一或我可以使用任何人??? 这是使用OnItemClickListener
的代码public class MainActivity extends AppCompatActivity {
private ArrayList<String> numbers=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //a listview with id "list"
ListView numberLV = (ListView) findViewById(R.id.list);
numbers.add("one");
numbers.add("two");
numbers.add("three");
numbers.add("four");
// and so on
listAdapter itemadapter=new listAdapter(this,numbers);
numberLV.setAdapter(itemadapter);
numberLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String num=numbers.get(position);
String display="You have clicked "+num;
Toast.makeText(MainActivity.this,display,Toast.LENGTH_SHORT).show();
}
});
}
}
class listAdapter extends ArrayAdapter<String> {
public listAdapter(@NonNull Context context, ArrayList<String> numbers){
super(context,0,numbers);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_num, parent, false);
}
TextView tvnum=(TextView) listItemView.findViewById(R.id.numbersTV);
String num=getItem(position);
tvnum.setText(num);
return listItemView;
}
}
这是OnClickListener的一个
public class MainActivity extends AppCompatActivity {
private ArrayList<String> numbers=new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //a listview with id "list"
ListView numberLV = (ListView) findViewById(R.id.list);
numbers.add("one");
numbers.add("two");
numbers.add("three");
numbers.add("four");
// and so on
listAdapter itemadapter=new listAdapter(this,numbers);
numberLV.setAdapter(itemadapter);
}
}
class listAdapter extends ArrayAdapter<String> {
public listAdapter(@NonNull Context context, ArrayList<String> numbers){
super(context,0,numbers);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_num, parent, false);
}
TextView tvnum=(TextView) listItemView.findViewById(R.id.numbersTV);
String num=getItem(position);
tvnum.setText(num);
listItemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String display="You have clicked "+getItem(position);
Toast.makeText(getContext(),display,Toast.LENGTH_SHORT).show();
}
});
return listItemView;
}
}
这两个代码对我来说都是正常的,但是混淆的是第二个代码是对还是错?
答案 0 :(得分:0)
如上面第一条评论所述,setOnItemClickListener
是在适配器项目上附加点击侦听器的首选方式。
在第二个场景中,setOnClickListener
方法中的单个项目附加了getView
,这也是有效的,因为在这种情况下,ItemView是间接子类 setOnClickListener
最初声明的View
。
答案 1 :(得分:0)
我认为setOnItemClickListener()比onClickListener()更好,因为,