使用自定义适配器填充列表视图的组件时获取空行

Getting empty rows while filling components of a listview using customadapter

我正在尝试从我的 phone 访问所有 missed calls 的详细信息列表。 我只是想访问 first 50 个未接来电。

问题是当 listview 中的某些行已填充而某些行为空时。

这是它的样子,即空白行。

这是我试图访问通话详细信息的 java 文件。

public class Second extends AppCompatActivity {
    TextView call;
    ListView l;
    int count = 0, k = 0;
    static int flag = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            StringBuffer sb = new StringBuffer();
            Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
                    null, null, null);
            int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
            int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
            int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
            int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
            int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
            int count = 0;
            int dircode = 0;
            String[] array = new String[50];
            int count1 = 0, count2 = 0, count3 = 0, param = 0;
int len=0;
            while (managedCursor.moveToNext()  &&len<50) {
                param = 0;
                String phNumber = managedCursor.getString(number);
                String namee = managedCursor.getString(name);
                String callType = managedCursor.getString(type);
                String callDate = managedCursor.getString(date);
                Date callDayTime = new Date(Long.valueOf(callDate));
                String callDuration = managedCursor.getString(duration);
                String dir = null;
                if (flag == 1) {
                    if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
                        dir = "MISSED";
                        sb.append("\nName: " + namee + "\nPhone Number:  " + phNumber + " \nCall Type:  " + dir + " \nCall Date:   " + callDayTime
                                + " \nCall duration in sec :   " + callDuration);
                    }
                    param = 1;
                }
                if (flag == 2) {
                    if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE) {
                        dir = "RECEIVED";
                        sb.append("\nName: " + namee + "\nPhone Number:  " + phNumber + " \nCall Type:  " + dir + " \nCall Date:   " + callDayTime
                                + " \nCall duration in sec :   " + callDuration);
                    }
                    param = 1;
                }
                if (flag == 3) {
                    Log.d("asd", "inside flag3");
                    if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE) {
                        dir = "DIALLED";
                        sb.append("\nName: " + namee + "\nPhone Number:  " + phNumber + " \nCall Type:  " + dir + " \nCall Date:   "
                                + callDayTime
                                + " \nCall duration in sec :   " + callDuration);
                    }
                    param = 1;

                }
                if (flag == 4) {
                    if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE)
                        count1++;
                    if (Integer.parseInt(callType) == CallLog.Calls.INCOMING_TYPE)
                        count2++;
                    if (Integer.parseInt(callType) == CallLog.Calls.OUTGOING_TYPE)
                        count3++;
                    continue;
                }
                if (param == 1) {
                    array[k] = new String(sb);
                    k++;len++;

                }
                sb.setLength(0);//flushing the buffer
            }
            if (flag == 4) {
                sb.append("Missed Type : " + count1 + "\nReceived type : " + count2 + "\nDialled type: " + count3);
                array[k] = String.valueOf(sb);
                k++;
            }

            l = (ListView) findViewById(R.id.listView);//creating listview
            for (int i = 0; i < array.length; i++) {
                if(array[i]==null)
                   Log.d("asd","NULLLLLL "+ i);
                else
                     Log.d("asd","NOT NULL "+i);
            }
            l.setAdapter(new CustomAdapter(this, array));
            managedCursor.close();
        }catch (SecurityException e){}
    }
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
    private static LayoutInflater inflater = null;
    String[] your_array_list;
    public CustomAdapter(Context context,String [] your_array_list)
    {
this.your_array_list=your_array_list;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return your_array_list.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView;
        rowView = inflater.inflate(R.layout.each_row, null);
            TextView textView= (TextView) rowView.findViewById(R.id.textview);
            textView.setText(your_array_list[position]);
            return rowView;
    }
}

为了验证所有 50 项都不为空,我使用此代码检查了是否其中一些为空---

 for (int i = 0; i < array.length; i++) {
            if(array[i]==null)
               Log.d("asd","NULLLLLL "+ i);
            else
                 Log.d("asd","NOT NULL "+i);
        }

但是所有 50 次迭代都在打印 NOT NULL

那么,如果字符串array中的字符串不为空,那么为什么listview中的某些行是空白的?

有人可以帮我解决这个问题吗?

我可以看到一个 flag 变量初始化为 0 但从来没有 incremented/decremented。

这个flag负责将数据存入StringBuffer

数组中所有索引都不是 null 的原因是因为您正在使用传递给构造函数的 StringBuffer 初始化每个索引。

所以请修复代码中的 flag 变量。这应该可以解决您的问题。您还可以使用这段代码查看每个索引中存储的值是多少。

for(int i = 0; i < 50; i++){
   Log.d(LOG_TAG, "data at " + i + ": " + array[i])
}

用合适的值替换LOG_TAG

您应该将 "param=1" 语句移动到内部 if 语句中。 问题是什么。例如,标志==1。但是调用类型不是MISSED_TYPE:

...
if (flag == 1) {
  if (Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
    dir = "MISSED";
    sb.append("\nName: " + namee + "\nPhone Number:  " + phNumber + " \nCall Type:  " + dir + " \nCall Date:   " + callDayTime
      + " \nCall duration in sec :   " + callDuration);
  }
  param = 1;
}
...

因此,您的 sb 将是空的,但是您将 param 设置为 1,并且这个空的 sb 将被添加到数组中。您应该将所有 if 语句替换为:

...
if (flag == 1 && Integer.parseInt(callType) == CallLog.Calls.MISSED_TYPE) {
  dir = "MISSED";
  sb.append("\nName: " + namee + "\nPhone Number:  " + phNumber + " \nCall Type:  " + dir + " \nCall Date:   " + callDayTime
    + " \nCall duration in sec :   " + callDuration);
  param = 1;
}
...