使用swipeview在listfragment中自定义列表视图

时间:2016-11-29 19:16:04

标签: android listview android-listfragment android-viewholder

我有两个listfragments。 " SolFrameFragment"和" EntryFragment"。 " SolFrameFragment"有一个自定义列表视图,每行有6个textviews。 " 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;
}
}

2 个答案:

答案 0 :(得分:0)

问题在于片段中的onActivityCreated回调方法。

调用new GetTitleContents().execute();立即返回,无法保证当时已填充MainActivity.titleList。所以你基本上是为适配器设置一个空列表。

答案 1 :(得分:0)

假设GetTitleContents是AsyncTask(您尚未将其添加到您的问题中),请尝试:

  1. 将您的列表片段传递给GetTitleContents任务的创建以供日后使用

  2. setListAdapter调用移至onPostExecute任务的GetTitleContents方法(稍后使用)

  3. 总的来说,它应该如下所示:

    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();
    }