嗨,我是一名新的android开发人员。点击后,我尝试更改项目中的某些元素,这是我的代码
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ImageView img = findViewById(R.id.imageView2);
img.setImageResource(R.drawable.validate);
TextView txtNumTicket, txtCaisse;
txtNumTicket = findViewById(R.id.ref);
txtCaisse = findViewById(R.id.textView);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.bon));
Toast.makeText(getApplicationContext(), "You have selected item no."
+ (i + 1) + "", Toast.LENGTH_SHORT).show();
}
});
当位置等于6时,我想返回初始状态(将图像更改为R.id.novalidate和彩色textview),该怎么办?
答案 0 :(得分:0)
评论后更新:
if (yourListView.getAdapter().getCount() == 6) {
img.setImageResource(R.drawable.novalidate);
txtNumTicket.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
txtCaisse.setTextColor(ContextCompat.getColor(Commande.this, R.color.your_new_colour));
}
第二次更新: 根据{{3}},您可以按照以下方法在列表的特定位置获取视图
public View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition ) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
因此,您可以通过调用以下内容获得所需的视图:
View view = getViewByPosition (number_of_the_view_you_want_to_change, name_of_your_listview);
然后绑定该项目的文本视图/图像视图(view.findViewById(...))并如上所述设置颜色和文本