安卓ListView多个数组与自定义适配器

3

我从数据库中提取了几个json数组,这些数组包括(Name = 名称列表,address = 整数列表),并且它们托管在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);

这是我的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;
}}

此外,在我的DialerFragment中,我有这段代码:
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("");
        }
    }
};

我有一个 ListView (fragment_dialer.xml) 和一个行模板(contact_row_layout.xml)。

目前的代码没有错误,可以使用我的数据库中的联系人来填充ListView。

问题: 我需要根据点击的ImageButton动态调用联系人。例如,如何将1个姓名数组与1个地址数组匹配,并将该地址传递给OnCallClick以启动呼叫?如果能够为ImageButton分配一个值(类似于PHP中的操作),然后在contactsadapter中调用该“值”就好了。

我真的很困扰,非常感谢任何帮助。同时,我对Android不太熟悉。

解决方案: DialerFragment:

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

然后是我的“单行”类:
class SingleRow{
String name;
String address;

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

然后是我的“联系人基础适配器”:
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;
}}

注: 你是正确的,谢谢。我的问题在于我对这种编程完全是新手。我以前做过OOP,但只是用PHP,不知道如何在Android中操纵数据。感谢所有指导我正确方向的人,我学到了很多!此外,我找到了这个YouTube视频播放列表,帮助我理解了许多有关listviews和适配器等基础知识。

下一步: 我将研究ViewHolders和RecyclerViews来优化代码:)。并将数据传递到调用处理程序中,并显示出席状况!


你的数据模型是 SQLite 数据库,因此请使用 SimpleCursorAdapter - pskink
我没有使用sqlite。SimpleCursorAdapter仍然可以工作吗? - hipkiss
“with the contacts I have in my DB.” 这句话是什么意思? - pskink
我明白了,那就不用考虑 SimpleCursorAdapter 了。 - pskink
很抱歉,@MarkKeen,我不能这样做,因为数据库同时使用网络和安卓应用程序。在HTTP POST之后,传递给我的是JSON格式的数据,然后我可以进行操作。名称和地址都属于同一个JSONObject,但我将它们分开了。 - hipkiss
显示剩余2条评论
2个回答

3

因此,创建名为Contact的对象,添加其属性,然后在适配器布局中添加所需的TextView,只需在设置号码,姓名等时调用它们。请参见以下内容。

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;

那么我之后如何将这个数字传递给 onCallClick 呢?只需要这样吗:String numberToCall = ContactsAdapter.mContactNumber; - hipkiss
你在列表中设置了适配器。然后你把它放在litView.setOnItemClickListener(....)里面,这样你就可以得到用户点击的位置。之后,通过adapter.getItem(position)获取联系人,然后你可以随心所欲地处理它。 - Vulovic Vukasin
那么我需要构建另一个适配器类吗?这让我感到困惑。您有办法让问题更明确吗? - hipkiss
不,你只需要一个适配器类。在你的活动中初始化该适配器类,其中包含你的ListView。之后,将该适配器设置为ListView即可。 - Vulovic Vukasin
我来试一下。你的答案是否假定只有一个信息数组?我应该将两个数组都加载到联系人适配器中,然后使用哈希映射将它们关联起来吗? - hipkiss
你使用一个 ArrayList<Contacts>。 - Vulovic Vukasin

2
我建议创建一个新的对象数组(Contact),您可以从现有的contactNameArray和contactAddressArray数组中填充它。
    Contact {
        public String name;
        public String address;
    }

您需要将这个数组传递给ContactsAdapter。
在您实现的AdapterView.onItemClick中,您只需调用
    contactadapter.getItem(position);

为了将您的联系人对象与单击的行相关联。
另外需要注意的是,我强烈建议在getView方法中实现静态ViewHolder模式(https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接