我有一个像下面这样的列表视图.TextView中的文本来自数据库.
-------------------------
TextView Button
-------------------------
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,我想在Toast中显示此行的TextView中的文本.
我的问题如下:当我点击按钮时,我显示的是行的文本,由光标选中.我没有显示按钮所在行的文本.我知道问题是mCursor变量.
我不知道如何解决它.有人有想法吗?
这是我的ModulCursorAdapter:
public class ModuleCursorAdapter extends ResourceCursorAdapter implements OnClickListener {
private Context mContext;
private Cursor mCursor;
public ModuleCursorAdapter(Context context, Cursor cur) {
super(context, R.layout.notes_row, cur);
this.mContext = context;
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = LayoutInflater.from(context);
return li.inflate(R.layout.notes_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
this.mCursor = cur;
TextView tvText1 = (TextView)view.findViewById(R.id.text1);
Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);
tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));
int idRow …Run Code Online (Sandbox Code Playgroud) 我读了这个问题,它说不要担心,但我想我需要一些保证.
我的自定义CursorAdapter的bindView:
@Override
public void bindView(View view, Context context, Cursor c) {
// get handles for views in xml
ImageView imageView = (ImageView)view.findViewById(R.id.add_lvrow_image);
TextView titleView = (TextView)view.findViewById(R.id.add_lvrow_title);
// get data from cursor and "massage" if necessary
String imageUri = c.getString(c.getColumnIndex(CollectionsTable.COL_IMAGEURI));
String title = c.getString(c.getColumnIndex(CollectionsTable.COL_TITLE));
// terrible time getting run-time sizes of imageView, hardcode for now
int XML_WIDTH = 100;
int XML_HEIGHT = 100;
Log.d(TAG, SCOPE + "bindView called: " +count);
count++;
// use static util class
ImageUtils.loadBitmap(context, imageUri, …Run Code Online (Sandbox Code Playgroud) 我不太明白,提供商是指ContentProvider吗?或者我认为它们是一样的.
有任何想法吗?
场景 朋友我有一个ListView,我在FrameLayout中使用它,它将为我的DrawerLayout提供滑动菜单.我在ListView中有自定义按钮和文本,它们在单独的xml中定义为listitems.现在我使用customAdapter为此ListView提供数据,该扩展CursorAdapter ...
我想获得点击的位置以及按钮点击事件以相应地获取数据...我的代码如下......
列表项XML
<?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:descendantFocusability="blocksDescendants"
>
<!-- ToggleButton
android:id="@+id/button"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
style="@style/toggleButton"
android:background="@drawable/silent_button_selector"
android:focusable="false"
/> -->
<ImageButton
android:id="@+id/button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:src="@drawable/icon_button"
/>
<TextView
android:id="@+id/calendarName"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_toRightOf="@+id/button"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
适配器类:
public class CalendarListAdapter extends CursorAdapter implements OnClickListener{
@SuppressWarnings("deprecation")
public CalendarListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView …Run Code Online (Sandbox Code Playgroud) 我SimpleCursorAdapter为我扩展的自定义适配器ListFragment无法正确显示数据库中的项目.它不会在TextView中显示文本,我还需要对光标做什么才能显示正确的文本?
我以为我必须覆盖bindView但没有效果
设置适配器:
public void populateList(){
String[] fields = new String[] {BowlersDB.NAME};
Cursor c = getActivity().getContentResolver().query(BowlersDB.CONTENT_URI,
new String[] {BowlersDB.ID,BowlersDB.NAME},null,null,BowlersDB.NAME + " COLLATE LOCALIZED ASC");
mAdapter = new CheckAdapter(getActivity(),R.layout.check_listview,c,fields,new int[] {R.id.nameCheckTV});
setListAdapter(mAdapter);
getLoaderManager().initLoader(0,null,this);
}
Run Code Online (Sandbox Code Playgroud)
适配器:
public class CheckAdapter extends SimpleCursorAdapter{
Context context;
Cursor c;
public CheckAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.c = c;
// TODO Auto-generated constructor stub
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) android android-listview android-cursor android-loadermanager android-cursoradapter
我有一个微调器,我从数据库中填充.我想选择默认选择列表中的哪个项目.我需要找出列表中的哪个项目(CursorAdapter)具有值"Default Away"并将其设置为所选值.
Spinner away_team_spinner = (Spinner)findViewById(R.id.away_team_spinner);
DatabaseHelper db = new DatabaseHelper(this);
Cursor team_list = db.getTeams(p_game_level);
startManagingCursor(team_list);
String[] team_name = new String[]{colTeamName};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, team_list, team_name, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
away_team_spinner.setAdapter(adapter);
//// HERE IS WHERE MY ERRORS START ////
Log.i("NEW_GAME","Before set arrayadapter");
CursorAdapter adapter_choose = (CursorAdapter)away_team_spinner.getAdapter();
Log.i("NEW_GAME","Before set setSelection");
away_team_spinner.setSelection(adapter_choose.getPosition("Default Away"));
Run Code Online (Sandbox Code Playgroud)
这是我通过在这个网站上搜索找到的"解决方案".但是,我不能将"getPosition"与CursorAdapter对象一起使用.我尝试了ArrayAdapter,但是在"之前设置arrayadapter"注释错误之后的行"android.widget.SimpleCursorAdapter无法转换为android.widget.ArrayAdapter".我究竟做错了什么?谢谢.
我正在制作一个我使用数据库的android食谱应用程序.在数据库中有一个名为""images"的列,其中我存储了我在drawable文件夹中存储的食谱图片文件的名称.现在我想用食谱制作一个列表,显示:1)标题配方2)一个简短的描述和3)配方的图像为了做到这一点,我使用Simplecursoradaptor.
我的问题是我无法显示图像.
我想从"images"列中读取文件名,然后在我的imageview中设置图像(R.id.imageview1)
这是我的代码,直到现在:
public class RecipesMainActivity extends Activity
{
public static final String ROW_ID = "row_id"; //Intent extra key
private ListView esodaListView; // the ListActivity's ListView
private SimpleCursorAdapter esodaAdapter; // adapter for ListView
DatabaseConnector databaseConnector = new DatabaseConnector(RecipesMainActivity.this);
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes_main);
esodaListView = (ListView)findViewById(R.id.esodaList);
esodaListView.setOnItemClickListener(viewEsodaListener);
databaseConnector.open();
// map each esoda to a TextView in the ListView layout
// The desired …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的CursorAdapter类,它使用字母索引器,我想实现可过滤,但我有一些问题让它正常运行.
c = db.selectData();
lv = (ListView) findViewById(android.R.id.list);
searchTerm = (EditText) findViewById(R.id.inputSearch);
lv.setFastScrollEnabled(true);
adapter = new MyCursorAdapter(getApplicationContext(), R.layout.rowlayout, c, new String[]{"_id, name, company, job_title"},
new int[]{R.id.company, R.id.job_title, R.id.name});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// Search for states whose names begin with the specified letters.
Cursor cursor = db.searchAttendees(constraint.toString());
return cursor;
}
});
lv.setAdapter(adapter);
getListView().setTextFilterEnabled(true);
searchTerm.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
System.out.println("Text ["+s+"]");
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence …Run Code Online (Sandbox Code Playgroud) 我想在制作特定按钮(动作)时更改行布局.
public class DbCursorAdapter extends CursorAdapter {
private View selectedView;
private boolean isListSingleColumn = true;
private Cursor mCursor;
private final LayoutInflater mInflater;
private static final int TYPE_ITEM_SINGLE_COLUMN = 0;
private static final int TYPE_ITEM_MULTI_COLUMN = 1;
/**
* DbCursorAdapter constructor
*
* @param context
* - The context
* @param cursor
* - The cursor used to make queries
*/
public DbCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, false);
this.mContext = context;
this.mCursor = cursor;
this.mInflater = LayoutInflater.from(context);
}
@Override …Run Code Online (Sandbox Code Playgroud) 在我的活动中,我有一个代表医生的列表视图.每行都有医生的名字和一个复选框.我已经实现了一个回调接口,这样当选择医生时,所有其他医生都会从列表视图中删除,只剩下选定的医生.
它似乎有效,因为一旦我选择了医生,所有其他人都被移除了.当我取消检查医生时,每个人都被加回.但是,如果我现在选择一位不同的医生,原来的医生会停下来而其他所有医生都会被移除.
为了更好地解释这个问题,让我们说当我开始活动时,我在列表视图中有两位医生,Joel和Sam.我想我想选择Joel,所以我这样做,而且Sam从列表中删除了.然后,我意识到我错了,所以我取消选择乔尔,我现在看到列表中的乔尔和萨姆.最后,我选择了Sam.但是,Sam被从列表中删除,只有Joel再次出现.
以下是适配器类的一些代码片段:
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
final long id = cursor.getLong(cursor.getColumnIndex(DoctorEntry._ID));
String firstName = cursor.getString(cursor.getColumnIndex(DoctorEntry.COLUMN_FIRSTNAME));
viewHolder.nameView.setText(firstName);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(mCallbacks != null){
if(isChecked){
mCallbacks.onDoctorChecked(id);
} else{
mCallbacks.onDoctorUnchecked();
}
}
}
});
}
public void onRegisterCallbacks(DoctorAdapterCallbacks activity){
mCallbacks = activity;
}
public static interface DoctorAdapterCallbacks{
void onDoctorChecked(long id);
void onDoctorUnchecked();
}
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我有以下实现:
@Override
public void onDoctorChecked(long id) { …Run Code Online (Sandbox Code Playgroud) java android callback android-cursorloader android-cursoradapter