0

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

稍后,我会让这个按钮打开电话的拨号菜单,并在点击时拨打一个号码。我现在想要的是仅当我的数据库中与列表视图关联的元素的长度等于 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);
        }

    }); }

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

public void callbuttonManagement() {

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


}

它使我的应用程序崩溃,并且 logcat 指示空指针异常。有什么建议么 ?谢谢 !

4

1 回答 1

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);
}


}
于 2013-04-03T04:35:28.090 回答