簡體   English   中英

使用共享首選項時,Android自定義列表視圖數據未更新

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

我正在嘗試從共享首選項中獲取值並將其顯示在自定義列表視圖中。 但是問題在於列表視圖沒有更新為第二個值,這意味着第一次它可以正常工作,但是第二次它覆蓋了第一個值,或者可能是正在打開另一個屏幕並在那里顯示。

我想在列表視圖中一一添加所有共享的首選項數據。 請幫助我解決這個問題。 以下是我的代碼。

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

讀取共享首選項數據的類

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

此功能顯示我如何在共享首選項中寫入數據

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