list-使用 OnItemClickListener 从 List items 中检索 null 值

问题描述

使用 OnItemClickListener 从 List items 中检索 null 值

我想获取一个 OnItemClickListener 从 list items 中检索值。现在是点击后,也不能获得值。
下面是相关的代码:

public class DiarySchedule extends ListActivity implements OnClickListener
{
    private DiaryDataSource datasource;
    private static final String TAG = "MAD Diary Schedule";
    private String delTitle;
    private String delDate;
    private String delTime;
    private String editTitle;
    private String editDate;
    private String editTime;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.diary_schedule);
    datasource = new DiaryDataSource(this);
    datasource.open();
    List<DiaryEntry> values = datasource.getAllDiaryEntries();
    DiaryScheduleAdapter adapter = new DiaryScheduleAdapter(this,values);
    setListAdapter(adapter);
    registerForContextMenu(getListView());
}
public class DiaryScheduleAdapter extends ArrayAdapter<DiaryEntry>
{
    private LayoutInflater li;
    public DiaryScheduleAdapter(Context context, List<DiaryEntry> values)
    {
        super(context, 0, values);
        li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        DiaryEntry diaryEntry = getItem(position);
        View v = convertView;
        if ( v == null )
        {
            v = li.inflate(R.layout.diary_schedule, null);
        }
        TextView date = (TextView)v.findViewById(R.id.scheduleListDate);
        String initialDate = diaryEntry.getDate();
        String formattedDate = ConvertToDate(initialDate);
        date.setText(formattedDate);
        TextView link = (TextView)v.findViewById(R.id.scheduleListLink);
        link.setText(" at ");
        TextView time = (TextView)v.findViewById(R.id.scheduleListTime);
        time.setText(diaryEntry.getTime());
        TextView title = (TextView)v.findViewById(R.id.scheduleListTitle);
        title.setText(diaryEntry.getTitle());
        v.setOnClickListener(new OnItemClickListener(position));
        return v;
    }
}
@Override
public boolean onCreateOptionsMenu (Menu menu)
{
    new MenuInflater(getApplication()).inflate(R.menu.diary_menu, menu);
    return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected (MenuItem item)
{
    switch (item.getItemId())
    {
        case R.id.add:
            Intent intent = new Intent(DiarySchedule.this, DiaryAddEntry.class);
            startActivity(intent);
            break;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.diary_context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId())
    {
        case R.id.edit:
            Intent editIntent = new Intent(DiarySchedule.this, DiaryEditEntry.class);
            editTitle = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTitle)).getText();
            editDate = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListDate)).getText();
            editDate = GetInfoConvertToDate(editDate);
            editTime = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTime)).getText();
            editIntent.putExtra("title", editTitle);
            editIntent.putExtra("date", editDate);
            editIntent.putExtra("time", editTime);
            startActivity(editIntent);
            break;
        case R.id.delete:
            delTitle = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTitle)).getText();
            delDate = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListDate)).getText();
            delDate = GetInfoConvertToDate(delDate);
            delTime = (String) ((TextView) info.targetView.findViewById(R.id.scheduleListTime)).getText();
            Log.v(TAG, "Hopefully title is: " + delTitle + " with date of " + delDate + " and time of " + delTime);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("No", dialogClickListener).show();
            break;
    }
    return super.onContextItemSelected(item);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        switch (which)
        {
            case DialogInterface.BUTTON_POSITIVE:
            datasource.deleteDiaryEntry(delTitle, delDate, delTime);

            // IN CASE NEED TO DELETE ALL DB ENTRIES UNCOMMENT THIS
            // (AND COMMENT THE ABOVE METHOD)
            //datasource.deleteAll();
            Intent intent = new Intent(DiarySchedule.this, DiarySchedule.class);
            startActivity(intent);
            break;
            case DialogInterface.BUTTON_NEGATIVE:
            // No action taken
            break;
        }
    }
};
@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub

}
 private class OnItemClickListener implements OnClickListener
{
    private int mPosition;

    OnItemClickListener(int position)
    {
            mPosition = position;
    }

    @Override
    public void onClick(View v)
    {
        Log.v(TAG, "onItemClick at position" + mPosition);
        final String title = (String) ((TextView) findViewById(R.id.scheduleListTitle)).getText();
        System.out.println("Title is: " + title);
        String date = (String) ((TextView) findViewById(R.id.scheduleListDate)).getText();
        date = GetInfoConvertToDate(date);
        System.out.println("Date is: " + date);
        final String time = (String) ((TextView) findViewById(R.id.scheduleListTime)).getText();
        System.out.println("Time is: " + time);
        Intent descIntent = new Intent(DiarySchedule.this, DiaryDetailed.class);
        descIntent.putExtra("title", title);
        descIntent.putExtra("date", date);
        descIntent.putExtra("time", time);
        startActivity(descIntent);
    }
}
}

当调试到 OnItemClickListener 点击方法时,能顺利运行但是不能获取值,出现的是空值。什么原因啊?

解决方案

  final String title = (String) ((TextView) findViewById(R.id.scheduleListTitle)).getText();

每个 都改成v.findViewById看看:

 final String title = (String) ((TextView)v.findViewById(R.id.scheduleListTitle)).getText();
时间: 2024-08-30 02:25:07

list-使用 OnItemClickListener 从 List items 中检索 null 值的相关文章

android-从编辑文本中检索int值

问题描述 从编辑文本中检索int值 有两个文本框和一个数组,想要将数组索引号写在文字框1,然后在点击Calculate按钮时,在文本框2中获取数组值. "MainActivity.java"代码如下: import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import

MySQL中的NULL值相关问题

对于SQL的新手,NULL值的概念常常会造成混淆,他们常认为NULL是与空字符串''相同的事.情况并非 如此.例如,下述语句是完全不同的: mysql> INSERT INTO my_table (phone) VALUES (NULL); mysql> INSERT INTO my_table (phone) VALUES (''); 这两条语句均会将值插入phone(电话)列,但第1条语句插入的是NULL值,第2条语句插入的是空字符串 .第1种情况的含义可被解释为"电话号码未知&

MySQL数据库中与 NULL值有关的几个问题

  对于SQL的新手,NULL值的概念常常会造成混淆,他们常认为NULL是与空字符串''相同的事.情况并非如此.例如,下述语句是完全不同的: mysql> INSERT INTO my_table (phone) VALUES (NULL); mysql> INSERT INTO my_table (phone) VALUES (''); 这两条语句均会将值插入phone(电话)列,但第1条语句插入的是NULL值,第2条语句插入的是空字符串.第1种情况的含义可被解释为"电话号码未知&

jxl中,null值的单元格怎么设置边框

问题描述 jxl中,null值的单元格怎么设置边框啊,因为null值根本没法加进去 解决方案 可以构造空值或者你的null字符来填充,然后再设置边框

sql 语句中的 NULL值_MsSql

今天写了这样的代码 复制代码 代码如下: DECLARE @atr NVARCHAR(20) SET @atr = NULL IF(@atr = NULL) BEGIN PRINT 1 END 原本是想打印出1的.但是没有. 把 代码修改成如下: DECLARE @atr NVARCHAR(20) SET @atr = NULL IF(@atr IS NULL) BEGIN PRINT 1 END 这样就正确打印出1了. 然后自己有做了如下的修改,把if语句修改成 复制代码 代码如下: IF(N

SqlServer中的Null值空值问题

原文:SqlServer中的Null值空值问题 sql使用的是三值谓词逻辑,所以逻辑表达式返回的结果可以为True.False或者未知,在三值逻辑中返回True与不返回False并不完全一样,   SQL对查询过滤条件的处理:接受TURE  拒绝FALSE和UnKnown   SQL对CHECK约束的处理:接受FALSE 拒绝 TRUE和UNKnown   UNKNOWN的微妙之处在于当对它取反时  结果仍是UNKNOWN, 对两个Null值进行比较的表达式(Null=null),其计算的结果仍

Oracle数据库中对null值的排序及mull与空字符串的区别_oracle

order by排序之null值处理方法在对业务数据排序时候,发现有些字段的记录是null值,这时排序便出现了有违我们使用习惯的数据大小顺序问题.在Oracle中规定,在Order by排序时缺省认为null是最大值,所以如果是ASC升序则被排在最后,而DESC降序则排在最前.所以,为何分析数据的直观性方便性,我们需要对null的记录值进行相应处理. 这是四种oracle排序中NULL值处理的方法:1.使用nvl函数语法:Nvl(expr1, expr2)     若EXPR1是NULL,則返回

JS 去除Array中的null值示例代码_javascript技巧

复制代码 代码如下: function ClearNullArr(arr){ for(var i=0,len=arr.length;i<len;i++){ if(!arr[i]||arr[i]==''||arr[i] === undefined){ arr.splice(i,1); len--; i--; } } return arr; }

SQL 中 NULL值测试代码_MsSql

刚刚想从数据库中的表EXPERT_DETAILS中检索出修改人Modifier(类型 VARCHAR2(20),可为空)为空的那些记录,因为该字段的类型为VARCHAR2(20),我使用的SQL语句为 复制代码 代码如下: select * from expert_details twhere t.modifier = '' 没有检索出一条记录,而这与存储在该表中的记录是不相符的.后来想到即便是空字符型存储在数据库中也应该是NULL而不是''. 然后我使用下列SQL 语句,仍然没有检索出一条记录