gpt4 book ai didi

java - 寻求一些了解 simpleCursorAdapter 工作原理的见解

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:16 26 4
gpt4 key购买 nike

大家好,我只是希望有人能阐明这段代码的工作原理,更具体地说是 simpleCursorAdapter。完整的程序是一个待办事项列表应用程序,它是一个非常简单的教程,用户可以输入数据或“注释”并使用游标和加载器保存到 sqlite 数据库。

所以我的问题是有一种特定的方法我无法理解它是如何工作的,因此我无法操纵数据的显示方式。我认为问题在于我只是不明白适配器如何采用与显示内容不同的布局并将其全部显示在 ListView 中。

  private void fillData() {

// Fields from the database (projection)
// Must include the _id column for the adapter to work
String[] from = new String[] { TodoTable.COLUMN_SUMMARY };


// Fields on the UI to which we map
int[] to = new int[] { R.id.label }; //I don't quite understand but I know it's just a value for the adapter

getLoaderManager().initLoader(0, null, this);

adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from,
to, 0); /*This line specifically I don't understand how it is working.
R.layout.todo_row is a near blank xml, used when there are no "todos"
with no listviews. R.layout.todo_list has the listview's but when
assigned in the adapter it doesn't work.


setListAdapter(adapter);

}

总的来说,我正在尝试并排制作 3 个 ListView ,以从数据库中读取数据并四处游玩。如果有人可以帮助我,我将不胜感激,谢谢。

R.layout.todo_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="24dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/reminder" >
</ImageView>

<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="@+id/TextView01"
android:textSize="24dp"
>
</TextView>


</LinearLayout>

和R.layout.todo_list

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@android:id/list"
android:layout_width="110dp"
android:layout_height="200dp" >

</ListView>

<ListView
android:id="@+id/listMiddle"
android:layout_width="110dp"
android:layout_height="200dp"
android:layout_toRightOf="@android:id/list" >
</ListView>

<ListView
android:id="@+id/listRight"
android:layout_width="110dp"
android:layout_height="200dp"
android:layout_toRightOf="@id/listMiddle" >
</ListView>

<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_todos" />

</RelativeLayout>

全类在下面

package de.vogella.android.todos;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import de.vogella.android.todos.contentprovider.MyTodoContentProvider;
import de.vogella.android.todos.database.TodoTable;

/*
* TodosOverviewActivity displays the existing todo items
* in a list
*
* You can create new ones via the ActionBar entry "Insert"
* You can delete existing ones via a long press on the item
*/

public class TodosOverviewActivity extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int DELETE_ID = Menu.FIRST + 1;
// private Cursor cursor;
private SimpleCursorAdapter adapter;
private SimpleCursorAdapter middleAdapter;
private SimpleCursorAdapter rightAdapter;



/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todo_list);
this.getListView().setDividerHeight(2);
fillData();
registerForContextMenu(getListView());
}

// Create the menu based on the XML defintion
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.listmenu, menu);
return true;
}

// Reaction to the menu selection
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.insert:
createTodo();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Uri uri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/"
+ info.id);
getContentResolver().delete(uri, null, null);
fillData();
return true;
}
return super.onContextItemSelected(item);
}

private void createTodo() {
Intent i = new Intent(this, TodoDetailActivity.class);
startActivity(i);
}

// Opens the second activity if an entry is clicked
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, TodoDetailActivity.class);
Uri todoUri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" + id);
i.putExtra(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri);

startActivity(i);
}



private void fillData() {

// Fields from the database (projection)
// Must include the _id column for the adapter to work
String[] from = new String[] { TodoTable.COLUMN_SUMMARY };
String[] middleId = new String[] { TodoTable.COLUMN_ID };

// Fields on the UI to which we map
int[] to = new int[] { R.id.label };
int[] two = new int[] { R.id.label };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from,
to, 0);

middleAdapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, middleId,
two, 0);


setListAdapter(adapter);
// setListAdapter(middleAdapter);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}

// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { TodoTable.COLUMN_ID, TodoTable.COLUMN_SUMMARY };
CursorLoader cursorLoader = new CursorLoader(this,
MyTodoContentProvider.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}

}

最佳答案

So my problem is that there is a specific method that I'm having trouble grasping how it works and as a result I cannot manipulate the way the data is displayed.

方法:

adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from, to, 0);

好吧,让我们按每个参数分解这个构造函数:

  1. this,一个上下文。适配器需要一个上下文来扩充每一行的布局。
  2. R.layout.todo_row,行的布局。 Cursor 中的每条记录都将显示在此布局中。 (具体如何显示 Cursor 取决于 fromto。)
  3. null,一个游标。这包含将显示在您的 ListView 中的所有数据。
  4. from,行布局中的基本 View 数组。
  5. ,您的 Cursor 中的基本列的数组。
  6. 0,何时以及为何应刷新数据的标志。

每件事背后的诀窍是:第四个 (from) 中的每个 ID 都必须与第二个参数 (R.layout.todo_row) 中的一个 View 相匹配。第五个参数中的每个字符串都必须与 Cursor 中的列名相匹配。第四个(from)和第五个参数(to)必须一一匹配,因为每一列都显示在一个View中。原来如此。


你现在可能已经意识到,这个注释:

R.layout.todo_row is a near blank xml, used when there are no "todos" with no listviews.

错了,抱歉。如果您想在 Cursor 为空时显示注释,请添加:

 <TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>

todo_list.xml,如 ListActivity's documenation 中所述.通过在您的 TextView 中使用这个“魔术 id”,注释应该会在适当的时候自动显示或隐藏。


所有这些只与第一个 ListView 交互(id:`android:id="@android:id/list"),您需要创建新的 Cursors 和 Adapters 才能使用其他 ListView。希望对您有所帮助!

关于java - 寻求一些了解 simpleCursorAdapter 工作原理的见解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15125285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com