activity_main.xml中的我的Listview没有显示任何数据。有人可以帮帮我吗?我是Android编程的新手......
是的,ArrayList中有数据,因为Log.d输出该列表中有几个条目。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bbrinck.bribi093_todo.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/taskLayout">
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom|end"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:clickable="true"
app:backgroundTint="@color/colorAccent"
app:srcCompat="@android:drawable/ic_menu_add" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/rowDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/colorPrimary"
android:textSize="30sp"
android:text="Description"/>
<TextView
android:id="@+id/rowDueDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rowDescription"
android:paddingLeft="5dp"
android:textColor="@color/colorPrimary"
android:textSize="12sp"
android:text="Due Date"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
protected ArrayList<Task> alTasks = new ArrayList<>();
public static final int REQUEST_CODE = 0;
protected DBHelper database;
protected RelativeLayout layout;
protected ListView listView;
protected TaskAdapter taskAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.database = new DBHelper(this);
this.taskAdapter = new TaskAdapter(this, this.alTasks);
layout = (RelativeLayout) findViewById(R.id.taskLayout);
listView = (ListView) findViewById(R.id.listView);
listView.setLongClickable(true);
listView.setAdapter(this.taskAdapter);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.floatingActionButton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendToAddIntent();
}
});
registerForContextMenu(listView);
this.loadTasks();
}
protected void removeTaskByPosition(int position){
if(!this.alTasks.isEmpty()) {
int realIndex = (this.alTasks.size() - 1) - position;
int taskId = this.alTasks.get(realIndex).id;
this.alTasks.remove(realIndex);
this.database.deleteTask(taskId);
}
}
protected void loadTasks(){
this.alTasks.clear();
this.alTasks = this.database.getAllTasks();
updateView();
}
protected void sendToAddIntent(){
Intent intent = new Intent(this, AddTaskActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
String description = data.getStringExtra("description");
String dueDate = data.getStringExtra("dueDate");
//Create new Task
this.database.inserTask(description, dueDate);
loadTasks();
updateView();
Toast toast = Toast.makeText(getApplicationContext(),
"Task was added!",
Toast.LENGTH_LONG);
toast.show();
}
}
protected void updateView(){
Log.d("NumberOfTasks", this.alTasks.size()+"");
//Display all tasks
//this.listView.setAdapter(this.taskAdapter);
this.taskAdapter.notifyDataSetChanged();
}
public String[] tasksToArray(){
String[] taskArray = new String[this.alTasks.size()];
//Create new String array in reverse order, so the newest task is at top.
for(int i = alTasks.size() - 1; i >= 0; i--){
taskArray[(alTasks.size() - 1) - i] = this.alTasks.get(i).description + " ("+this.alTasks.get(i).dueDate+")";
}
return taskArray;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu_add_task:
sendToAddIntent();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete:
removeTaskByPosition(info.position);
updateView();
Toast.makeText(this, "The selected task was deleted!",Toast.LENGTH_SHORT).show();
return true;
case R.id.view:
return super.onContextItemSelected(item);
}
return false;
}
}
public class TaskAdapter extends ArrayAdapter {
private final Context context;
private final ArrayList<Task> itemsArrayList;
protected LayoutInflater inflater;
public TaskAdapter(Context context, ArrayList<Task> itemsArrayList) {
super(context, R.layout.listview_row, itemsArrayList);
this.inflater = LayoutInflater.from(context);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.listview_row, parent, false);
// 3. Get the two text view from the rowView
TextView descriptionView = (TextView) rowView.findViewById(R.id.rowDescription);
TextView dueDateView = (TextView) rowView.findViewById(R.id.rowDueDate);
// 4. Set the text for textView
descriptionView.setText(itemsArrayList.get(position).description);
dueDateView.setText(itemsArrayList.get(position).dueDate);
// 5. retrn rowView
return rowView;*/
View itemView = convertView;
if (itemView == null) {
itemView = this.inflater.inflate(R.layout.listview_row, parent, false);
}
Task currentTask = this.itemsArrayList.get(position);
TextView description = (TextView) itemView.findViewById(R.id.rowDescription);
description.setText(currentTask.description);
TextView dueDate = (TextView) itemView.findViewById(R.id.rowDueDate);
dueDate.setText(currentTask.dueDate);
return itemView;
}
}
public ArrayList<Task> getAllTasks() {
ArrayList<Task> array_list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "SELECT * FROM " + TASK_TABLE_NAME, null );
res.moveToFirst();
while(!res.isAfterLast()){
Task tmpTask = new Task(res.getInt(res.getColumnIndex(TASK_ID)), res.getString(res.getColumnIndex(TASK_DESCRIPTION)), res.getString(res.getColumnIndex(TASK_DUEDATE)));
array_list.add(tmpTask);
res.moveToNext();
}
return array_list;
}
答案 0 :(得分:0)
您是否在getCount()
内实施了TaskAdapter
方法?
@Override public int getCount() {
return itemsArrayList.size(); }
答案 1 :(得分:0)
在申请taskAdapter.notifyDataSetChanged()
之前,请检查arraylist值是否正在改变。如果改变使用此
taskAdapter.notifyDataSetChanged();