first i\'m fetching all messages to my app to be store in listview then I\'m selecting messages from listview using checkboxes and wish to send these se
May I know why you want to select multiple rows. I mean what exact action you want to perform by selecting multiple rows?
Here is the updated code for you. Please let me know in case you didn't get any code:
---------------
package com.example.multiselectlist;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{
Button btnDelete;
ListView listViewSMS;
Cursor cursor;
SMSListAdapter smsListAdapter;
Context context;
ArrayAdapter adapter;
List list = new ArrayList();
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
listViewSMS=(ListView)findViewById(R.id.lvSMS);
btnDelete = (Button)findViewById(R.id.buttonDelete);
cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
smsListAdapter = new SMSListAdapter(this,getModel());
listViewSMS.setAdapter(smsListAdapter);
listViewSMS.setOnItemClickListener(this);
btnDelete.setOnClickListener(this);
}
@Override
public void onItemClick(AdapterView> arg0, View v, int arg2, long arg3) {
TextView label = (TextView) v.getTag(R.id.tvSMSSend);
CheckBox checkbox = (CheckBox) v.getTag(R.id.cbSelect);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
}
private List getModel() {
if(cursor.getCount()>0){
for(int i=0;i0){
for(int i=0;i0){
for(int i=0;i
SMSListModel
class:
package com.example.multiselectlist;
public class SMSListModel {
private String address;
String body;
private boolean selected;
public SMSListModel(String address, String body) {
this.address = address;
this.body = body;
}
public String getAddress() {
return address;
}
public String getBody() {
return body;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
SMSListAdapter
class:
package com.example.multiselectlist;
import java.util.List;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class SMSListAdapter extends ArrayAdapter {
private final List list;
private final Activity mContext;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public SMSListAdapter(Activity context,List list)
{
super(context, R.layout.listview_each_item, list);
mContext = context;
this.list = list;
}
static class ViewHolder {
protected TextView textAddress;
protected TextView textBody;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = mContext.getLayoutInflater();
convertView = inflator.inflate(R.layout.listview_each_item, null);
viewHolder = new ViewHolder();
viewHolder.textAddress = (TextView) convertView.findViewById(R.id.tvSMSSend);
viewHolder.textBody = (TextView) convertView.findViewById(R.id.tvSMSBody);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.cbSelect);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.tvSMSSend, viewHolder.textAddress);
convertView.setTag(R.id.tvSMSBody, viewHolder.textBody);
convertView.setTag(R.id.cbSelect, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.textAddress.setText(list.get(position).getAddress());
viewHolder.textBody.setText(list.get(position).getBody());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
xml
: