我有一个非常大的问题,对我来说这是......所以在这里 我有一个包含TextView和Button的片段,当我单击按钮并选择联系人时,我需要存储在ListView中的数字和名称以及名称和编号的右侧我想要一个按钮,这样就可以了选择的联系人可以删除...我不知道你是否理解,所以在ListView中所有将被存储为单个项目,名称下面的名称是联系人的号码,右侧是删除按钮...我知道我必须使用自定义适配器,但我只是不明白。
以下是我的适配器的代码
private class MessageAdapter extends ArrayAdapter<Message> {
public MessageAdapter(ArrayList<Message> messages) {
super(getActivity(), 0, messages);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.list_item_contact, null);
}
// Configure the view for this Contact
Message m = getItem(position);
TextView nameTextView = (TextView) convertView
.findViewById(R.id.contact_list_item_nameTextView);
nameTextView.setText(m.getNames());
TextView numberTextView = (TextView) convertView
.findViewById(R.id.contact_list_item_numberTextView);
numberTextView.setText(m.getNumbers());
return convertView;
}
}
以下是我尝试将联系人列入清单......
if (requestCode == REQUEST_CONTACT) {
Uri result = data.getData();
Log.v("Contact", "Got a result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone numbers for the selected contact id
Cursor c = getActivity().getContentResolver()
.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?",
new String[] { id },
ContactsContract.Contacts.DISPLAY_NAME);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);
int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME);
if (c.getCount() == 1) {
c.moveToFirst();
// contact has a single phone number, so there's no need to
// display a second dialog
mName = c.getString(nameIdx);
mNumber = c.getString(phoneIdx);
// Ovo radim ovako da bih mogao da ih upisem u fajl
mMessage.setmNames(mName);
mMessage.setmNumbers(mNumber);
((MessageAdapter) getListAdapter()).notifyDataSetChanged();
c.close();
}
}
因此,当我得到所选联系人的号码和姓名时,我想将其添加到列表中,但我总是只获得最后选择的号码和名称...希望您理解我的意思,谢谢。
以下是onCreate
和onCreateView
,以防万一:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_message, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Ovo ispod omogucava da se ugase sve activities i da se vrati na
// pocetnu valjda
// Zavisi sta smo stavili da nam bude parent class u xml manifest
if (NavUtils.getParentActivityName(getActivity()) != null) {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
mTextField = (EditText) v.findViewById(R.id.message_text);
mTextField.setText(mMessage.getmText());
mTextField.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mMessage.setmText(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
mAddContactButton = (Button) v.findViewById(R.id.message_contact);
mAddContactButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, REQUEST_CONTACT);
}
});
return v;
}
和onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMessage = new Message();
UUID messageId = (UUID) getArguments()
.getSerializable(EXTRA_MESSAGE_ID);
mMessage = MessageLab.get(getActivity()).getMessage(messageId);
mMessages = MessageLab.get(getActivity()).getMessages();
MessageAdapter adapter = new MessageAdapter(mMessages);
setListAdapter(adapter);
setHasOptionsMenu(true);
}
班级
public class MessageFragment extends ListFragment {
private ArrayList<String> names_list = new ArrayList<String>();
private ArrayList<String> numbers_list = new ArrayList<String>();
private ArrayList<Message> mMessages;
private Message mMessage;
private EditText mTextField;
private Button mAddContactButton;
private String mName, mNumber;
private String build_name="", build_number="";
public static final String EXTRA_MESSAGE_ID = "com.falca.emergencymessage.message_id";
private static final int REQUEST_CONTACT = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMessage = new Message();
UUID messageId = (UUID) getArguments()
.getSerializable(EXTRA_MESSAGE_ID);
mMessage = MessageLab.get(getActivity()).getMessage(messageId);
mMessages = MessageLab.get(getActivity()).getMessages();
MessageAdapter adapter = new MessageAdapter(mMessages);
setListAdapter(adapter);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (NavUtils.getParentActivityName(getActivity()) != null) {
NavUtils.navigateUpFromSameTask(getActivity());
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_message, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Ovo ispod omogucava da se ugase sve activities i da se vrati na
// pocetnu valjda
// Zavisi sta smo stavili da nam bude parent class u xml manifest
if (NavUtils.getParentActivityName(getActivity()) != null) {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
mTextField = (EditText) v.findViewById(R.id.message_text);
mTextField.setText(mMessage.getmText());
mTextField.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
mMessage.setmText(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
mAddContactButton = (Button) v.findViewById(R.id.message_contact);
mAddContactButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, REQUEST_CONTACT);
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == REQUEST_CONTACT) {
Uri result = data.getData();
Log.v("Contact", "Got a result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone numbers for the selected contact id
Cursor c = getActivity().getContentResolver()
.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?",
new String[] { id },
ContactsContract.Contacts.DISPLAY_NAME);
int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);
int nameIdx = c.getColumnIndex(Phone.DISPLAY_NAME);
if (c.getCount() > 1) { // contact has multiple phone numbers
final CharSequence[] numbers = new CharSequence[c.getCount()];
int i = 0;
if (c.moveToFirst()) {
mName = c.getString(nameIdx);
while (!c.isAfterLast()) { // for each phone number, add it
// to the numbers array
String type = (String) Phone.getTypeLabel(
this.getResources(), c.getInt(phoneType), ""); // insert
// a
// type
// string
// in
// front
// of
// the
// number
String number = type + ": " + c.getString(phoneIdx);
numbers[i++] = number;
c.moveToNext();
}
// build and show a simple dialog that allows the user to
// select a number
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setTitle(R.string.select_contact_phone_number_and_type);
builder.setItems(numbers,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int item) {
mNumber = (String) numbers[item];
int index = mNumber.indexOf(":");
mNumber = mNumber.substring(index + 2);
// Ovo radim ovako da bih mogao da ih upisem
// u fajl
build_name += mName + ";";
build_number += mNumber + ";";
mMessage.setmNames(build_name);
mMessage.setmNumbers(build_number);
((MessageAdapter) getListAdapter()).notifyDataSetChanged();
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity(getActivity());
alert.show();
} else
Log.w("Contact", "No results");
} else if (c.getCount() == 1) {
c.moveToFirst();
// contact has a single phone number, so there's no need to
// display a second dialog
mName = c.getString(nameIdx);
mNumber = c.getString(phoneIdx);
// Ovo radim ovako da bih mogao da ih upisem u fajl
build_name += mName + ";";
build_number += mNumber + ";";
mMessage.setmNames(build_name);
mMessage.setmNumbers(build_number);
((MessageAdapter) getListAdapter()).notifyDataSetChanged();
c.close();
}
}
}
@Override
public void onResume() {
super.onResume();
// Update the list view
((MessageAdapter) getListAdapter()).notifyDataSetChanged();
}
//MessageAdapter sluzi za rucno pravljenje liste poruka
private class MessageAdapter extends ArrayAdapter<Message> {
public MessageAdapter(ArrayList<Message> messages) {
super(getActivity(), 0, messages);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.list_item_contact, null);
}
// Configure the view for this Message
Message m = getItem(position);
TextView nameTextView = (TextView) convertView
.findViewById(R.id.contact_list_item_nameTextView);
nameTextView.setText(m.getmNames());
TextView numberTextView = (TextView) convertView
.findViewById(R.id.contact_list_item_numberTextView);
numberTextView.setText(m.getmNumbers());
return convertView;
}
}
public static MessageFragment newInstance(UUID messageId) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_MESSAGE_ID, messageId);
MessageFragment fragment = new MessageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onPause() {
super.onPause();
MessageLab.get(getActivity()).saveMessages();
}
}
答案 0 :(得分:0)
您似乎总是更新相同的单个mMessage字段。
mMessage.setmNames(build_name);
mMessage.setmNumbers(build_number);
当您有新条目时,您必须更新ArrayList mMessages。在列表中添加条目。适配器始终将此arrayList作为数据源,因此您必须更新此arrayList。
因此,为每个条目创建一条新消息并将其添加到mMessages数组列表中。