问题描述
我发现,我需要一个ViewBinder不是一个自定义光标addapter但是我遇到的问题是,在每一行不会再确定是否需要隐藏或没有。它只是始终保持它隐藏
编辑:
我们gotting它来显示图像,并隐藏他们在需要的时候,但是当我滚动它们是令人沮丧的。
公共类HistoryFragment扩展片段{ ListView的listTimeline;
SimpleCursorAdapter适配器;
TextView的txtCreatedAt,txtFertile,txtTemp,txtCervix;
ImageView的imgIntercorse,imgEnergy,IMG pregancy,imgMood,imgHeadache,imgPeriod;
IntentFilter的过滤器; 静态最后的String [] FROM = {StatusData.KEY_CHARTING_DATE,StatusData.KEY_CHARTING_NOTES,StatusData.KEY_CHARTING_FERTILE,
StatusData.KEY_CHARTING_TEMPERATURE,StatusData.KEY_CHARTING_PERIOD,StatusData.KEY_CHARTING_INTERCORSE,
StatusData.KEY_CHARTING_MOOD,StatusData.KEY_CHARTING_HEADACHE,StatusData.KEY_CHARTING_TEST,
StatusData.KEY_CHARTING_ENERGY};
静态最终诠释[] TO = {R.id.txtCreatedAt,R.id.txtNote,R.id.txtFertile,
R.id.txtTemp,R.id.imgPeriod,R.id.imgIntercorse,
R.id.imgMood,R.id.imgHeadache,R.id.img pregancy,
R.id.imgEnergy}; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.fragment_history,集装箱,FALSE);
listTimeline =(ListView控件)view.findViewById(R.id.listTimeline); 返回视图。
} @覆盖
公共无效onResume(){
super.onResume();
this.setupList();
} @覆盖
公共无效的onPause(){
super.onPause();
} 私人无效setupList(){
txtCreatedAt =(TextView中)getActivity()findViewById(R.id.txtCreatedAt)。
txtFertile =(TextView中)getActivity()findViewById(R.id.txtFertile)。
txtTemp =(TextView中)getActivity()findViewById(R.id.txtTemp)。
txtCervix =(TextView中)getActivity()findViewById(R.id.txtCervix)。
imgIntercorse =(ImageView的)getActivity()findViewById(R.id.imgIntercorse)。
imgEnergy =(ImageView的)getActivity()findViewById(R.id.imgEnergy)。
IMG pregancy =(ImageView的)getActivity()findViewById(R.id.img pregancy)。
。imgMood =(ImageView的)getActivity()findViewById(R.id.imgMood);
imgHeadache =(ImageView的)getActivity()findViewById(R.id.imgHeadache)。
。imgPeriod =(ImageView的)getActivity()findViewById(R.id.imgPeriod);
//获取数据
光标C = getActivity()getContentResolver()查询(StatusProvider.CONTENT_URI_CHARTING,NULL,NULL,NULL,StatusData.KEY_CHARTING_DATE +DESC)。 //&所述3是氢;
//创建使用光标指向所希望的数据的转接器
//以及布局信息
适配器=新SimpleCursorAdapter(getActivity(),R.layout.history_row,C,FROM,TO);
adapter.setViewBinder(新CustomViewBinder()); //指定适配器的ListView
listTimeline.setAdapter(适配器);
} 私有类CustomViewBinder实现ViewBinder { 私人日期parseDate(字符串日期){
SimpleDateFormat的curFormater =新的SimpleDateFormat(YYYY-MM-DD);
日期选项dateObj =新的日期(); 尝试{
选项dateObj = curFormater.parse(日期);
}赶上(java.text.ParseException E){
// TODO自动生成catch块
e.printStackTrace();
}
返回选项dateObj;
} @覆盖
公共布尔setViewValue(查看视图,光标光标,诠释参数:columnIndex){ 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)){
SimpleDateFormat的格式=新的SimpleDateFormat(MM / DD / YYYY);
字符串日期= cursor.getString(参数:columnIndex);
日期选项dateObj = parseDate(日期); 字符串formatedDate = format.format(选项dateObj); TextView的电视=(TextView的)视图。
tv.setText(formatedDate);
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_period = cursor.getString(参数:columnIndex);
如果(is_period!= NULL){
如果(is_period.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_intercorse = cursor.getString(参数:columnIndex);
如果(is_intercorse!= NULL){
如果(is_intercorse.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_mood = cursor.getString(参数:columnIndex);
如果(is_mood!= NULL){
如果(is_mood.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_headache = cursor.getString(参数:columnIndex);
如果(is_headache!= NULL){
如果(is_headache.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_test = cursor.getString(参数:columnIndex);
如果(is_test!= NULL){
如果(is_test.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
} 如果(参数:columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)){
//如果列IS_STAR然后我们用自定义视图。
字符串is_energy = cursor.getString(参数:columnIndex);
如果(is_energy!= NULL){
如果(is_energy.equalsIgnoreCase(否)){
//设置视图GONE的知名度
view.setVisibility(View.INVISIBLE);
}
}
返回true;
}
//对于其他人,我们简单地返回false,这样的默认绑定
//发生。
返回false;
} }
}
您已经有了一些空变量。如果这是一个可接受的值,那么就检查空值在如果
的条件。如果不是,那么你需要改变你从拉这个数据SQLite的表。是这样的:
CREATE TABLE some_table(_id INTEGER PRIMARY KEY AUTOINCREMENT,some_column文本NOT NULL)
等
使用该结构,如果插入不包括用于该列的值,则它将失败。
What i found was that i needed a ViewBinder not a custom cursor addapter However the issue i'm having is that on each row it doesn't re-determine if it needs to be hidden or not. Its just always keeping it hidden
Edit:We've gotting it to show the images and hide them when needed but they are disheartening when i scroll.
public class HistoryFragment extends Fragment {
ListView listTimeline;
SimpleCursorAdapter adapter;
TextView txtCreatedAt, txtFertile, txtTemp, txtCervix;
ImageView imgIntercorse, imgEnergy, imgPregancy, imgMood, imgHeadache, imgPeriod;
IntentFilter filter;
static final String[] FROM = { StatusData.KEY_CHARTING_DATE, StatusData.KEY_CHARTING_NOTES, StatusData.KEY_CHARTING_FERTILE,
StatusData.KEY_CHARTING_TEMPERATURE, StatusData.KEY_CHARTING_PERIOD, StatusData.KEY_CHARTING_INTERCORSE,
StatusData.KEY_CHARTING_MOOD, StatusData.KEY_CHARTING_HEADACHE, StatusData.KEY_CHARTING_TEST,
StatusData.KEY_CHARTING_ENERGY };
static final int[] TO = { R.id.txtCreatedAt, R.id.txtNote, R.id.txtFertile,
R.id.txtTemp, R.id.imgPeriod, R.id.imgIntercorse,
R.id.imgMood, R.id.imgHeadache, R.id.imgPregancy,
R.id.imgEnergy};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
listTimeline = (ListView) view.findViewById(R.id.listTimeline);
return view;
}
@Override
public void onResume() {
super.onResume();
this.setupList();
}
@Override
public void onPause() {
super.onPause();
}
private void setupList() {
txtCreatedAt = (TextView) getActivity().findViewById(R.id.txtCreatedAt);
txtFertile = (TextView) getActivity().findViewById(R.id.txtFertile);
txtTemp = (TextView) getActivity().findViewById(R.id.txtTemp);
txtCervix = (TextView) getActivity().findViewById(R.id.txtCervix);
imgIntercorse = (ImageView) getActivity().findViewById(R.id.imgIntercorse);
imgEnergy = (ImageView) getActivity().findViewById(R.id.imgEnergy);
imgPregancy = (ImageView) getActivity().findViewById(R.id.imgPregancy);
imgMood = (ImageView) getActivity().findViewById(R.id.imgMood);
imgHeadache = (ImageView) getActivity().findViewById(R.id.imgHeadache);
imgPeriod = (ImageView) getActivity().findViewById(R.id.imgPeriod);
// Get the data
Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, null , null, StatusData.KEY_CHARTING_DATE + " DESC"); // <3>
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
adapter = new SimpleCursorAdapter(getActivity(), R.layout.history_row, c, FROM, TO);
adapter.setViewBinder(new CustomViewBinder());
// Assign adapter to ListView
listTimeline.setAdapter(adapter);
}
private class CustomViewBinder implements ViewBinder {
private Date parseDate(String date) {
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
Date dateObj = new Date();
try {
dateObj = curFormater.parse(date);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dateObj;
}
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) {
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
String date = cursor.getString(columnIndex);
Date dateObj = parseDate(date);
String formatedDate = format.format(dateObj);
TextView tv = (TextView) view;
tv.setText(formatedDate);
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_PERIOD)) {
// If the column is IS_STAR then we use custom view.
String is_period = cursor.getString(columnIndex);
if (is_period != null) {
if (is_period.equalsIgnoreCase("no")){
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE)) {
// If the column is IS_STAR then we use custom view.
String is_intercorse = cursor.getString(columnIndex);
if (is_intercorse != null) {
if (is_intercorse.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_MOOD)) {
// If the column is IS_STAR then we use custom view.
String is_mood = cursor.getString(columnIndex);
if (is_mood != null) {
if (is_mood.equalsIgnoreCase("no") ) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE)) {
// If the column is IS_STAR then we use custom view.
String is_headache = cursor.getString(columnIndex);
if (is_headache != null) {
if (is_headache.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_TEST)) {
// If the column is IS_STAR then we use custom view.
String is_test = cursor.getString(columnIndex);
if (is_test != null) {
if (is_test.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_ENERGY)) {
// If the column is IS_STAR then we use custom view.
String is_energy = cursor.getString(columnIndex);
if (is_energy != null) {
if (is_energy.equalsIgnoreCase("no")) {
// set the visibility of the view to GONE
view.setVisibility(View.INVISIBLE);
}
}
return true;
}
// For others, we simply return false so that the default binding
// happens.
return false;
}
}
}
You've got some null variables. If this is an acceptable value, then just check for null values in your if
conditions. If it is not, then you need to change the SQLite table that you're pulling this data from. Something like:
CREATE TABLE some_table(_id INTEGER PRIMARY KEY AUTOINCREMENT, some_column TEXT NOT NULL)
etc.
With this structure, if an insertion doesn't include a value for this column, then it will fail.
这篇关于Android的碎片与viewbinder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!