item没有出现在android的列表中

时间:2015-10-05 06:34:01

标签: android listview

我有一个SMSItem对象的列表视图。当任何新的短信到达时,它会显示在列表中。但点击列表后,该项目正在消失。该项目未显示在列表中。

我的arrayadapter如下:

public class SmsArrayAdapter extends ArrayAdapter<SMSItem> {

    List<SMSItem> smsBody;
    Context context;
    private static LayoutInflater inflater = null;
    String fromNumber;

    public SmsArrayAdapter(Context context, int resource,
            List<SMSItem> smsBody, String fromNumber) {
        super(context, resource, smsBody);
        this.smsBody = smsBody;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.fromNumber = fromNumber;
    }

    public SMSItem getStr(int position) {
        return smsBody.get(position);
    }

    public void setRead(int position, String smsMessageId) {
        smsBody.get(position).status = true;
        SMSItem smsItem = smsBody.get(position);
        smsItem.status = true;
        //smsBody.set(position, smsItem);
        ContentValues values = new ContentValues();
        values.put("read",true);
        int flag = context.getContentResolver().update(Uri.parse("content://sms/inbox"),
                values, "_id=" + smsMessageId, null);
        Toast.makeText(context, "The result is "+flag, Toast.LENGTH_LONG).show();  

    /*  Uri uri = Uri.parse("content://sms/inbox");
        String selection = "address = ? AND body = ? AND read = ?";
        String from = "03590000004";
        String body =smsItem.sms;
        String[] selectionArgs = {from, body, "0"};

        ContentValues values = new ContentValues();
        values.put("read", true);

        int flag = context.getContentResolver().update(uri, values, selection, selectionArgs);
        Toast.makeText(context, "The result is "+flag, Toast.LENGTH_LONG).show(); */

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return super.getCount();
    }

    @Override
    public SMSItem 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) {
    //  Toast.makeText(context, "The index is "+position, Toast.LENGTH_LONG).show();
        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.text_sms = (TextView) convertView
                    .findViewById(R.id.textView_sms);
            holder.text_time = (TextView) convertView
                    .findViewById(R.id.textView_time);

            /************ Set holder with LayoutInflater ************/
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textfrom.setText(" " + fromNumber);
        SMSItem smsItem = smsBody.get(position);
        String smsTextToDisplay = smsItem.sms;
        if (smsTextToDisplay.length() > 100)
            smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

        holder.text_sms.setText(smsTextToDisplay);

        holder.text_time.setText(smsItem.time);
        if (smsItem.status == false) {
            convertView.setBackgroundColor(context.getResources().getColor(
                    R.color.light_blue_overlay));
        }
        else
        {
            convertView.setBackgroundColor(Color.WHITE);    
        }

        return convertView;
    }

}

问题出在哪里?我怎么解决这个问题?非常感谢任何形式的帮助。

我的onclick代码是:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        try {

             View v = smsListView.getChildAt(pos  - 
                      smsListView.getFirstVisiblePosition());
             v.setBackgroundColor(Color.WHITE);

            SMSItem smsMessageStr = (SMSItem) arrayAdapter.getItem(pos);
            if (smsMessageStr.status==false) {

                // String smsMessageId = ((SmsArrayAdapter)
                // arrayAdapter).getId(pos);
                ((SmsArrayAdapter) arrayAdapter).setRead(pos, smsMessageStr.ID); 
                Toast.makeText(this, "ID is " + smsMessageStr.ID, Toast.LENGTH_LONG)
                        .show();

                arrayAdapter.notifyDataSetChanged();
            }

            setBadge();

            Intent intent = new Intent(SmsActivity.this,
                    ShowIndividualSMS.class);
            intent.putExtra("SMS", smsMessageStr.sms);
            startActivity(intent);

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "exception is " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e("TAG", e.getMessage());
        }
    }

1 个答案:

答案 0 :(得分:0)

  

但点击列表后,该项目正在消失

问题在于ListView的onItemClickListener(...)没有适配器。