0
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

1 Answers1

0

Convert it as the following:

(String[]) myArr.toArray(new String[]{});
robermann
  • 1,722
  • 10
  • 19
  • after converting the list to an array i will have the same problem where i wont be able to pass the array to the inflater since it is initialized locally and after initializing it globally i have to set a size therefore it will become size dependent and will not vary with the number of contacts – – user3368764 Mar 02 '14 at 12:37
  • so you can't do this? "setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1, myArr.toArray(new String[]{})));" – robermann Mar 02 '14 at 12:40
  • (I'm just trying to understand you aim) – robermann Mar 02 '14 at 12:41
  • i did like you said but i didn't use tv.setText(myArr.get(position).toString()); that was the problem i thought i should use it as an array and use myArr[position] – user3368764 Mar 02 '14 at 12:43
  • Yes if it is an array you must use myArr[position]. So is this a working answer for your purposes? – robermann Mar 02 '14 at 12:51