所以我试图在android studio中创建一个简单的优先事项列表。我创建了一个硬编码的列表进行测试,列表单元格的内容只是优先级的整数和消息的字符串。我无法让细胞填充。我终于到了在屏幕上填充列表的地步,但是我不知道如何在每次对象构造之后/期间如何获得自动递增的优先级。我只得到全1。我认为我不清楚细胞群体发生的过程,或者我不太了解基本的构建器。任何帮助/指针,我们将不胜感激。抱歉,所有Log.d的信息我都完全不了解,并且在弄清楚正确的语法时遇到了麻烦。
这是Cell对象
public class CellContent {
private int priority = 0;
private String itemContent;
public CellContent(String itemContent)
{
priority+=1;
this.itemContent = itemContent;
}
public String getItemContent() {
return itemContent;
}
public void setItemContent(String itemContent) {
this.itemContent = itemContent;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority=priority;
}
}
这是包含单元格的列表
public class ListDataSource {
private ArrayList<CellContent> todoList;
public ListDataSource() {
todoList = new ArrayList<CellContent>();
setupToDoList();
Log.d("ListDataSource", "ListDataSource object created");
}
public void setupToDoList()
{
todoList.add(new CellContent("Wash Dishes"));
todoList.add(new CellContent("Do Laundry"));
todoList.add(new CellContent("Clean Bathroom"));
todoList.add(new CellContent("Sweep and Mop"));
todoList.add(new CellContent("Walk Dog"));
}
public int getPriority(int position)
{
return todoList.get(position).getPriority();
}
public String getItemContent(int position)
{
return todoList.get(position).getItemContent();
}
public int getDataSourceLength()
{
return todoList.size();
}
}
这是适配器
public class ListDataSourceAdapter extends BaseAdapter {
private Context myContext;
private LayoutInflater myInflater;
private ListDataSource myDataSource;
public ListDataSourceAdapter(Context ctx, ListDataSource ds)
{
Log.d("ListDataSourceAdapter","Attempting to create ListDataSourceAdapter");
myContext = ctx;
myDataSource = ds;
myInflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ListDataSourceAdapter","ListDataSourceAdapter created successfully");
}
@Override
public int getCount()
{
return myDataSource.getDataSourceLength();
}
@Override
public Object getItem(int arg0)
{
return myDataSource.getItemContent(arg0);
}
@Override
public long getItemId(int position)
{
return myDataSource.getPriority(position);
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
Log.d("getView","Attempting getView");
TextView priority;
TextView itemContent;
if(convertView == null)
{
Log.d("getView", "Attempting to inflate cell");
convertView = myInflater.inflate(R.layout.cell_layout, parent, false);
}
Log.d("getView","attempting to link TextView priority");
priority = (TextView)convertView.findViewById(R.id.txtPriority);
Log.d("getView","link established, Attempting to set text for priority");
priority.setText(String.valueOf((myDataSource.getPriority(position))));
Log.d("getView", "attempting to link TextView listContent");
itemContent = (TextView)convertView.findViewById(R.id.txtListContent);
Log.d("getView","link established, Attempting to set text for listContent");
itemContent.setText(myDataSource.getItemContent(position));
Log.d("getView","Successfully executed getView");
return convertView;
}
}
最后但并非最不重要的是,这是我的活动
public class MainActivity extends AppCompatActivity {
private ListDataSource ds;
private ListView listViewToDo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity","Attempting to create data source");
try {
ds = new ListDataSource();
}
catch(Exception e)
{
e.printStackTrace();
Log.d("MainActivity","Failed to create data source");
}
Log.d("Main Activity","Attempting to link empty list view to on screen view");
listViewToDo = (ListView)findViewById(R.id.listOfLists);
Log.d("Main Activity","Views linked, Attempting to set adapter to listView");
listViewToDo.setAdapter(new ListDataSourceAdapter(this, ds));
Log.d("Maing Activity","Successfully set Adapter");
}
}
最佳答案
priority
是一个实例字段。您的每个CellContent
实例都有自己的priority
;每一个都初始化为零,然后在构造函数中加1。 CellContent
的一个实例不知道CellContent
的任何其他实例的内容,它们是完全独立的,因此每个实例都报告其优先级为1。
您要查找的行为需要一个静态字段。静态字段由所有封闭类型的实例共享,并且在首次使用该类时仅初始化一次:
private static int priorityCounter = 0; // shared by all instances
private int priority; // only this instance
public CellContent(...) {
priority = ++priorityCounter;
...
}
现在,每次创建
CellContent
都会增加一个共享计数器并将其priority
设置为该值。