我正在尝试从共享首选项中获取值并将其显示在自定义列表视图中。但问题是列表视图没有用第二个值更新,意味着第一次它完全正常工作但第二次它覆盖了第一个值,或者可能是它打开另一个屏幕并在那里显示。
我想逐个添加列表视图中的所有共享首选项数据。请帮我解决这个问题。以下是我的代码。
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);
}
答案 0 :(得分:0)
在setListData()中,您只向适配器添加了1个项目,因此listView不会显示第2个项目。