Reputation: 2897
I have an arrayadapter in android as follows :
public class SmsArrayAdapter extends ArrayAdapter<String> {
List<String> smsBody;
List<Boolean> Status;
List<String> time;
List<String> SmsMessageId;
Context context;
private static LayoutInflater inflater = null;
String fromNumber;
public SmsArrayAdapter(Context context, int resource, List<String> smsBody,
List<Boolean> Status, List<String> time, List<String> SmsMessageId,
String fromNumber) {
super(context, resource, smsBody);
this.smsBody = smsBody;
this.Status = Status;
this.context = context;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.fromNumber = fromNumber;
this.time = time;
this.SmsMessageId=SmsMessageId;
}
public String getStr(int position) {
return smsBody.get(position);
}
public String getId(int position)
{
return SmsMessageId.get(position);
}
public void setRead(int position,String smsMessageId)
{
Status.set(position, true);
ContentValues values = new ContentValues();
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" +smsMessageId, null);
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return smsBody.get(position);
}
public static class ViewHolder {
public TextView textfrom;
public TextView text_sms;
public TextView text_time;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
/****** Inflate tabitem.xml file for each row ( Defined below ) *******/
convertView = inflater.inflate(R.layout.row_item, null);
/****** View Holder Object to contain tabitem.xml file elements ******/
holder = new ViewHolder();
holder.textfrom = (TextView) convertView
.findViewById(R.id.textView_from);
holder.textfrom.setText(" " + fromNumber);
holder.text_sms = (TextView) convertView
.findViewById(R.id.textView_sms);
String smsTextToDisplay = smsBody.get(position);
if (smsTextToDisplay.length() > 100)
smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";
holder.text_sms.setText(smsTextToDisplay);
holder.text_time = (TextView) convertView
.findViewById(R.id.textView_time);
holder.text_time.setText(time.get(position));
if (Status.get(position) == false) {
convertView.setBackgroundColor(context.getResources().getColor(
R.color.light_blue_overlay));
}
/************ Set holder with LayoutInflater ************/
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
return convertView;
}
}
I have initialized this arrayadapter as follows :
arrayAdapter = new SmsArrayAdapter(this, R.layout.row_item, smsBody,
status, time, new ArrayList<String>(), fromNumber);
smsListView.setAdapter(arrayAdapter);
I want to access item of list as follows :
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String smsMessageStr = (String) arrayAdapter.getItem(pos);
String smsMessageId = ((SmsArrayAdapter) arrayAdapter).getId(pos);
((SmsArrayAdapter) arrayAdapter).setRead(pos,smsMessageId);
Intent intent = new Intent(SmsActivity.this,
ShowIndividualSMS.class);
intent.putExtra("SMS", smsMessageStr);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this,"exception is "+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
But I get the exception : "invalid index 1 , size is 0" . For this exception , I have understood that the SmsMessageId list is empty . Why ? How can I solve this error ?
Upvotes: 0
Views: 1813
Reputation: 1399
public String getId(int position)
{
if(SmsMessageId !=null && SmsMessageId.size() > 0)
return SmsMessageId.get(position);
}
Upvotes: 1
Reputation: 325
When you initialize your adapter, you give it new ArrayList<String>()
as its SmsMessageID. This ArrayList is empty. Somewhere between when you initialize it and when you try to access that first element of SmsMessageID you have to populate that array with some elements.
You could also, if you wanted to populate that array later, modify getID so that if the element it was trying to get didn't exist, it returned some falsey value. This would be good practice.
I don't have a good idea of what the program is trying to do, not having comments, so I'm sorry I can't be more helpful.
Upvotes: 1
Reputation: 2727
List<String> SmsMessageId
is initialized in constructor [adapter] with empty list new ArrayList<String>()
Upvotes: 1