Listview没有添加第二项,只存储一个项目并在刷新时重写它

时间:2016-03-04 15:09:21

标签: android listview

你好我有活动选项卡,在第二个选项卡中我有Listiew从sql中提取数据并显示结果但是问题是我用logcat检查所有数据是否正确获取但listview只显示最后一个,当我添加新项目时到数据库它也显示最后一个但不是以前作为列表,当我试着resfresh也没有工作请帮助SOS昨天它工作有什么不对甚至如果我重新创建活动它不起作用 Tab2活动:

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.example.sultan.diplom.bazaMoiTemy.Contact;
import com.example.sultan.diplom.bazaMoiTemy.DataBaseHandler;

import java.util.ArrayList;
import java.util.List;


public class Tab2 extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    ArrayAdapter<String> arrayAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    List<Contact> contacts;
    DataBaseHandler db;
    ListView mListView;
    List<String> your_array_list,listcena,job,opisan;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_2,container,false);
        mListView = (ListView) v.findViewById(R.id.lvMain);
        db = new DataBaseHandler(getActivity());
        contacts = db.getAllContacts();
        your_array_list = new ArrayList<String>();
        listcena = new ArrayList<String>();
        job = new ArrayList<String>();
        opisan = new ArrayList<String>();
        mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);



        for (Contact cn : contacts) {
            String log = "ID:" + cn.get_id() + " Name: " + cn.get_name()
                    + cn.get_cena()+" "+cn.getJob()+" "+cn.get_opis();
            your_array_list.add(cn.get_name());
            listcena.add(cn.get_cena());
            job.add(cn.getJob());
            opisan.add(cn.get_opis());
            Log.e("AAAAAAAAAAA,",cn.get_name()+"");

        }

        arrayAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                your_array_list );
        mListView.setAdapter(arrayAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                String name = your_array_list.get(position);
                String jobs = job.get(position);
                String opis= opisan.get(position);
                String cen = listcena.get(position);


                Intent intent = new Intent(getActivity(), Temys.class);
                intent.putExtra("name", name);
                intent.putExtra("jobs", jobs);
                intent.putExtra("opis", opis);
                intent.putExtra("cen", cen);

                startActivityForResult(intent, 1);

            }
        });
        return v;
    }

       @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
                db=null;
                contacts = null;
                db = new DataBaseHandler(getActivity());
                contacts = db.getAllContacts();

                your_array_list = new ArrayList<String>();
                listcena = new ArrayList<String>();
                job = new ArrayList<String>();
                opisan = new ArrayList<String>();

                for (Contact cn : contacts) {
                    String log = "ID22222:" + cn.get_id() + " Name: " + cn.get_name()
                            + cn.get_cena()+" "+cn.getJob()+" "+cn.get_opis();
                    your_array_list.add(cn.get_name());
                    listcena.add(cn.get_cena());
                    job.add(cn.getJob());
                    opisan.add(cn.get_opis());
                    Log.e("1111111111111,",log);

                }
                arrayAdapter = new ArrayAdapter<String>(
                        getActivity(),
                        android.R.layout.simple_list_item_1,
                        your_array_list );
                arrayAdapter.notifyDataSetChanged();

                mListView.setAdapter(arrayAdapter);













            }
        }, 4000);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:id="@+id/swipe">
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
            android:id="@+id/lvMain"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    </ListView>


</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

联系人:

public class Contact {

    // private variables
    int _id;
    String _name;

    String _cena;
    String _opis;
    String job;
    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String get_cena() {
        return _cena;
    }

    public void set_cena(String _cena) {
        this._cena = _cena;
    }

    public String get_name() {
        return _name;
    }

    public void set_name(String _name) {
        this._name = _name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String get_opis() {
        return _opis;
    }

    public void set_opis(String _opis) {
        this._opis = _opis;
    }


    public Contact(String _name, String job, String _opis, String _cena) {
        this._name = _name;
        this.job = job;
        this._opis = _opis;
        this._cena = _cena;
    }



    // Empty constructor
    public Contact() {

    }

    // constructor


    // constructor


    // constructor



}

2 个答案:

答案 0 :(得分:1)

使用

更改onRefresh()块
@Override
public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(false);
            db=null;
            contacts = null;
            db = new DataBaseHandler(getActivity());
            contacts = db.getAllContacts();

            your_array_list.clear();
            listcena.clear();
            job.clear();
            opisan.clear();

            for (Contact cn : contacts) {
                String log = "ID22222:" + cn.get_id() + " Name: " + cn.get_name()
                        + cn.get_cena()+" "+cn.getJob()+" "+cn.get_opis();
                your_array_list.add(cn.get_name());
                listcena.add(cn.get_cena());
                job.add(cn.getJob());
                opisan.add(cn.get_opis());
                Log.e("1111111111111,",log);

            }
            arrayAdapter.notifyDataSetChanged();
        }
    }, 4000);
}

每次刷新时都要创建ArrayList和ArrayAdapter的新对象,不需要它只需要清除以前的数据,添加新数据并通过调用arrayAdapter.notifyDataSetChanged()通知ArrayAdapter更改数据集;

答案 1 :(得分:0)

谢谢你我所有的问题是当我删除它时,我将Listview放在Scrollview中它的工作正常