【发布时间】: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