import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
public class Main extends ListActivity implements OnClickListener {
final String [] MyName = new String [1000];
ArrayList<String> myArr = new ArrayList<String>();
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()){
int NameIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String Name = people.getString(NameIndex);
MyName[i] = Name.toString();
myArr.add(Name.toString());
i++;
}
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.textView1,
myArr.toArray()));
}
class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource,
int textViewResourceId, Object[] objects) {
super(context, resource, textViewResourceId, (String[]) objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_view, parent,false);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(myArr.get(position).toString());
return row;
}
}
您好我正在尝试在电话目录中实现包含联系人姓名的List myArr 。大小随联系人数量而变化。完成列表创建并添加所有联系人后,我将列表 myArr 传递给seListAdapter函数。此代码中遇到的问题是它在模拟器中启动时显示错误。如何将整个列表myArr传递给方法,然后在inflater textview中单独显示每个名称 我搜索了myArr.functions,发现myArr.get()只返回一个元素
答案 0 :(得分:0)
将其转换为以下内容:
(String[]) myArr.toArray(new String[]{});