获取问题上设置列表视图项的颜色 [英] Getting issue to set the color on list view item
问题描述
我的列表具有项目2的TextView和一个imageview.I膨胀的列表ArrayAdapter.Everything工作正常,只是改变列表项色click.I有22个项目在我的列表视图。主要列表视图显示10个项目在屏幕上,并得到scroll.Now我的问题是,当我点击单个项目0-9(最初的10项)的项目适当改变它们的颜色上点击其他项目,但是当我滚动和点击上有位置的项目大于9我的活动crashes.I我指的 http://www.mail-archive.com/android-developers@googlegroups.com/msg09740.html 链接写code。在的环
。帮助我摆脱这种problem.Any建议或解决方案,将是非常美联社preciated.Thanx提前。
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
。getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); //隐藏虚拟键盘
的setContentView(R.layout.defect_pic_listview);
尝试{
适配器=新MyArrayAdapter(这一点,makeList());
setListAdapter(适配器);
adapter.notifyDataSetChanged();
。getListView()setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView()。setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
长ID){
// Toast.makeText(getApplicationContext(),项目+位置+点击
// Toast.LENGTH_SHORT).show();
的System.out.println(位置+位置);
。INT第一= getListView()getFirstVisiblePosition();
的System.out.println(第一=+先);
。INT最后= getListView()getLastVisiblePosition();
的System.out.println(最后=+即止);
INT总=最后 - 第一;
的System.out.println(总=+总);
如果(getListView()。isItemChecked(位置)){
的for(int i = 0; I< =最后,我++){
的System.out.println(我=+ I);
如果(第一+我= =位置){
。getListView()getChildAt(ⅰ).setBackgroundColor(Color.GREEN);
的System.out.println(L1 =+ getListView());
// l.getItemAtPosition(ⅰ);
//的System.out.println(升位置+ 1);
}
其他{
。getListView()getChildAt(ⅰ).setBackgroundColor(Color.TRANSPARENT);
的System.out.println(12 =+ getListView());
}
}
}
其他{
。getListView()getChildAt(位置 - 第一).setBackgroundColor(Color.TRANSPARENT);
}
}
});
}
赶上(例外五){
Log.d(错误,e.getMessage());
}
}
我给的答案我自己的问题。
下面是完美运行code: -
getListView()。setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
长ID){
// Toast.makeText(getApplicationContext(),项目+位置+点击
// Toast.LENGTH_SHORT).show();
的System.out.println(位置=+位置);
。INT第一= getListView()getFirstVisiblePosition();
的System.out.println(第一=+先);
。INT最后= getListView()getLastVisiblePosition();
的System.out.println(最后=+即止);
INT总=最后 - 第一;
的System.out.println(总=+总);
如果(getListView()。isItemChecked(位置)){
的for(int i =第一; I< =最后,我++){
的System.out.println(我=+ I);
如果(我==位置){
Log.w(TAG,我在If块);
。getListView()getChildAt(我先).setBackgroundColor(Color.GREEN);
的System.out.println(L1 =+ getListView());
// l.getItemAtPosition(ⅰ);
//的System.out.println(升位置+ 1);
}
其他{
。getListView()getChildAt(我先).setBackgroundColor(Color.TRANSPARENT);
的System.out.println(12 =+ getListView());
}
}
}
其他{
。getListView()getChildAt(位置).setBackgroundColor(Color.TRANSPARENT);
}
}
});
I have list with items having two textview and one imageview.I inflate the list with ArrayAdapter.Everything is working fine except changing list item color on click.I have 22 items in my listview. Primarily listview displaying 10 items on the screen and get the other items on scroll.Now my problem is when I clicks on a single item between 0-9(initial 10 items) item changes their color properly on click, but when i scroll and clicks on an item having position greater then 9(after the initial 10 items) my activity crashes.I am referring http://www.mail-archive.com/android-developers@googlegroups.com/msg09740.html link to write the code in for loop
.Help me to get rid of this problem.Any suggestions or solutions will be highly appreciated.Thanx in advance.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
setContentView(R.layout.defect_pic_listview);
try{
adapter = new MyArrayAdapter(this,makeList());
setListAdapter(adapter);
adapter.notifyDataSetChanged();
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
// Toast.LENGTH_SHORT).show();
System.out.println("position"+position);
int first = getListView().getFirstVisiblePosition();
System.out.println("first="+first);
int last = getListView().getLastVisiblePosition();
System.out.println("last="+last);
int total = last - first;
System.out.println("total="+total);
if(getListView().isItemChecked(position)){
for(int i = 0 ; i <= last ; i++){
System.out.println("i="+i);
if(first+i == position){
getListView().getChildAt(i).setBackgroundColor(Color.GREEN);
System.out.println("l1="+getListView());
// l.getItemAtPosition(i);
// System.out.println("l position"+l);
}
else{
getListView().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
System.out.println("l2="+getListView());
}
}
}
else{
getListView().getChildAt(position - first).setBackgroundColor(Color.TRANSPARENT);
}
}
});
}
catch(Exception e){
Log.d("error",e.getMessage());
}
}
I am giving the answer to my own question.
Here's the perfect running code:-
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
// Toast.LENGTH_SHORT).show();
System.out.println("position="+position);
int first = getListView().getFirstVisiblePosition();
System.out.println("first="+first);
int last = getListView().getLastVisiblePosition();
System.out.println("last="+last);
int total = last - first;
System.out.println("total="+total);
if(getListView().isItemChecked(position)){
for(int i = first ; i <= last ; i++){
System.out.println("i="+i);
if(i == position){
Log.w("TAG", "I am in If block");
getListView().getChildAt(i-first).setBackgroundColor(Color.GREEN);
System.out.println("l1="+getListView());
// l.getItemAtPosition(i);
// System.out.println("l position"+l);
}
else{
getListView().getChildAt(i-first).setBackgroundColor(Color.TRANSPARENT);
System.out.println("l2="+getListView());
}
}
}
else{
getListView().getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
}
}
});
这篇关于获取问题上设置列表视图项的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!