我的代码只显示列表View中的人员姓名。我想在列表视图中显示数字。
活动
public class HomeActivity extends Activity {
List<String> names;
//List<integer> time;
List<Integer> number;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// textView = (TextView) findViewById(R.id.listView);
getCallDetails();
}
private void getCallDetails() {
names = new ArrayList<String>();
number = new ArrayList<Integer>();
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, strOrder);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
//sb.append("Call Log :");
while (managedCursor.moveToNext()) {
String namee = managedCursor.getString(name);
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
String callType = null;
int callcode = Integer.parseInt(callTypeCode);
names.add(namee);
// number.add(phNum);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
// adaptorname adaptor = new adaptorname(this, names);
//adaptorname adaptor = new adaptorname(this, names);
/* adaptorname adaptor = new SimpleCursorAdapter(this, R.layout.cell, managedCursor,
adaptorname adaptor= new adaptorname[]{this,names,number}, new int[]{
R.id.lblName, R.id.lblnum});*/
adaptorname adaptor = new adaptorname(this, names);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adaptor);
}
}
}
这是我正在使用的自定义适配器
适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
*
*/
public class adaptorname extends BaseAdapter {
List<String> name;
//my addition
List<String> numbers;
Context context;
adaptorname(Context context, List<String> name,List<String> numbers){
this.name = name;
this.numbers=numbers;
this.context = context;
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int position) {
return name.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// // Get the data item for this position
// CellName cellNsme = new CellName();
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.cell, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.lblName);
//my addition
viewHolder.number=(TextView) convertView.findViewById(R.id.lblnum);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.name.setText(name.get(position));
//my addition
//viewHolder.number.setText(numbers.get(position));
// Return the completed view to render on screen
return convertView;
}
public class ViewHolder{
TextView name;
TextView number;
}
}
这是列表视图布局
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.moni.friendrecco.Call_Reports">
<
ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
这是我的行的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="130dp"
android:background="#5ffff7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lblName"
android:id="@+id/lblName"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lblnum"
android:id="@+id/lblnum"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/lblName"
android:layout_alignEnd="@+id/lblName"
android:layout_below="@+id/lblName" />
</RelativeLayout>
答案 0 :(得分:0)
您可以创建自己的对象列表:
<强> Contact.java 强>:
public class Contact {
private String name;
private String number;
public Contact(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
<强> row_layout.xml:强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<强> ContactListAdapter.java:强>
public class ContactListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Contact> mContactList;
public ContactListAdapter(Activity activity, List<Contact> mContactList) {
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
this.mContactList = mContactList;
}
@Override
public int getCount() {
return mContactList.size();
}
@Override
public Object getItem(int position) {
return mContactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
// Get item_layout:
rowView = mInflater.inflate(R.layout.row_layout, null);
// Get TextView from row_layout:
TextView textName =
(TextView) rowView.findViewById(R.id.name);
TextView textNumber =
(TextView) rowView.findViewById(R.id.number);
// Get name and number from current position:
Contact myContact = mContactList.get(position);
textName.setText(myContact.getName());
textNumber.setText(myContact.getNumber());
return rowView;
}
}
最后 MainActivity.java :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Contact> contactList = new ArrayList<>();
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
null, null, strOrder);
int name = managedCursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
//sb.append("Call Log :");
while (managedCursor.moveToNext()) {
String namee = managedCursor.getString(name);
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
String callType = null;
int callcode = Integer.parseInt(callTypeCode);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
contactList.add(new Contact(namee, phNum));
}
// Add list to your custom adapter
ListView myListView = (ListView) findViewById(R.id.liste);
ContactListAdapter mAdapter = new ContactListAdapter(this, contactList);
// Set Adapter
myListView.setAdapter(mAdapter);
}
}
<强> activity_main.xml中:强>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/liste"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</RelativeLayout>