-1

以下代码在列表视图上显示来自 sqlite 数据库的信息。第一个它显示日期并且列表视图允许我单击,当我单击一个日期时,它会显示在列表视图上该日期完成的信息。我的问题是当我在这个日期 12.01.2013 保存数据并且我稍后重复相同的日期然后它显示相同的日期两次并且我希望它只显示一个日期并且当我单击它以显示我的数据时早上保存的数据和我在同一天晚些时候保存的数据,而不是两个相似的日期。

这是代码

public class Pro extends Activity implements OnItemClickListener {

    private ListView uNamesListView;

        private ListAdapter customerListAdapter;
        private ArrayList<PojoClass> pojoArrayList;

    final Context context = this;
    private Button button;
    private TextView result;
    DBAdapter db = new DBAdapter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        uNamesListView = (ListView) findViewById(R.id.listView1);
        uNamesListView.setOnItemClickListener(this);

        pojoArrayList = new ArrayList<PojoClass>();
        customerListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
        uNamesListView.setAdapter(customerListAdapter);                 


    }

           public List<String> populateList() {


                List<String> uGraduateNamesList = new ArrayList<String>();
                DatabaseHelper openHelperClass = new DatabaseHelper(this);
                SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
                Cursor cursor = sqliteDatabase.query(DBAdapter.SCAN_TABLE, null, null, null, null, null, null);
                startManagingCursor(cursor);
                while (cursor.moveToNext()) {

                    String cDate = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_Date ));
                    String cMeterNumber = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_MeterNumber ));
                    String cVname = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_Vname ));
                    String cPname = cursor.getString(cursor.getColumnIndex(DBAdapter.COLUMN_PName ));

                    PojoClass ss = new PojoClass();
                    ss.SetName(cDate);
                    ss.SetSurname(cMeterNumber);
                    ss.SetID(cVname);
                    ss.SetHaddress(cPname);
                pojoArrayList.add(ss);

                    uGraduateNamesList.add(cDate);
                }
                sqliteDatabase.close();

                return uGraduateNamesList;

    }

    @Override
    public void onItemClick(AdapterView<?> aa, View view, int ss, long bb) {
        // TODO Auto-generated method stub
        Intent updateCustomerIntent = new Intent(Pro.this, ViewByVendor.class);

        updateCustomerIntent.putExtra("product", product);
        startActivity(updateCustomerIntent);

        Toast.makeText(getApplicationContext(), "Clicked on :" + ss, Toast.LENGTH_SHORT).show();
        PojoClass clickedObject =  pojoArrayList.get(ss);

        Bundle dataBundle = new Bundle();

        dataBundle.putString("clickedDate", clickedObject.getDates());
        dataBundle.putString("clickedMeterNumber", clickedObject.getMeterNumber());
        dataBundle.putString("clickedVname", clickedObject.getVname());
        dataBundle.putString("clickedPname", clickedObject.getPname());
        updateCustomerIntent.putExtras(dataBundle);
        startActivity(updateCustomerIntent);


    }


}

我会感谢你的帮助。

4

1 回答 1

0

在读取列表视图的日期时,您只需要唯一的日期。在 SQL 中,这可以使用DISTINCT或来完成GROUP BY

SELECT DISTINCT Date FROM ScanTable
SELECT Date FROM ScanTable GROUP BY Date

Android 代码如下所示:

cursor = db.query(true,                       // <-- distinct
                  DBAdapter.SCAN_TABLE,
                  new String[] { DBAdapter.COLUMN_Date },
                  null, null, null, null, null, null);
cursor = db.query(DBAdapter.SCAN_TABLE,
                  new String[] { DBAdapter.COLUMN_Date },
                  null, null,
                  DBAdapter.COLUMN_Date,      // <-- groupBy
                  null, null);

要显示日期的数据,您应该只将日期传递给该活动,并读取与日期匹配的所有记录:

SELECT * FROM ScanTable WHERE Date = ?
String selectedDate = ...;
cursor = db.query(DBAdapter.SCAN_TABLE, null,
                  DBAdapter.COLUMN_Date + " = ?", selectedDate,
                  null, null, null);
于 2013-01-12T07:47:18.097 回答