Lots of Android ListView Arrays with custom adapter

I pulled a couple of json arrays from DB, these are arrays (Name = list of names, address = list of integers) and placed in DialerFragment:

String[] contactNameArray= LoginHandler.getMyName();
String[] contactAddressArray= LoginHandler.getMyContactsAddress();
ListAdapter contactadapter = new ContactsAdapter(getActivity(), contactNameArray);
ListView contactsListView = (ListView) view.findViewById(R.id.contact_list);    
contactsListView.setAdapter(contactadapter);

      

Here is my "Contacts":

public class ContactsAdapter extends ArrayAdapter<String>{
private static final String TAG = "ContactsListAdapter";


public ContactsAdapter(Context context, String[] values){
    super(context, R.layout.contact_row_layout,  values);
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    Log.d(TAG, "getView");
    // The LayoutInflator puts a layout into the right View
    LayoutInflater inflater = LayoutInflater.from(getContext());
    // inflate takes the resource to load, the parent that the resource may be
    // loaded into and true or false if we are loading into a parent view.
    View view = inflater.inflate(R.layout.contact_row_layout, parent, false);
    // We retrieve the text from the array
    String contact = getItem(position);
    // Get the TextView we want to edit
    TextView mContactName = (TextView)view.findViewById(R.id.contact_name);
    view.findViewById(R.id.contacts_avatar);

    // Put the next TV Show into the TextView
    mContactName.setText(contact);

    return view;
}}

      

Also, in my DialerFragment, I have this code:

private final View.OnClickListener mOnMakeCallClick = new OnClickListener(){
    public void onClick(final View view){
        String numberToCall = "2";
        if (!TextUtils.isEmpty(numberToCall)){
            mCallWithVideo = view.getId() == R.id.contact_videocall;
            Log.d(TAG, "make call clicked: " + numberToCall + ", video=" + (mCallWithVideo ? "true" : "false"));
            mCallInitiated = ((Main)getActivity()).makeCall(numberToCall, mCallWithVideo);
            mTelNumberView.setText("");
        }
    }
};

      

I have a ListView (fragment_dialer.xml) and a string template (contact_row_layout.xml).

The code currently works without error, populating the ListView with the contacts I have in my database.

Problem: I need to dynamically call contacts depending on which ImageButton was clicked. That is, how do I map 1 array of names to 1 array of addresses and then pass that address to OnCallClick to initiate the call? It would be nice if I could assign a value to the ImageButton (as you would do something like this in PHP) and then name that "value" in the contactsadapter file.

I am really very fixated on this, any help would be greatly appreciated. Also I am completely new to Android.

Solution: DialerFragment:

ListView contactsListView = (ListView) view.findViewById(R.id.contact_list);
contactsListView.setAdapter(new ContactsBaseAdapter(getActivity()));

      

Then my "one line" class:

class SingleRow{
String name;
String address;

SingleRow(String name, String address){
    this.name=name;
    this.address=address;
}}

      

Then my "base pin adapter":

public class ContactsBaseAdapter extends BaseAdapter{
private static final String TAG = "CONTACTS_BASE_ADAPTER";
ArrayList<SingleRow> list;
Context context;
ContactsBaseAdapter(Context c){
    list = new ArrayList<SingleRow>();
    context = c;
    String[] contactNameArray= LoginHandler.getMyName();
    String[] contactAddressArray= LoginHandler.getMyContactsAddress();

    for(int i= 0; i< contactNameArray.length; i++){
        list.add(new SingleRow(contactNameArray[i], contactAddressArray[i]));
    }
}
@Override
public int getCount() {
    return list.size();
}
@Override
public Object getItem(int i) {
    return list.get(i);
}
@Override
public long getItemId(int i) {
    return i;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) 
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View row = inflater.inflate(R.layout.contact_row_layout,parent, false);

    ImageView mContactPresence = (ImageView)row.findViewById(R.id.contacts_avatar);
    TextView mContactName = (TextView)row.findViewById(R.id.contact_name);
    TextView mContactAddress = (TextView)row.findViewById(R.id.contact_number);

    SingleRow contactrow = list.get(i);

    mContactName.setText(contactrow.name);
    mContactAddress.setText(contactrow.address);
    Drawable drawable = null;
    mContactPresence.setImageDrawable(drawable);

    return row;
}}

      

Notes: You were right, thanks. My problem was that I was completely new to this kind of programming. I used to do OOP but only in PHP and didn't know how to manipulate data like this in android. Thumbs up from everyone who pointed me in the right direction, I learned a lot! Also, you found this YouTube playlist that helped me understand a lot of the basics of lists and adapters, etc.

Next: I'll be looking for ViewHolders and RecyclerViews for code optimization :). and pass the data to the call handler along with the presence !!

+3


source to share


2 answers


So create an object named Contact, add its attributes, after that in the adapter layout add the TextViews you want and just call them when setting the number, name, etc. Take a look below.

public class ContactsAdapter extends ArrayAdapter {private static final String TAG = "ContactsListAdapter";



public ContactsAdapter(Context context, String[] values){
    super(context, R.layout.contact_row_layout,  values);
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    Log.d(TAG, "getView");
    // The LayoutInflator puts a layout into the right View
    LayoutInflater inflater = LayoutInflater.from(getContext());
    // inflate takes the resource to load, the parent that the resource may be
    // loaded into and true or false if we are loading into a parent view.
    View view = inflater.inflate(R.layout.contact_row_layout, parent, false);
    // We retrieve the text from the array

    // Get the TextView we want to edit
//Create more textviews for showing desired atributes
    TextView mContactName = (TextView)view.findViewById(R.id.contact_name);
    TextView mContactNumber = (TextView) view.findViewById(R.id.contact_number);
    view.findViewById(R.id.contacts_avatar);
    //Create object contact that will have name, number, etc atributes...
    Contact contact = getItem(position);
    // Put the next TV Show into the TextView
    mContactName.setText(contact.getName());
    mContactNumber.setText(contact.getNumber());
    return view;

      

+3


source


I would recommend creating a new array of a new object (Contact) that you will populate from the existing contactNameArray and contactAddressArray arrays.

    Contact {
        public String name;
        public String address;
    }

      

You will pass this array to the ContactAdapter.

In your AdapterView.onItemClick implementation, you simply call



    contactadapter.getItem(position);

      

To get the contact object associated with the pressed string.

As a side note, I highly recommend using the static ViewHolder pattern in your getView ( https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html ).

+2


source







All Articles