0

我有一个显示联系人的列表视图。这是使用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();
                }
            }
        });
    }

问题

当他们的名字为空白时,让我的列表视图显示“未命名的客户”的最干净、最有效的方法是什么?(没有将它们存储在数据库中)

4

1 回答 1

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. 在这种情况下,您可以根据每个项目的数据决定如何处理。

于 2013-08-09T15:04:37.567 回答