如何解决 android 中片段的排序和数据显示问题?
How to Fix sorting issue and data display on fragment in android?
我正在学习 Android 自己。现在,我正在尝试学习片段。在这里,我想在联系人 activity.
上托管片段
我有 2 个问题
问题 1:
详细信息片段未显示所有数据。
例如来自我的 java class 。有
新联系人("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")
名单上有 Thal,姓氏。单击它应该显示名字、姓氏、phone 和电子邮件。但它只显示名字但有问题 2
问题 2:
我已经成功地在列表片段上按字母顺序对姓氏进行排序。但是,当我在列表片段上使用排序列表时,它没有在详细信息片段上显示正确的数据。详细片段显示片段数据的顺序是我在纯 java class 中的顺序。它应该显示要列出的相关数据。
当我不排序时,会显示正确的数据,但在单击列表片段上的姓氏时,详细信息片段上只有名字。
如何解决?我哪里弄错了?
联系activity
public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
@Override
public void itemClicked(long id){
//method also defined in the listener
View fragmentContainer = findViewById(R.id.fragment_detail_container);
if (fragmentContainer != null){
ContactDetailsFragment detailsFragment = new ContactDetailsFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
detailsFragment.setContact(id);
fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
}
}
联系方式片段
public class ContactDetailsFragment extends Fragment {
private long contactId;
public ContactDetailsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
contactId = savedInstanceState.getLong("contactId");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contact_details, container, false);
}
@Override
public void onStart(){
super.onStart();
View view = getView();
if (view != null){
TextView FNameText = (TextView) view.findViewById(R.id.textFName);
Contact contact = myContact[(int) contactId];
FNameText.setText(contact.getFName());
TextView LNameText = (TextView) view.findViewById(R.id.textLName);
LNameText.setText(contact.getLName());
TextView PhoneText = (TextView) view.findViewById(R.id.textPhone);
PhoneText.setText(contact.getPhone());
TextView EmailText = (TextView) view.findViewById(R.id.textEmail);
EmailText.setText(contact.getEmail());
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putLong("contactId", contactId);
}
public void setContact(long id){
this.contactId = id;
}
}
联系人列表片段
public class ContactListFragment extends ListFragment {
//ArrayAdapter<Contact> cAdapter;
ArrayAdapter<String> cAdapter;
interface ContactListListener{
void itemClicked(long id);
}
//add listener to fragment
private ContactListListener listener;
public ContactListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//String[] lastname = new String[Contact.class]
//cAdapter = new ArrayAdapter<>(inflater.getContext(), R.layout.fragment_contact_list, myContact);
String[] lnames = new String[Contact.myContact.length];
//String[] fnames = new String[Contact.myContact.length];
for (int i = 0; i < lnames.length; i++){
lnames[i] = Contact.myContact[i].getLName();
//fnames[i] = myContact[i].getFName();
}
//ArrayAdapter<String> cAdapter;
//ArrayAdapter<String> cAdapter;
//cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, myContact);
cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, lnames);
//to sort alphabetically
/*
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
});
*/
cAdapter.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
setListAdapter(cAdapter);
cAdapter.notifyDataSetChanged();
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
this.listener = (ContactListListener) activity;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
if (listener != null){
listener.itemClicked(id);
}
}
}
片段联系方式xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textFName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textLName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textPhone"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textEmail"
/>
</LinearLayout>
联系activityxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_contact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:baselineAligned="false"
tools:context="edu.uco.rawal.p5rabina.ContactActivity">
<fragment
class="edu.uco.rawal.p5rabina.ContactListFragment"
android:layout_width="0dp"
android:layout_weight ="1"
android:layout_height="match_parent"
android:id="@+id/contact_list_frag"/>
<FrameLayout
android:id="@+id/fragment_detail_container"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
Contact.java(纯javaclass)
public class Contact {
private String fname, lname, phone, email;
public static final Contact[] myContact = {
new Contact("Rabin", "Awal", "+405-315-0007", "rawal@yahoo.com"),
new Contact("David", "Gilmour", "+405-315-2027", "david@gilmour.com"),
new Contact("James", "Hetfield", "+405-315-2527", "james@metallica.com"),
new Contact("Kirk", "Hammet", "+405-315-2995", "kirk@metallica.com"),
new Contact("Tom", "Morello", "+405-315-2886", "tom@tommorello.com"),
new Contact("Angus", "Young", "+405-315-2831", "angus@acdc.com.au"),
new Contact("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")
};
private Contact(String fname, String lname, String phone, String email){
this.fname = fname;
this.lname = lname;
this.email = email;
this.phone = phone;
}
public String getFName() {
return fname;
}
public String getLName() {
return lname;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
/*
public void setFName(String fname) {
this.fname = fname;
}
public void setLName(String lname) {
this.lname = lname;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
*/
@Override
public String toString() {
//return super.toString();
return fname.toString();
//return this.fname;
}
}
谢谢
it is displaying only first name
在 ListView 中,这是有道理的,因为 ArrayAdapter<String>
将 toString
您的对象,即 return fname.toString();
(实际上可能只是 return fname
)
如果你的意思是在细节片段内部,那么在你的 XML 中,每个 TextView
理想情况下应该有 android:layout_height="wrap_content"
而不是 match_parent
。否则一个TextView
占满一屏
I have successfully sorted Lastnames alphabetically on the list fragment.
好的,那么我不确定问题出在哪里...也许您的意思是这就是问题所在?
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
});
这里有两个 Contact 对象...如果您想按姓氏排序,请执行此操作。
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact c1, Contact c2) {
return c1.getLName().compareToIgnoreCase(c2.getLName());
}
});
关于这段代码。
String[] lnames = new String[Contact.myContact.length];
//String[] fnames = new String[Contact.myContact.length];
for (int i = 0; i < lnames.length; i++){
lnames[i] = Contact.myContact[i].getLName();
//fnames[i] = myContact[i].getFName();
}
如果您想显示 Contact
的所有信息而不仅仅是 last/first 名称,我建议您尝试查找如何创建 ArrayAdapter<Contact>
。
我也不太确定在 onStart()
中使用 getView()
...我认为您应该使用 onCreateView
或 onViewCreated
中的视图。
我正在学习 Android 自己。现在,我正在尝试学习片段。在这里,我想在联系人 activity.
上托管片段我有 2 个问题
问题 1: 详细信息片段未显示所有数据。
例如来自我的 java class 。有 新联系人("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")
名单上有 Thal,姓氏。单击它应该显示名字、姓氏、phone 和电子邮件。但它只显示名字但有问题 2
问题 2: 我已经成功地在列表片段上按字母顺序对姓氏进行排序。但是,当我在列表片段上使用排序列表时,它没有在详细信息片段上显示正确的数据。详细片段显示片段数据的顺序是我在纯 java class 中的顺序。它应该显示要列出的相关数据。
当我不排序时,会显示正确的数据,但在单击列表片段上的姓氏时,详细信息片段上只有名字。
如何解决?我哪里弄错了?
联系activity
public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
@Override
public void itemClicked(long id){
//method also defined in the listener
View fragmentContainer = findViewById(R.id.fragment_detail_container);
if (fragmentContainer != null){
ContactDetailsFragment detailsFragment = new ContactDetailsFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
detailsFragment.setContact(id);
fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
}
}
联系方式片段
public class ContactDetailsFragment extends Fragment {
private long contactId;
public ContactDetailsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
contactId = savedInstanceState.getLong("contactId");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_contact_details, container, false);
}
@Override
public void onStart(){
super.onStart();
View view = getView();
if (view != null){
TextView FNameText = (TextView) view.findViewById(R.id.textFName);
Contact contact = myContact[(int) contactId];
FNameText.setText(contact.getFName());
TextView LNameText = (TextView) view.findViewById(R.id.textLName);
LNameText.setText(contact.getLName());
TextView PhoneText = (TextView) view.findViewById(R.id.textPhone);
PhoneText.setText(contact.getPhone());
TextView EmailText = (TextView) view.findViewById(R.id.textEmail);
EmailText.setText(contact.getEmail());
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putLong("contactId", contactId);
}
public void setContact(long id){
this.contactId = id;
}
}
联系人列表片段
public class ContactListFragment extends ListFragment {
//ArrayAdapter<Contact> cAdapter;
ArrayAdapter<String> cAdapter;
interface ContactListListener{
void itemClicked(long id);
}
//add listener to fragment
private ContactListListener listener;
public ContactListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//String[] lastname = new String[Contact.class]
//cAdapter = new ArrayAdapter<>(inflater.getContext(), R.layout.fragment_contact_list, myContact);
String[] lnames = new String[Contact.myContact.length];
//String[] fnames = new String[Contact.myContact.length];
for (int i = 0; i < lnames.length; i++){
lnames[i] = Contact.myContact[i].getLName();
//fnames[i] = myContact[i].getFName();
}
//ArrayAdapter<String> cAdapter;
//ArrayAdapter<String> cAdapter;
//cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, myContact);
cAdapter = new ArrayAdapter<>(inflater.getContext(), android.R.layout.simple_list_item_1, lnames);
//to sort alphabetically
/*
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
});
*/
cAdapter.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
setListAdapter(cAdapter);
cAdapter.notifyDataSetChanged();
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
this.listener = (ContactListListener) activity;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
if (listener != null){
listener.itemClicked(id);
}
}
}
片段联系方式xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textFName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textLName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textPhone"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:id="@+id/textEmail"
/>
</LinearLayout>
联系activityxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_contact"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:baselineAligned="false"
tools:context="edu.uco.rawal.p5rabina.ContactActivity">
<fragment
class="edu.uco.rawal.p5rabina.ContactListFragment"
android:layout_width="0dp"
android:layout_weight ="1"
android:layout_height="match_parent"
android:id="@+id/contact_list_frag"/>
<FrameLayout
android:id="@+id/fragment_detail_container"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
>
</FrameLayout>
</LinearLayout>
Contact.java(纯javaclass)
public class Contact {
private String fname, lname, phone, email;
public static final Contact[] myContact = {
new Contact("Rabin", "Awal", "+405-315-0007", "rawal@yahoo.com"),
new Contact("David", "Gilmour", "+405-315-2027", "david@gilmour.com"),
new Contact("James", "Hetfield", "+405-315-2527", "james@metallica.com"),
new Contact("Kirk", "Hammet", "+405-315-2995", "kirk@metallica.com"),
new Contact("Tom", "Morello", "+405-315-2886", "tom@tommorello.com"),
new Contact("Angus", "Young", "+405-315-2831", "angus@acdc.com.au"),
new Contact("Ron", "Thal", "+405-315-2827", "ron@ronthal.com")
};
private Contact(String fname, String lname, String phone, String email){
this.fname = fname;
this.lname = lname;
this.email = email;
this.phone = phone;
}
public String getFName() {
return fname;
}
public String getLName() {
return lname;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
/*
public void setFName(String fname) {
this.fname = fname;
}
public void setLName(String lname) {
this.lname = lname;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setEmail(String email) {
this.email = email;
}
*/
@Override
public String toString() {
//return super.toString();
return fname.toString();
//return this.fname;
}
}
谢谢
it is displaying only first name
在 ListView 中,这是有道理的,因为 ArrayAdapter<String>
将 toString
您的对象,即 return fname.toString();
(实际上可能只是 return fname
)
如果你的意思是在细节片段内部,那么在你的 XML 中,每个 TextView
理想情况下应该有 android:layout_height="wrap_content"
而不是 match_parent
。否则一个TextView
占满一屏
I have successfully sorted Lastnames alphabetically on the list fragment.
好的,那么我不确定问题出在哪里...也许您的意思是这就是问题所在?
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return o1.toString().compareToIgnoreCase(o2.toString());
}
});
这里有两个 Contact 对象...如果您想按姓氏排序,请执行此操作。
cAdapter.sort(new Comparator<Contact>() {
@Override
public int compare(Contact c1, Contact c2) {
return c1.getLName().compareToIgnoreCase(c2.getLName());
}
});
关于这段代码。
String[] lnames = new String[Contact.myContact.length];
//String[] fnames = new String[Contact.myContact.length];
for (int i = 0; i < lnames.length; i++){
lnames[i] = Contact.myContact[i].getLName();
//fnames[i] = myContact[i].getFName();
}
如果您想显示 Contact
的所有信息而不仅仅是 last/first 名称,我建议您尝试查找如何创建 ArrayAdapter<Contact>
。
我也不太确定在 onStart()
中使用 getView()
...我认为您应该使用 onCreateView
或 onViewCreated
中的视图。