2016-03-08 70 views
0

我有2个表格,日志和价格。表格日志中的内容显示为每个项目的文字视图。现在我想显示一些内容表格价格到相同的基础适配器。如何将多个表格数据显示为一个布局

这是可能的,我该怎么做?

这是我的基本适配器的活动,其中我显示内容形式表日志。我应该如何在这里显示内容表格价格?

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.display_logs_listview); 
     boolean sort = getIntent().getBooleanExtra("sort", false); 
     mainListView = (ListView) findViewById(R.id.ListViewItem); 

     final String place = (String) getIntent().getExtras().get("keyPlace"); 
     dbHandler = new LogsDBHandler(this); 
     ArrayList<Logs> logsList = sort ? dbHandler.getAllLogsByPlace() : dbHandler.getAllLogs(place); 

     TextView result = (TextView) findViewById(R.id.LogMassResult); 
     double sum = 0.0; 
     for(int i=0; i<logsList.size(); i++) { 
      sum += logsList.get(i).getResult(); 
     } 
     result.setText(String.format("%.2f", sum)); 

     listAdapter = new LogsArrayAdapter(logsList); 
     mainListView.setAdapter(listAdapter); 
    } 

    private class LogsArrayAdapter extends BaseAdapter { 
     private LayoutInflater inflater; 
     private List<Logs> logsList; 
     private List<Price> priceList; 

     public LogsArrayAdapter(List<Logs> logsList) { 
      inflater = LayoutInflater.from(DisplayLogs.this); 
      this.logsList = logsList; 
     } 

     @Override 
     public int getCount() { 
      return logsList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return logsList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return logsList.get(position).getId(); 
     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_display_logs, parent, false); 
      } 
      Logs log = logsList.get(position); 
      ((TextView) convertView.findViewById(R.id.textPlace)).setText(log.getPlace()); 
      ((TextView) convertView.findViewById(R.id.textNumber)).setText(log.getPlate_number()); 
      ((TextView) convertView.findViewById(R.id.textSort)).setText(log.getSort_id()); 
      ((TextView) convertView.findViewById(R.id.textGrade)).setText(log.getGrade()); 
      ((TextView) convertView.findViewById(R.id.textDiameter)).setText(log.getDiameter()); 
      ((TextView) convertView.findViewById(R.id.textLength)).setText(log.getLength()); 
      Log.d("Value", log.getCreatedAt()); 
      try { 
       Date dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(log.getCreatedAt()); 
       ((TextView) convertView.findViewById(R.id.textDate)).setText(new SimpleDateFormat("dd.MM.yyyy HH:mm").format(dt)); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 

      Log.d("Masa Trupca", String.format("%.2f", log.getResult())); 
      String final_result = String.format("%.2f", log.getResult()); 
        ((TextView) convertView.findViewById(R.id.textAmount)).setText(final_result); 
      return convertView; 
     } 
    } 

这是我用于获取价格的dbQuery。我在我的Logs类中创建了这个。这里我显示的是基于字符串参数的价格。

public Cursor getPrice() { 
     Cursor cursor = db.query("Price", new String[]{"price_stump_kn", "price_stump_eur", "road_price_kn", "road_price_eur"}, "sort = ? AND grade = ? AND length = ? BETWEEN diameter_dg = ? AND diameter_gg = ?", 
       new String[]{getSort_id(), getGrade(), getLength(), getDiameter(), getDiameter()}, null, null, null); 
     if (cursor.moveToFirst()) { 
      do { 
       Price price = new Price(); 
       price.setStumpPrice_kn(cursor.getString(0)); 
       price.setStumpPrice_eur(cursor.getString(1)); 
       price.setRoadPrice_kn(cursor.getString(2)); 
       price.setRoadPrice_eur(cursor.getString(3)); 
      } while (cursor.moveToNext()); 
     } 
     return cursor; 
    } 

那么应该如何显示来自一个基本适配器(listview)内的两个表的内容?

回答

0

这取决于这两个表中的记录是如何相关的。你说你在Logs类(我认为是一个域类,而不是DAO)中包含了一个DB查询,我怀疑你的类结构有点混乱。因此,我尝试为混合您的日志和价格的两种方式中的每一种勾画出一个阶级结构。解决方案A:每个日志都连接到一个价格,两个数据都将显示在一个项目中。

class LogDAO extends SQLiteOpenHelper { 
... 
    public Log getLogs(some selection parameters) { 
     Get logs according to selection parameters, and for each log 
     call getPrice(selection parameter according to log just found) 
     log.setPrice(price just found) 
    ... 

现在,在您的适配器,该项目日志,并与log.getPrice()你可以得到的价格属性,并可随时在您的适配器混合日志和价格的属性,在视图项目显示它们。

解决方案B:有一个混合列表 - 有些项目日志,其它的是价格

这里的关键是,你可以动态决定,为每个项目,在适配器中要使用的布局。所以结构将是:

class LogPriceDAO extends SQLiteOpenHelper { 
... 
    public Object getLogsAndPrices(some selection parameters) 
     Get logs and prices in some sequence, according to your business 
     logic and the selection parameters (If logs and prices have some 
     common superclass, use that instead of Object) 
... 

class LogsAndPricesAdapter extends BaseAdapter { 
... 
    @Override 
    public View getView (int i, View view, ViewGroup viewGroup) { 
     ... 
     Object currObject = this.getItem(i); // or common superclass 
     ... 
     View v; 
    if (currObject instanceOf Log) { 
     v = layoutInflater.inflate(R.layout.log_layout, null) 
     now fill fields of your log layout 
     ... 
    } else { 
     if (currObject instanceOf Price) { 
     v = layoutInflater.inflate(R.layout.price_layout, null) 
     now fill fields of your price layout 
     ... 
    return v 

instanceOf操作被一些人认为是不好的风格。因此,完美的方法是为LogPrice定义一个共同的超类,它提供了一个操作public Boolean isLog(),调用者可以从该操作决定它获得哪种类型的对象。

+0

谢谢你的回答。我会记住这一点。 我错了,根本不需要这个。我发现我的sql查询有问题(对我来说非常复杂)并且无法使其工作。 – RubyDigger19

相关问题