带有自定义适配器的 Android ListView 多个数组

标签 android arrays listview android-listview

我从数据库中提取了几个 json 数组,这些是数组(名称 = 名称列表,地址 = 整数列表)并托管在 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);

这是我的联系人适配器:

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 还很陌生。

解决方案: 拨号器 fragment :

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 视频播放列表,它帮助我了解了 ListView 和适配器等的许多基础知识。

下一步: 我将查看 ViewHolders 和 RecyclerViews 以优化代码 :)。并将数据连同存在一起传递到调用处理程序中!!

最佳答案

因此,创建名为 Contact 的对象,为其添加属性,之后在您的适配器布局中,添加您想要的 TextView,并在设置号码、名称等时调用它们。看下面。

公共(public)类 ContactsAdapter 扩展 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;

关于带有自定义适配器的 Android ListView 多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31493211/

相关文章:

c# - ListView 不将列导出到 excel

java - 如何通过自定义适配器将项目的 ArrayList 填充到 ListView?

android - 如何以编程方式启动应用程序详细信息?

android - 如何从自定义 ListView 数组适配器类中的 ListView 中删除行并刷新 ListView 中的剩余项目?

android - Jetpack Compose UI - 在 AlertDialog 内单击时按钮宽度发生变化

android - 如何在Android Studio中强制构建系统忽略资源文件?

arrays - Twig:追加到数组/对象内的数组

arrays - Excel VBA : What is the Maximum Number of String Elements that can be Stored in an Array

javascript - 这是将名称搜索到 JavaScript 数组中的正确方法吗?

android - ListView 内部 fragment 错误