简体   繁体   中英

Android custom list view data is not updating while using shared preference

I am trying to get the value from shared preference and display it in customized list view. But the problem is list view is not getting updated with second value, means first time it's perfectly working fine but second time it overwrites the first value or may be it is opening another screen and displaying over there.

I want to add all the shared preferences data in list view one by one. please help me to solve this. Following is my code.

ListModel.java

public class ListModel {

private String Title = "";
private String Description = "";

/*********** Set Methods ******************/

public void setTitle(String Title) {
    this.Title = Title;
}

public void setDescription(String Description) {
    this.Description = Description;
}

/*********** Get Methods ****************/

public String getTitle() {
    return this.Title;
}

public String getDescription() {
    return this.Description;
}
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter implements OnClickListener {

/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList<?> data;
private static LayoutInflater inflater = null;
public Resources res;
ListModel tempValues = null;

/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList<?> d, Resources resLocal) {

    /********** Take passed values **********/
    activity = a;
    data = d;
    res = resLocal;

    /*********** Layout inflator to call external xml layout () ***********/
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

/******** What is the size of Passed Arraylist Size ************/
public int getCount() {

    if (data.size() <= 0)
        return 1;
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder {

    public TextView textViewTitle;
    public TextView textViewDescr;
}

/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;

    if (convertView == null) {

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        vi = inflater.inflate(R.layout.displaydata, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.textViewTitle = (TextView) vi.findViewById(R.id.title);
        holder.textViewDescr = (TextView) vi.findViewById(R.id.description);

        /************ Set holder with LayoutInflater ************/
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    if (data.size() <= 0) {
        holder.textViewTitle.setText("No Data");

    } else {
        /***** Get each Model object from Arraylist ********/
        tempValues = null;
        tempValues = (ListModel) data.get(position);

        /************ Set Model values in Holder elements ***********/

        holder.textViewTitle.setText(tempValues.getTitle());
        holder.textViewDescr.setText(tempValues.getDescription());
        // holder.image.setImageResource(res.getIdentifier(
        // "com.androidexample.customlistview:drawable/"
        // + tempValues.getImage(), null, null));

        /******** Set Item Click Listner for LayoutInflater for each row *******/

        vi.setOnClickListener(new OnItemClickListener(position));
    }
    return vi;
}

@Override
public void onClick(View v) {
    Log.v("CustomAdapter", "=====Row button clicked=====");
}

/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener {
    private int mPosition;
    OnItemClickListener(int position) {
        mPosition = position;
    }

    @Override
    public void onClick(View arg0) {
        Assignment sct = (Assignment) activity;
        sct.onItemClick(mPosition);
    }
}
}

Class Which reads shared preferencedata

public class Assignment extends Activity {

ListView list;
ImageView imageView; 
CustomAdapter adapter;
public Assignment CustomListView = null;
public ArrayList<ListModel> CustomListViewValuesArr = new ArrayList<ListModel>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.assignment);

    imageView = (ImageView) findViewById(R.id.createassignment);
    list = (ListView) findViewById(R.id.displaydata);

    CustomListView = this;
    setListData();
    Resources res = getResources();

    adapter = new CustomAdapter(CustomListView, CustomListViewValuesArr,
            res);
    list.setAdapter(adapter);

    imageView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(Assignment.this,
                    Assignment_Create.class);
            startActivity(intent);
        }
    });
}

public void setListData() {

    final ListModel sched = new ListModel();

    /******* Firstly take data in model object ******/
    sched.setTitle("Title : "
            + PreferenceConnector.readString(this,
                    PreferenceConnector.TITLE, null));
    sched.setDescription("Description : "
            + PreferenceConnector.readString(this,
                    PreferenceConnector.DESC, null));

    /******** Take Model Object in ArrayList **********/
    CustomListViewValuesArr.add(sched);
}

public void onItemClick(int mPosition) {
    ListModel tempValues = (ListModel) CustomListViewValuesArr
            .get(mPosition);
    Toast.makeText(
            CustomListView,
            "" + tempValues.getTitle() + "" + ""
                    + tempValues.getDescription(), Toast.LENGTH_LONG)
            .show();
}
 }

This function shows how i am writing data in shared Preference

public void sharedPrefernces() {
    if (Code.title != null)
        PreferenceConnector.writeString(this, PreferenceConnector.TITLE,
                Code.title);
    if (Code.description != null)
        PreferenceConnector.writeString(this, PreferenceConnector.DESC,
                Code.description);
}

在setListData()中,您只向适配器添加了1个项目,因此listView不会显示第2个项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM