-
-
Save Dzakus/106ba25b9888d125e106 to your computer and use it in GitHub Desktop.
ListView with checkbox Przykład dla: Promenel http://forum.android.com.pl/f829/wa-asna-lista-i-metoda-onitemclicklistener-nie-dziaa-a-383034/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="48dp" > | |
<CheckBox | |
android:id="@+id/checkBox1" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:layout_centerVertical="true" | |
android:clickable="false" | |
android:focusable="false" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapka; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.app.AlertDialog; | |
import android.app.ListActivity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.CheckBox; | |
import android.widget.ListView; | |
public class MainActivity extends ListActivity { | |
private MyAdapter mAdater; | |
private static class RowData { | |
public RowData(String text, boolean status) { | |
this.text = text; | |
this.status = status; | |
} | |
String text; | |
boolean status; | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ArrayList<RowData> list = new ArrayList<RowData>(); | |
list.add(new RowData("Tekst", true)); | |
list.add(new RowData("Tekst fałsz", false)); | |
list.add(new RowData("Tekst random", System.currentTimeMillis() % 2 == 0)); | |
mAdater = new MyAdapter(this, list); | |
setListAdapter(mAdater); | |
} | |
@Override | |
protected void onListItemClick(ListView l, View v, int position, long id) { | |
RowData data = (RowData) mAdater.getItem(position); | |
data.status = !data.status; | |
mAdater.notifyDataSetChanged(); | |
String msg = String.format("Kliknieto \"%s\" na pozycji %d, status %b", data.text, position, data.status); | |
new AlertDialog.Builder(this).setMessage(msg).show(); | |
} | |
// TODO ArrayAdapter? Dlaczego wcześniej nie zrobiłem tego na Arrayu? Nie wiem. Jakieś zaćmienie. | |
public class MyAdapter extends BaseAdapter { | |
private LayoutInflater mInflater; | |
private List<RowData> mData; | |
public MyAdapter(Context context, List<RowData> mData) { | |
this.mInflater = LayoutInflater.from(context); | |
this.mData = mData; | |
} | |
@Override | |
public int getCount() { | |
return mData != null ? mData.size() : 0; | |
} | |
@Override | |
public RowData getItem(int position) { | |
return mData.get(position); | |
} | |
@Override | |
public long getItemId(int arg0) { | |
// Return _ID for db | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (convertView == null) { | |
// TODO View Holder? | |
convertView = mInflater.inflate(R.layout.checkbox_list_row, parent, false); | |
} | |
RowData data = getItem(position); | |
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1); | |
checkbox.setChecked(data.status); | |
checkbox.setText(data.text); | |
return convertView; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment