通过ID(R.id.)获取String的长度

时间:2013-04-03 04:17:21

标签: android database string android-listview int

我的应用程序中有一个listview,显示数据库中的元素。列表视图的每个元素都显示数据库元素中的一些内容以及一个按钮。

稍后,我将使此按钮打开手机的拨号菜单,并在点击时拨打电话。我现在想要的是只有当我的数据库中与listview关联的元素的长度等于7时才显示此按钮。我的问题是获取此字符串的长度,因为它是使用对象的id从我的数据库调用的。

这是我的代码,我想检查长度的元素是与R.id.telephone“电话”,它是从数据库调用然后发送到另一个活动:

private void displayListView() {

    // getExtra
    Bundle bundle = getIntent().getExtras();
    String title = bundle.getString("title", "Choose here :");
    String inInterval = bundle.getString("inInterval");
    Log.d(TAG, "inInterval = " + inInterval);

    poititle.setText(title);

    // put the results of the method in a cursor
    Cursor c = dbHelper.findPoiInTable(inInterval);
    String[] columns = new String[] { DatabaseAdapter.COL_NAME,
            DatabaseAdapter.COL_STREET, DatabaseAdapter.COL_WEBSITE,
            DatabaseAdapter.COL_TELEPHONE, DatabaseAdapter.COL_REMARKS,
            DatabaseAdapter.COL_PRICE };
    int[] to = new int[] { R.id.name, R.id.street, R.id.website,
            R.id.telephone, R.id.remarks, R.id.price };
    cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
            columns, to, 0);

    ListView listView = (ListView) findViewById(R.id.poilistview);
    // Assign adapter to ListView
    listView.setAdapter(cursorAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        // Comportement des éléments de la listview
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent i = new Intent(getApplicationContext(),
                    POIActivity.class);

            String name = ((TextView) view.findViewById(R.id.name))
                    .getText().toString();
            String website = ((TextView) view.findViewById(R.id.website))
                    .getText().toString();
            String telephone = ((TextView) view
                    .findViewById(R.id.telephone)).getText().toString();
            String remarks = ((TextView) view.findViewById(R.id.remarks))
                    .getText().toString();
            String price = ((TextView) view.findViewById(R.id.price))
                    .getText().toString();
            // i.putExtra(ID_EXTRA, name) ;
            i.putExtra(ID_NAME, name);
            i.putExtra(ID_WEBSITE, website);
            i.putExtra(ID_TELEPHONE, telephone);
            i.putExtra(ID_REMARKS, remarks);
            i.putExtra(ID_PRICE, price);
            startActivity(i);
        }

    }); }

这是我尝试过的:我在我的diplsayListView()方法调用的同一个活动中创建了一个方法callbuttonManagement(),该方法的代码在上面,我用这个方式定义:

public void callbuttonManagement() {

    int countTelephone = ID_TELEPHONE.length();
    if (countTelephone == 7)
    {
        callButton.setVisibility(View.VISIBLE);
    }
    else
    {
        callButton.setVisibility(View.GONE);
    }


}

它崩溃我的应用程序,logcat表示nullpointerexception。有什么建议 ?谢谢!

1 个答案:

答案 0 :(得分:0)

class member boolean mShowButton;
String telephone = ((TextView) view
                .findViewById(R.id.telephone)).getText().toString();

if (telephone.length() == 7)
{
     mShowButton = true:
}

public void callbuttonManagement() {


if (mShowButton)
{
    callButton.setVisibility(View.VISIBLE);
}
else
{
    callButton.setVisibility(View.GONE);
}


}