Reputation: 2897
I have an arrayadapter named SmsArrayAdapter having the code as following :
public class SmsArrayAdapter extends ArrayAdapter<String> {
List<String> smsBody;
List<Boolean> Status;
Context context;
private static LayoutInflater inflater = null;
String fromNumber;
public SmsArrayAdapter(Context context, int resource, List<String> smsBody,List<Boolean> Status,
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;
}
public String getStr(int position)
{
return smsBody.get(position);
}
@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;
}
@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(" SMS FROM " + 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);
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 want to get the item of this arrayadapter . For this I have the following code :
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String smsMessageStr = (String) arrayAdapter.getItem(pos);
Toast.makeText(this, smsMessageStr, Toast.LENGTH_SHORT).show();
}
But I can not get the right item . What can I do ? How can I avoid this error ?
Upvotes: 0
Views: 4641
Reputation: 6967
In adapter you can use getItem() to access items in list. Your array adapter holds string elements, getItem() returns a string object.
Upvotes: 1