使用 swipeview 的列表片段中的自定义列表视图
Custom listview in a listfragment with swipeview
我有两个列表片段。 "SolFrameFragment" 和 "EntryFragment"。 "SolFrameFragment" 有一个自定义列表视图,每行有 6 个文本视图。 "GetTitleContents" 填充 "MainActivity.titleList" 这是一个对象 Arraylist。我在 "SolFrameFragment" 中的列表视图总是空的,我不明白为什么。
编辑:在SolFrameFragment中没有进入GetView。有什么问题?
SolFrameFragment
public class SolFrameFragment extends ListFragment implements View.OnClickListener {
private SolFrameAdapter solFrameAdapter;
ArrayList<TitleClass> titleList;
public SolFrameFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sol_frame, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
titleList = new ArrayList<>();
new GetTitleContents().execute();
titleList = MainActivity.titleList;
solFrameAdapter = new SolFrameAdapter(getActivity(), R.layout.solframe_single, titleList);
setListAdapter(solFrameAdapter);
}
@Override
public void onClick(View v) {
}
}
SolFrameAdapter
public class SolFrameAdapter extends ArrayAdapter<TitleClass> {
private final Context context;
private final ArrayList<TitleClass> titleClassList;
public SolFrameAdapter(Context context, int resource, ArrayList<TitleClass> titleClassList) {
super(context, resource);
this.context = context;
this.titleClassList = titleClassList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TitleHolder holder;
LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.solframe_single, parent, false);
holder = new TitleHolder();
holder.titleTV = (TextView) convertView.findViewById(R.id.titleNameTV);
holder.linkTV = (TextView) convertView.findViewById(R.id.titleLinkTV);
holder.todayEntryTV = (TextView) convertView.findViewById(R.id.titleTodayCountTV);
holder.totalEntryTV = (TextView) convertView.findViewById(R.id.titleTotalCountTV);
holder.lastWriterTV = (TextView) convertView.findViewById(R.id.titleLastWriterTV);
holder.firstWriterTV = (TextView) convertView.findViewById(R.id.titleFirstWriterTV);
convertView.setTag(holder);
} else {
holder = (TitleHolder) convertView.getTag();
}
TitleClass titleClass = titleClassList.get(position);
holder.titleTV.setText(titleClass.getTitle());
holder.linkTV.setText(titleClass.getLink());
holder.todayEntryTV.setText(titleClass.getTodayEntry());
holder.totalEntryTV.setText(titleClass.getTotalEntry());
holder.firstWriterTV.setText(titleClass.getFirstWriter());
holder.lastWriterTV.setText(titleClass.getLastWriter());
return convertView;
}
static class TitleHolder {
TextView totalEntryTV;
TextView todayEntryTV;
TextView titleTV;
TextView linkTV;
TextView firstWriterTV;
TextView lastWriterTV;
}
}
TitleClass
public class TitleClass {
int totalEntry;
int todayEntry;
String title;
String link;
String firstWriter;
String lastWriter;
public TitleClass(int todayEntry, int totalEntry, String link, String title, String firstWriter, String lastWriter) {
this.totalEntry = totalEntry;
this.todayEntry = todayEntry;
this.title = title;
this.link = link;
this.firstWriter = firstWriter;
this.lastWriter = lastWriter;
}
public int getTotalEntry() {
return totalEntry;
}
public int getTodayEntry() {
return todayEntry;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getFirstWriter() {
return firstWriter;
}
public String getLastWriter() {
return lastWriter;
}
}
solframe_single.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleNameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Başlık İsmi"
android:textSize="20sp" />
<TextView
android:id="@+id/titleTodayCountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="bugün"
android:textSize="20sp" />
<TextView
android:id="@+id/titleLinkTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleFirstWriterTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleLastWriterTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleTotalCountTV"
android:layout_width="0dp"
android:layout_height="0dp" />
</RelativeLayout>
</RelativeLayout>
GetTitleContents
public class GetTitleContents extends AsyncTask<String, Void, ArrayList<TitleClass>> {
SolFrameFragment listFragment;
ArrayList<TitleClass> titleArrayList = new ArrayList<>();
private String url = "website_url";
private int pageNo = 1;
public GetTitleContents(SolFrameFragment listFragment) {
super();
this.listFragment = listFragment;
}
@Override
protected ArrayList<TitleClass> doInBackground(String... params) {
try {
if (!titleArrayList.isEmpty())
titleArrayList.clear();
//Some works for fetching data
titleArrayList.add(new TitleClass(todayEntry, totalEntry, titleLink, titleName, firstAuthor[1], lastAuthor[1]));
}
} catch (IOException e) {
e.printStackTrace();
}
return titleArrayList;
}
@Override
protected void onPostExecute(ArrayList<TitleClass> titleArrayList) {
super.onPostExecute(titleArrayList);
if (listFragment != null) {
listFragment.setListAdapter(new SolFrameAdapter(listFragment.getActivity(), R.layout.solframe_single, titleArrayList));
}
}
public int getTodayEntry(String text) {
String result = null;
Pattern p = Pattern.compile("[0-9]+$");
Matcher m = p.matcher(text);
if (m.find()) {
result = m.group();
}
if (result != null)
return Integer.parseInt(result);
else
return 0;
}
}
问题出在片段中的 onActivityCreated 回调方法中。
立即调用new GetTitleContents().execute();
returns,无法保证MainActivity.titleList届时已填充。因此,您实际上是在为适配器设置一个空列表。
假设 GetTitleContents 是一个 AsyncTask(您还没有将它添加到您的问题中),尝试:
将您的列表片段传递给 GetTitleContents
任务的创建供以后使用
将setListAdapter
调用移动到GetTitleContents
任务的onPostExecute
方法(后面使用)
总而言之,它应该类似于以下内容:
GetTitleContents 任务
public class GetTitleContents extends AsyncTask<Void, Void, ArrayList<TitleClass>> {
ListFragment listFragment;
public GetTitleContents(ListFragment listFragment) {
super();
this.listFragment = listFragment;
}
@Override
protected ArrayList<TitleClass> doInBackground(Void... voids) {
// whatever you have currently that fetches and creates the arraylist
}
@Override
protected void onPostExecute(ArrayList<TitleClass> titleList) {
super.onPostExecute(titleList);
// whatever code you have that sets it in MainActivity (probably no longer needed)
if (listFragment != null) {
listFragment.setListAdapter(new SolFrameAdapter(listFragment.getActivity(), R.layout.solframe_single, titleList));
}
}
}
更新了onActivityCreated
方法
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new GetTitleContents(this).execute();
}
我有两个列表片段。 "SolFrameFragment" 和 "EntryFragment"。 "SolFrameFragment" 有一个自定义列表视图,每行有 6 个文本视图。 "GetTitleContents" 填充 "MainActivity.titleList" 这是一个对象 Arraylist。我在 "SolFrameFragment" 中的列表视图总是空的,我不明白为什么。
编辑:在SolFrameFragment中没有进入GetView。有什么问题?
SolFrameFragment
public class SolFrameFragment extends ListFragment implements View.OnClickListener {
private SolFrameAdapter solFrameAdapter;
ArrayList<TitleClass> titleList;
public SolFrameFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_sol_frame, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
titleList = new ArrayList<>();
new GetTitleContents().execute();
titleList = MainActivity.titleList;
solFrameAdapter = new SolFrameAdapter(getActivity(), R.layout.solframe_single, titleList);
setListAdapter(solFrameAdapter);
}
@Override
public void onClick(View v) {
}
}
SolFrameAdapter
public class SolFrameAdapter extends ArrayAdapter<TitleClass> {
private final Context context;
private final ArrayList<TitleClass> titleClassList;
public SolFrameAdapter(Context context, int resource, ArrayList<TitleClass> titleClassList) {
super(context, resource);
this.context = context;
this.titleClassList = titleClassList;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TitleHolder holder;
LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.solframe_single, parent, false);
holder = new TitleHolder();
holder.titleTV = (TextView) convertView.findViewById(R.id.titleNameTV);
holder.linkTV = (TextView) convertView.findViewById(R.id.titleLinkTV);
holder.todayEntryTV = (TextView) convertView.findViewById(R.id.titleTodayCountTV);
holder.totalEntryTV = (TextView) convertView.findViewById(R.id.titleTotalCountTV);
holder.lastWriterTV = (TextView) convertView.findViewById(R.id.titleLastWriterTV);
holder.firstWriterTV = (TextView) convertView.findViewById(R.id.titleFirstWriterTV);
convertView.setTag(holder);
} else {
holder = (TitleHolder) convertView.getTag();
}
TitleClass titleClass = titleClassList.get(position);
holder.titleTV.setText(titleClass.getTitle());
holder.linkTV.setText(titleClass.getLink());
holder.todayEntryTV.setText(titleClass.getTodayEntry());
holder.totalEntryTV.setText(titleClass.getTotalEntry());
holder.firstWriterTV.setText(titleClass.getFirstWriter());
holder.lastWriterTV.setText(titleClass.getLastWriter());
return convertView;
}
static class TitleHolder {
TextView totalEntryTV;
TextView todayEntryTV;
TextView titleTV;
TextView linkTV;
TextView firstWriterTV;
TextView lastWriterTV;
}
}
TitleClass
public class TitleClass {
int totalEntry;
int todayEntry;
String title;
String link;
String firstWriter;
String lastWriter;
public TitleClass(int todayEntry, int totalEntry, String link, String title, String firstWriter, String lastWriter) {
this.totalEntry = totalEntry;
this.todayEntry = todayEntry;
this.title = title;
this.link = link;
this.firstWriter = firstWriter;
this.lastWriter = lastWriter;
}
public int getTotalEntry() {
return totalEntry;
}
public int getTodayEntry() {
return todayEntry;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getFirstWriter() {
return firstWriter;
}
public String getLastWriter() {
return lastWriter;
}
}
solframe_single.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleNameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Başlık İsmi"
android:textSize="20sp" />
<TextView
android:id="@+id/titleTodayCountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="bugün"
android:textSize="20sp" />
<TextView
android:id="@+id/titleLinkTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleFirstWriterTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleLastWriterTV"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/titleTotalCountTV"
android:layout_width="0dp"
android:layout_height="0dp" />
</RelativeLayout>
</RelativeLayout>
GetTitleContents
public class GetTitleContents extends AsyncTask<String, Void, ArrayList<TitleClass>> {
SolFrameFragment listFragment;
ArrayList<TitleClass> titleArrayList = new ArrayList<>();
private String url = "website_url";
private int pageNo = 1;
public GetTitleContents(SolFrameFragment listFragment) {
super();
this.listFragment = listFragment;
}
@Override
protected ArrayList<TitleClass> doInBackground(String... params) {
try {
if (!titleArrayList.isEmpty())
titleArrayList.clear();
//Some works for fetching data
titleArrayList.add(new TitleClass(todayEntry, totalEntry, titleLink, titleName, firstAuthor[1], lastAuthor[1]));
}
} catch (IOException e) {
e.printStackTrace();
}
return titleArrayList;
}
@Override
protected void onPostExecute(ArrayList<TitleClass> titleArrayList) {
super.onPostExecute(titleArrayList);
if (listFragment != null) {
listFragment.setListAdapter(new SolFrameAdapter(listFragment.getActivity(), R.layout.solframe_single, titleArrayList));
}
}
public int getTodayEntry(String text) {
String result = null;
Pattern p = Pattern.compile("[0-9]+$");
Matcher m = p.matcher(text);
if (m.find()) {
result = m.group();
}
if (result != null)
return Integer.parseInt(result);
else
return 0;
}
}
问题出在片段中的 onActivityCreated 回调方法中。
立即调用new GetTitleContents().execute();
returns,无法保证MainActivity.titleList届时已填充。因此,您实际上是在为适配器设置一个空列表。
假设 GetTitleContents 是一个 AsyncTask(您还没有将它添加到您的问题中),尝试:
将您的列表片段传递给
GetTitleContents
任务的创建供以后使用将
setListAdapter
调用移动到GetTitleContents
任务的onPostExecute
方法(后面使用)
总而言之,它应该类似于以下内容:
GetTitleContents 任务
public class GetTitleContents extends AsyncTask<Void, Void, ArrayList<TitleClass>> {
ListFragment listFragment;
public GetTitleContents(ListFragment listFragment) {
super();
this.listFragment = listFragment;
}
@Override
protected ArrayList<TitleClass> doInBackground(Void... voids) {
// whatever you have currently that fetches and creates the arraylist
}
@Override
protected void onPostExecute(ArrayList<TitleClass> titleList) {
super.onPostExecute(titleList);
// whatever code you have that sets it in MainActivity (probably no longer needed)
if (listFragment != null) {
listFragment.setListAdapter(new SolFrameAdapter(listFragment.getActivity(), R.layout.solframe_single, titleList));
}
}
}
更新了onActivityCreated
方法
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new GetTitleContents(this).execute();
}