3

I've pulled a couple json arrays from a DB, these are the arrays (Name = a list of names, address = a list of integers) and hosted 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 ContactsAdapter:

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;
}}

Additionally, within 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 ListView (fragment_dialer.xml) and the row template (contact_row_layout.xml).

The code currently works with no errors, populating the ListView with the contacts I have in my DB.

The issue: I need to dynamically call the contacts depending on which ImageButton was clicked. i.e. How do I match up 1 array of Names with 1 array of addresses and then pass that address into the OnCallClick to initiate the call? It would be nice if I could assign the ImageButton a value (as you would do something similar in PHP) and then call that "value" within the contactsadapter.

I'm really quite stuck on this, any help would be greatly appreciated. Also I'm quite new to Android.

Solution: DialerFragment:

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

Then my "single row" class:

class SingleRow{
String name;
String address;

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

Then my "contacts base 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 thank you. My issue was I was completely new to this sort of programming. I'd done OOP before but only in PHP and didn't know how to manipulate data like that within android. Thumbs up to all who pointed me in the right direction, I learned a lot! Also, found this youtube video playlist that helped me understand a lot of the fundamentals of listviews and adapters etc.

Next: I'll be looking at ViewHolders and RecyclerViews to optimize the code :). and to pass the data into a call handler along with presence!!

7
  • your data model is sqlite db, so use SimpleCursorAdapter Commented Jul 18, 2015 at 16:52
  • I'm not using sqlite. Would SimpleCursorAdapter still work? Commented Jul 18, 2015 at 16:57
  • so what does "with the contacts I have in my DB." mean? Commented Jul 18, 2015 at 16:58
  • I'm using one login/auth system for both an android and a web app. DB in this case refers to a standard mysql database Commented Jul 18, 2015 at 16:59
  • i see so forget about SimpleCursorAdapter Commented Jul 18, 2015 at 17:00

2 Answers 2

3

So, create object called Contact, add him atributes, after that in your adapter layout, add the TextViews you want and just call them when setting number, name, etc. 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;
Sign up to request clarification or add additional context in comments.

6 Comments

Then how would I pass the number into the onCallClick after that? Would it just be: String numberToCall = ContactsAdapter.mContactNumber; ?
You set the adapter in the list. Then you put it litView.setOnItemClickListener(....) and there, you get position where the user has clicked. Contact contact = adapter.getItem(position) after that, you can do with it whatever you want.
So I need to build another adapter class? That's confused me. Is there anyway you can make that clearer please?
No, you have one adapter class. You initialize that adapter class in your activity where you listview is. After that, you set the listview that adapter and you are good to go
I'll give this a go. Does your answer assume only 1 array of information? Would I load both arrays into the contactsadapter then use a hashmap to relate them?
|
2

I would recommend making a new array of a new object (Contact) which you would populate from your existing contactNameArray & contactAddressArray arrays.

    Contact {
        public String name;
        public String address;
    }

You would pass this array in to ContactsAdapter.

In your implementation of AdapterView.onItemClick, you'd simply call

    contactadapter.getItem(position);

In order to get your Contact object associated with the clicked row.

As an additional note, I'd strongly recommend implementing the static ViewHolder pattern in your getView (https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.