배열 목록이 있었고 어댑터를 통해 내 목록을 채 웁니다.
사실, 클릭 한 항목에 약간의 색을 지정하고 싶습니다.
첫 번째 항목을 클릭 할 때마다 첫 번째 항목과 마지막 항목 모두 동일한 배경색을 얻는 것이 문제입니다.
목록 어댑터 첫 번째 및 마지막 ID 충돌
코드 : Test.java
//To keep track of previously clicked textview
TextView last_clicked=null;
ListView lv=(ListView)findViewById(R.id.my_activity_list);
//My test array
String[] data={"one","two","three","four","five","six"};
list=new ArrayList<String>(data.length);
list.addAll(Arrays.asList(data));
//evolist is my custom layout
adapter= new ArrayAdapter<String>(c,R.layout.evolist,list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> ad, View v, int pos, long id){
//set black for other
if(last_clicked!=null)
last_clicked.setBackgroundColor(Color.BLACK);
//set red color for selected item
TextView tv=(TextView)v;
//I also tried TextView tv=(TextView)v.findViewById(R.id.tvo)
//I tried printing tv.getId() and noticed that both first and last had same IDs
tv.setBackgroundColor(Color.RED);
last_clicked=tv;
}
});
레이아웃 : 내가 잘못, 또는 내가 오류의 종류를 얻을에만 evolist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/white"
android:padding="10dp"
android:textSize="20sp">
</TextView>
? (삼성 갤럭시 Y 듀오, 2.3.7)을 views
는 당신이 스크롤 할 때, 상단에서 사라 View
이 항목에 사용되는 하나입니다 (ListView
재활용되기 때문에 내가 무슨 일이 일어나고 생각하는 것은
나는 그 일이 왜 일어나는 지 알지 못하지만, 단지 id 때문일 경우, tv.setId() 메소드를 사용하여 외부 적으로 ID를 제공하려고 시도합니다. 그게 작동 할 수도 있습니다 –
@shreya 샤 :이 스 니펫과 코멘트를 시도해 볼 수 있습니까? – everlasto