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;
}
}
Hello I am trying to implement a List myArr containing contact names in the phone directory. The size varies along with the number of contacts. after finishing the list creation and adding all contacts i passed the list myArr to the seListAdapter function. The problem faced in this code is that it is showing an error on launch in the emulator. how can i pass the entire list myArr to the method and then display each name separately in the inflater textview i have searched for the myArr.functions and only found myArr.get() returning only one element