Soru & Cevap

[ListFragment][ASyncTask] ListView Hep Boş Geliyor ...

30.11.2016 - 04:43

Merhaba arkadaşlar. Bir websitesinden veri çekerek bunu TitleClass objesini kullanarak oluşturduğum ArrayList'e dolduruyorum. Ardından bunu ListFragment içindeki her bir satırı 6 textview'den oluşan ListView'a yazdırmaya çalışıyorum. Ancak ListView hep boş geliyor. Debug ettiğimde önce uygulamanın açıldığını, ondan sonra ASyncTask'ın veriyi ArrayList'e aktardığını gördüm. Fakat sorunu çözemedim. Adapter'daki getView'e hiç girmiyor.

SolFrameFragment (ListFragment)

public class SolFrameFragment extends ListFragment implements View.OnClickListener {

    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);
        new GetTitleContents(this).execute();
    }

    @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> titleList) {
        super(context, resource);
        this.context = context;
        this.titleClassList = titleList;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TitleHolder holder;

        if (convertView == null) {
            LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
            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;
    }
}

GetTitleContents

public class GetTitleContents extends AsyncTask<String, Void, ArrayList<TitleClass>> {

    SolFrameFragment listFragment;
    ArrayList<TitleClass> titleArrayList = new ArrayList<>();
    private String url = "veriyi_cektigim_site";
    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();

                //Veri çekme işlemleri

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

TitleClass (Obje)

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

 

4 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-30491-1565085904.jpg
serhatleventyavas
01.12.2016 - 01:40

bu sorunun neden gerçekleştiğini anlamadım fakat eğer illa ListFragment kullanmak gerekmiyor ise ,Fragment içine bir Listview yada RecyclerView view koyarak da listeleme yapabilirsiniz.

Mehmet Şahin Çiftçi
04.12.2016 - 03:16
Bunun nasıl yapıldığını bilmiyorum. İnternette kaynak da bulamadım.