簡體   English   中英

如何從arrayadapter獲取項目

[英]How to get Item from arrayadapter

我有一個名為SmsArrayAdapter的arrayadapter,其代碼如下:

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;
    }

}

我想獲得這個arrayadapter的項目。 為此,我有以下代碼:

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();

}

但是我找不到合適的物品。 我能做什么 ? 如何避免此錯誤?

在適配器中,可以使用getItem()訪問列表中的項目。 您的數組適配器包含字符串元素,getItem()返回一個字符串對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM