我从数据库中提取了几个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来优化代码:)。并将数据传递到调用处理程序中,并显示出席状况!
SimpleCursorAdapter
。 - pskink