我正在构建我的第一个Android应用程序.我遇到的问题是与a相关的日期的存储和本地化显示SimpleCursorAdapter
.我有一个类封装了SQLitedatabase
对三个表的访问.该类负责以ISO格式存储所有日期("yyyy-MM-dd").当从数据库中读取日期值并在屏幕上显示时,我希望它们以本地化格式进行格式化.这是我提出的方法.它使用a ViewBinder
来进行格式化:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.text1) {
((TextView) view).setText(getDateFormatView().format(
parseDatabaseDate(cursor.getString(columnIndex))));
return true;
} else if (view.getId() == R.id.text2) {
((TextView)view).setText(
cursor.getString(columnIndex));
return true;
} else {
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
getDateFormatView()
创建一个SimpleDateFormat
具有从中读取的模式的对象strings.xml
.parseDatabaseDate()
是否SimpleDateFormat
使用常量模式从数据库中解析日期yyyy-MM-dd
.
虽然这段代码工作得很好但我想知道是否有更好的方法来做到这一点.我不喜欢的是我需要:
SimpleDateFormat
对象(一个用于parseDatabaseDate()
解析日期字符串SQLiteDatabase
,另一个用于格式化值)java.util.Date
创建的对象然后立即丢弃所以这就是我要问的原因:是否有更好的方式以本地化格式显示日期?
android localization internationalization android-viewbinder
我正在制作一个电视指南应用程序,它使用一次ListActivity
显示一个频道/一天的电视节目.我使用RelativeLayout
的ListView
项目,我想ListView
看看是这样的:
07:00 The Breakfast Show
Latest news and topical reports
08:00 Tom and Jerry
More cat and mouse capers
Run Code Online (Sandbox Code Playgroud)
我ListView
使用以下代码获取项目的数据:
Cursor cursor = db.rawQuery(SELECT blah,blah,blah);
String[] columnNames = new String[]{"start_time","title", "subtitle"};
int[] resIds = new int[]{R.id.start_time_short, R.id.title, R.id.subtitle};
adapter = new SimpleCursorAdapter(this, R.layout.guide_list_item, cursor, columnNames, resIds);
Run Code Online (Sandbox Code Playgroud)
我的问题是该start_time
字段datetime
具有以下格式:
2011-01-23 07:00:00
Run Code Online (Sandbox Code Playgroud)
所以我得到的是:
2011-01-23 07:00:00 The Breakfast Show
Latest news and topical reports
2011-01-23 08:00:00 Tom and Jerry
More …
Run Code Online (Sandbox Code Playgroud) android android-listview simplecursoradapter android-viewbinder
当我点击其中一个列表项时,我已经填充了ListActivity
一个启动另一个活动的Cursor
使用SimpleCursorAdapter
.我也ViewBinder
用来做一些自定义的数据转换.
我想CheckBox
在列表中的每一行添加一个,所以我改变了视图,并添加了一个CheckBox
重力权利.
添加CheckBox
已删除了单击项目的功能.按下列表项时,不再调用onListItemClick
我覆盖的方法ListActivity
.删除此CheckBox
修复程序.为什么是这样?
此外,如果单击列表项的主要部分但CheckBox
在选中项目时具有其他功能,如何设置列表以便继续执行我所需的功能?是否会onCheckedChangedListener
为列表中的每个项目重新设置工作或相同的视图实例?
我试图用SimpleCursorAdapter
一个ViewBinder
来从数据库中获取的图像,并把它放到我的ListView
项目视图.这是我的代码:
private void setUpViews() {
mNewsView = (ListView) findViewById(R.id.news_list);
Cursor cursor = getNews();
SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.cursor_item, cursor,
new String[] { "title", "content", "image" },
new int[] { R.id.cursor_title, R.id.cursor_content,
R.id.news_image });
ViewBinder viewBinder = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
ImageView image = (ImageView) view;
byte[] byteArr = cursor.getBlob(columnIndex);
image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
return true;
}
};
ImageView image = (ImageView) findViewById(R.id.news_image);
viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image")); …
Run Code Online (Sandbox Code Playgroud) android blob cursor indexoutofboundsexception android-viewbinder
我有4个TextViews
,2个ImageViews
,2个Buttons
和2个小部件,它们是a中行定义的一部分ListView
.数据来自XML和a SimpleAdapter
.要访问这些,TextViews
我ViewBinde
在自定义类中实现r并覆盖setViewValue
.这是有效的,TextViews
我想动态改变的两个是在setViewValue
.令我困惑的是为什么我的另外两个TextView没有通过setViewValue
.我这是基于设置一个断点,执行线程只输入两次.我期待看到它4次或更多次?
这是setViewValue
我设置断点的地方.
@Override
public boolean setViewValue(View view, Object data, String text)
{
if(view.getId() == R.id.txtvw1)
{
//blah do some stuff
}
else if (view.getId() == R.id.txtvw2)
{
//Blah do some stuff
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
xml声明TextViews
(1显示,4不):
<TextView
android:id="@+id/txtvw1"
android:layout_centerHorizontal="true"
android:layout_width="185dp"
android:layout_height="25dp"
android:textSize="20sp"
android:layout_marginTop="60dp"
android:gravity="center"
android:inputType="none"
android:text="@string/str_StaticA"
android:textColor="#C0F700" />
<TextView
android:id="@+id/txtvw4"
android:layout_alignParentLeft="true"
android:layout_marginLeft="35dp"
android:layout_width="95dp"
android:layout_height="50dp" …
Run Code Online (Sandbox Code Playgroud) 安卓工作室 3.6
构建.gradle:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-beta01'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Run Code Online (Sandbox Code Playgroud)
在 app/build.gradle 中:
android {
viewBinding.enabled = true
dataBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
在我的活动中:
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.myproject.BuildConfig
import com.myproject.R
import com.myproject.adapter.CustomFragmentStateAdapter
import com.myproject.databinding.QrBluetoothSwipeActivityBinding
import com.myproject.ui.fragment.BluetoothPageFragment
import com.myproject.ui.fragment.QrPageFragment
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableInt
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var viewBinding: QrBluetoothSwipeActivityBinding
var positionObservable = ObservableInt()
companion object {
private val TAG = QRBluetoothSwipeActivity::class.java.name …
Run Code Online (Sandbox Code Playgroud) 我正在为Android开发一个闹钟应用程序,我希望在主屏幕上显示警报列表.其中的每一行都ListView
在xml文件中定义.我希望TextViews
每周的每一天都能分开.程序将检查sqlite db是否为例如.monday
is = 1的值,然后将其颜色更改TextView
为红色.我写了这段代码,但这不起作用.怎么了?
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = db.fetchAllAlarms();
startManagingCursor(c);
String[] from = new String[] { db.KEY_TIME, db.KEY_NAME };
int[] to = new int[] { R.id.time, R.id.alarmName };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter alarms =
new SimpleCursorAdapter(this, R.layout.alarm_row, c, from, to);
alarms.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public …
Run Code Online (Sandbox Code Playgroud) sqlite android simplecursoradapter android-cursor android-viewbinder
我有一个复杂布局的列表R.layout.menu_row
.它由一个ProgressBar
和一个文本字段组成.我使用的适配器:
SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(path),
R.layout.menu_row, new String[] { "title", "progress" },
new int[] { R.id.text1,R.id.progressBar1});
Run Code Online (Sandbox Code Playgroud)
适配器知道如何自己处理TextViews
但不是ProgressBars
,所以我编写了一个复杂的数据绑定器:
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
//here goes the code
if () {
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在我被困在函数内填充映射.我需要将string的值设置progress
为setProgress
方法ProgressBar
.但我没有得到字符串progress
和句子的句柄ProgressBar
.
android android-progressbar simpleadapter android-viewbinder
我已经列出了使用数据库的名称SimpleCursorAdapter
,我想使用SimpleCursorAdapter.ViewBinder
's方法更改特定名称的颜色.我在运行此方法时遇到问题,我的数据库包含不同的名称,但ListView
会将所有namse显示为一个特定名称.我究竟做错了什么?如何更改特定名称的文本颜色?有可能用ViewBinder
吗?
这是我的代码部分ViewBinder
:
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
// TODO Auto-generated method stub
String[] temp = dh.return_result();
tv = (TextView)findViewById(R.id.textView1);
tv = (TextView) view;
for(int i = 0; i<temp.length; i++)
{
if(temp[i].equalsIgnoreCase("Ameer Zhann"))
{
tv.setText(temp[i]);
tv.setTextColor(Color.rgb(58, 58, 224));
return true;
}
}
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
这是我的输出图像:
我怎么解决这个问题?
android android-listview simplecursoradapter android-viewbinder
我想从视图(android.view.View)中删除基于codition.view的图像视图是该图像视图的src.如何从视图中删除imageView.请帮忙