列表视图中没有名称的联系人的占位符

时间:2013-08-09 14:24:56

标签: android sqlite listview cursor simplecursoradapter

我有一个显示联系人的列表视图。这是使用SimpleCursorAdapter填充的。联系人的名称将显示在列表视图中 - 因此,如果联系人没有名称,则列表视图项目仍将显示,但不显示任何文本 - 因此显示空白行并使用户感到困惑。

我使用此方法从我的数据库中获取联系人列表:

public static List<Customer> getcustomerList()
        {
            try
            {
                List<Customer> customerList = new ArrayList<Customer>();
                Cursor cursor = getAllCustomers();
                if (cursor.moveToFirst()) {
                    do {
                        Customer cust = new Customer();
                        cust.customerId = UUID.fromString(cursor.getString(0));
                        cust.firstName = cursor.getString(1);
                        cust.lastName = cursor.getString(2);
                        customerList.add(cust);
                    } while (cursor.moveToNext());
                }
                return customerList;
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return null;
            }
        }

从我的活动中调用,在这里:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cust_list);
        // Show the Up button in the action bar.
        setupActionBar();
        populateListView();
    }

private void populateListView() {
        Cursor cursor = Db.Functions.getCustomerList();
        String[] cols = new String[] {
                "Name", "MobileNumber" };
        int[] to = new int[] {
                R.id.lblCustName, R.id.lblNumber };
        adapter = new SimpleCursorAdapter(
                this, R.layout.customer_info, cursor, cols, to, 0);
        ListView listView = (ListView) findViewById(R.id.custlist);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                try
                {
                    // Called when an individual item is pressed on customer list
                    Gen.vib(CustomApp.app, 15);
                    Cursor searchCursor = (Cursor) listView.getItemAtPosition(position);
                    String idString = searchCursor.getString(searchCursor.getColumnIndexOrThrow("_id"));
                    Intent intent = new Intent(CustomApp.app, ViewCustomerActivity.class);
                    intent.putExtra("uuid", idString);
                    startActivityForResult(intent, CODE_VIEWCUSTOMER);
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }

问题

什么是最清洁,最有效的方法,使我的列表视图显示为“未命名的客户”,当他们的名称为空? (不在数据库中存储它们)

1 个答案:

答案 0 :(得分:2)

最简单的方法是在阅读数据库时检查它:

public static List<Customer> getcustomerList()
    {
        try
        {
            List<Customer> customerList = new ArrayList<Customer>();
            Cursor cursor = getAllCustomers();
            if (cursor.moveToFirst()) {
                do {
                    Customer cust = new Customer();
                    cust.customerId = UUID.fromString(cursor.getString(0));
                     //Check if custumer has no name neither lastname:
                    if(cursor.getString(1).isEmpty() && cursor.getString(2).isEmpty()){
                      cust.firstName = "Unnamed Customer";
                   }
                   else{ 
                    cust.firstName = cursor.getString(1);
                    cust.lastName = cursor.getString(2);
                   }
                    customerList.add(cust);
                } while (cursor.moveToNext());
            }
            return customerList;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }

但我认为,更好的解决方案是使用custom Cursor Adapter。在这种情况下,您可以根据数据决定如何处理每个项目。