ListView + 체크 박스
-
21-12-2019 - |
문제
Checkbox로 ListView를 수행하려고하지만 할 수는 없습니다. CKECKBOX가 선택되거나 선택되지 않은 경우 알려주는 청취자를 구현할 수있는 방법을 모르겠습니다.
누군가가 청취자의 구현이 어떻게되는지 알고 있고 나를 도울 수 있는지 알고있는 경우 여기에 내 코드를 남겨 둡니다.
이것은 목록의 각 항목의 코드입니다.
public class TemaRescatado {
protected String tema;
protected long id;
protected Boolean selected = false;
public TemaRescatado(String tema2, long id) {
super();
this.tema = tema2;
this.id = id;
}
public TemaRescatado(String tema2) {
super();
this.tema = tema2;
}
public String getTema() {
return tema;
}
public void setTema(String tema1) {
this.tema = tema1;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
.
이것은 어댑터의 코드입니다.
public class AdapterGenerales extends BaseAdapter {
protected Activity activity;
protected ArrayList<TemaRescatado> items;
public AdapterGenerales() {
// TODO Auto-generated constructor stub
}
public AdapterGenerales(Activity activity, ArrayList<TemaRescatado> items) {
this.activity = activity;
this.items = items;
}
public int getCount() {
return items.size();
}
public Object getItem(int arg0) {
return items.get(arg0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Generamos una convertView por motivos de eficiencia
View v = convertView;
//Asociamos el layout de la lista que hemos creado
if(convertView == null){
LayoutInflater inf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.item_gen, null);
}
// Creamos un objeto directivo
TemaRescatado temaResc = items.get(position);
//Rellenamos el listview de temas
TextView iden = (TextView) v.findViewById(R.id.textView1);
iden.setText(temaResc.getTema());
// Retornamos la vista
return v;
}
@Override
public long getItemId(int position) {
return items.get(position).getId();
}
}
.
이는 활동의 코드입니다.
public class SeleccionTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccion_test);
ListView lista = (ListView) findViewById(R.id.listView1);
final ArrayList<TemaRescatado> arraydirGen = new ArrayList<TemaRescatado>();
TemaRescatado temaRescGen;
temaRescGen = new TemaRescatado("General1",1);
arraydirGen.add(temaRescGen);
temaRescGen = new TemaRescatado("General2",2);
arraydirGen.add(temaRescGen);
temaRescGen = new TemaRescatado("General3",3);
arraydirGen.add(temaRescGen);
temaRescGen = new TemaRescatado("General4",4);
arraydirGen.add(temaRescGen);
temaRescGen = new TemaRescatado("General5",5);
arraydirGen.add(temaRescGen);
temaRescGen = new TemaRescatado("General6",6);
arraydirGen.add(temaRescGen);
final AdapterGenerales adapter = new AdapterGenerales(this, arraydirGen);
lista.setAdapter(adapter);
lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("Seleccionado - Listener", "pos: "+position +" id:"+id);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seleccion_test, menu);
return true;
}
}
.
item_gen.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="@string/vacia" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:text="@string/vacia" />
</RelativeLayout>
. 해결책
getView(...)
에서 compoundbutton.oncheckedchangelistener 시도 뷰백 보내기
코드 스 니펫
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox1);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
TemaRescatado element = (TemaRescatado) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
. 다른 팁
이 자습서에서 ListView 튜토리얼
또한 onClickListener 또는 OnCheckListener의 getView () 메서드에서
에 할당해야한다는 것을 기억하십시오.제휴하지 않습니다 StackOverflow