【问题标题】:Getting empty rows while filling components of a listview using customadapter使用 customadapter 填充列表视图的组件时获取空行
【发布时间】:2016-07-26 13:29:08
【问题描述】:

我正在尝试通过我的手机访问所有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中的某些行是空白的呢?

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

【问题讨论】:

  • 几个月前我做了同样的应用程序。但我所做的是使用CursorLoaded 代替,因为它创建了一个新线程并在该线程上执行它而不会冻结 UI。您是否尝试调试此代码?如果是,请发布您的发现。
  • @Saran Sankaran,现在我添加了快照。可能现在我的问题很清楚了。
  • 快照太小,什么都看不懂。也请尝试调试应用程序。
  • 你的意思是说我应该在android studio的debug console中显示输出。或者我应该将Log 语句放入我的代码中以查看执行流程?
  • @Saran Sankaran ,增加了快照的大小,现在它是可见的。

标签: java android listview custom-adapter


【解决方案1】:

您应该将“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;
}
...

【讨论】:

    【解决方案2】:

    我可以看到 flag 变量初始化为 0,但从未递增/递减。

    这个flag负责将数据存入StringBuffer

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

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

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

    用合适的值替换LOG_TAG

    【讨论】:

    • @Saran Sankaran , flag 在这里是一个静态变量,在本次活动开始之前的前一个活动中,其值设置为 1 或 2 或 3 或 4。这就是为什么当本次活动开始时 flag 值不是0. 如果我错了,请纠正我。
    • @a874 您是否尝试添加该 for 循环来检查数组每个索引处的数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2014-01-05
    相关资源
    最近更新 更多