简体   繁体   English

使用共享首选项时,Android自定义列表视图数据未更新

[英]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 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 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个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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