I was trying to populate my listview with data from sqlite in onclicklistener of my load button.
However, I\'m a little confused from the examples I have seen. here
Use a CustomAdapter
public class Display extends Activity implements OnItemClickListener{
DatabaseManager db;
List<PlayerData> contacts;
ArrayList<String> con= new ArrayList<String>();
LayoutInflater mInflater;
CheckBox cb;
TextView tv1;
CustomAdapter cus;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
ListView lv= (ListView) findViewById(R.id.lv);
db = new DatabaseManager(this);
contacts = db.getAllContacts();
cus = new CustomAdapter();
lv.setAdapter(cus);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < contacts.size(); i++)
{
if(cus.mCheckStates.get(i)==true)
{
sb.append(contacts.get(i).getPhoneNumber());
sb.append("\n");
}
}
Toast.makeText(getApplicationContext(), sb,
Toast.LENGTH_LONG).show();
Intent intent = new Intent(Display.this,SendMessage.class);
if(con!=null)
{
intent.putStringArrayListExtra("list",con);
startActivity(intent);
}
}
});
}
public void onItemClick(AdapterView parent, View view, int
position, long id) {
cus.toggle(position);
}
class CustomAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{
private SparseBooleanArray mCheckStates;
CustomAdapter()
{
mCheckStates = new SparseBooleanArray(contacts.size());
mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.rowlayout, null);
TextView tv= (TextView) vi.findViewById(R.id.textView1);
tv1= (TextView) vi.findViewById(R.id.textView2);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ contacts.get(position).getName());
tv1.setText("Phone No :"+ contacts.get(position).getPhoneNumber());
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
notifyDataSetChanged();
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
listview.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"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
rowlayout.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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="34dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignLeft="@+id/textView1"
android:text="TextView" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginRight="33dp"
android:text="@string/choice" />
</RelativeLayout>
Each row in listview will look like below. You can click the check box to select and unselect. Later you can get the selected item and do whatever is required.
Click the button at the bottom after checking the checkbox.