ListView mit SimpleAdapter _id übergeben

M

MrChao

Neues Mitglied
0
Hallo,

ich habe seit Stunden das Problem das ich die Datenbank _id den des ListView Eintrag "nicht richtig" zur 2. Activity übergeben bekomme.
Ich poste mal erst die Codeabschnitte:

Code:
public class StempeluhrActivity extends Activity {
    private static final String HolydayHours = null;
    private Button ButtonCome;
    private Button ButtonGo;
    private DbAdapter dbHelper;
    private ListView list;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ButtonCome    = (Button) findViewById(R.id.button1);
        ButtonGo    = (Button) findViewById(R.id.button2);
        list         = (ListView) findViewById(R.id.listView1);
        
        /**
         * Die Datenbank hochfahren
         */
        dbHelper = new DbAdapter(this);
        dbHelper.open();
        
        // Die Vorhandenen Einträg anzeigen
        try {
            FillListViewWithData();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            Log.e("FillListViewWithData", e1.getMessage());
        }
Code:
    private void FillListViewWithData() throws Exception 
    {
         
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        
        String SummaryWorkingTime      = "00:00";
        String SummarySundayHours      = "00:00";
        String SummaryHolydayHours     = "00:00";
        String SummaryNightHours     = "00:00";
        int       SummaryOvernight     = 0;
        
        
        // Datenbankabfrage
         Cursor    cursor = dbHelper.fetchAllPoints();
         if (cursor.getCount()>0)
         {
             if (cursor.moveToFirst()) {
             do {
                 map = new HashMap<String, String>(); 
                 String dateTime         = cursor.getString(cursor.getColumnIndex("savedtime"));
                 String ComeTime         = cursor.getString(cursor.getColumnIndex("cometime"));
                        ComeTime         = FormatTime(ComeTime);
                 String GoTime           = cursor.getString(cursor.getColumnIndex("gotime"));
                        GoTime              = FormatTime(GoTime); 
                 String Overnight        = String.valueOf(cursor.getInt(cursor.getColumnIndex("overnight")));
                 if (Overnight == "0") Overnight = " ";
                 String Workingtime        = timeDiff(GoTime, ComeTime);
                 String SundayHours        = GetSundayHours(dateTime, ComeTime, GoTime);
                 String HolydaysHours    = GetHolydayHours(dateTime, ComeTime, GoTime);
                 String NightHours        = GetNightHours(ComeTime, GoTime);
                 
                 // Wenn Feiertag ist
                 if (HolydaysHours != "00:00")
                 {
                     // Wenn auch noch Sonntag
                     if (SundayHours != "00:00")
                     {
                         SundayHours = "00:00";
                         Workingtime = "00:00";
                         NightHours  = "00:00";
                     }
                     SundayHours = "00:00";
                     Workingtime = "08:00";
                     NightHours  = "00:00";
                 } else
                 if (SundayHours != "00:00")
                 {
                     Workingtime = "00:00";
                     NightHours  = "00:00";
                 }
                 
                 // Gesamtzeiten berechnen
                 SummaryWorkingTime = Addtime(SummaryWorkingTime, Workingtime);
                 SummarySundayHours = Addtime(SummarySundayHours, SundayHours);
                 SummaryHolydayHours = Addtime(SummaryHolydayHours, HolydaysHours);
                 SummaryNightHours = Addtime(SummaryNightHours, NightHours);
                 SummaryOvernight = SummaryOvernight + Integer.valueOf(Overnight);

                 if (NightHours         == "00:00") NightHours     = "-----";
                 if (Workingtime     == "00:00") Workingtime     = "-----";
                 if (SundayHours     == "00:00") SundayHours     = "-----";
                 if (HolydaysHours     == "00:00") HolydaysHours     = "-----";
                 if (Overnight        == "0")        Overnight = " ";
                 
         //        map.put("_id", id);
                 map.put("date",         FormatDate(dateTime));
                 map.put("come",         ComeTime);
                 map.put("go",           GoTime);
                 map.put("workingtime",     Workingtime);
                 map.put("nighthours",    NightHours);    
                 map.put("sundayhours",     SundayHours);
                 map.put("holydayhours",    HolydaysHours);
                 map.put("overnight",     Overnight);

                 mylist.add(map);

             } while (cursor.moveToNext());
             }

             
             // Wenn alle Zeilen ausgegeben sind, die Gesamtzeile drunter schreiben
             map = new HashMap<String, String>(); 
             map.put("date",         "------");
             map.put("come",         "-----");
             map.put("go",           "-----");
             map.put("workingtime",     SummaryWorkingTime);
             map.put("nighthours",    SummaryNightHours);    
             map.put("sundayhours",     SummarySundayHours);
             map.put("holydayhours",    SummaryHolydayHours);
             map.put("overnight",     String.valueOf(SummaryOvernight));

             mylist.add(map);
             

             SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.gamerow,
                     new String[] {"date", "come", "go", "workingtime", "nighthours", "sundayhours", "holydayhours", "overnight"}, 
                     new int[] {R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, R.id.textView7, R.id.textView8});
             
             try{
                 list.setAdapter(mSchedule);
                 
             } catch (Exception e) {
            // TODO Auto-generated catch block
                 Log.e("FillListViewWithData", e.getMessage());
             }
Code:
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view,
                int position, long id) {
                Intent i = new Intent(StempeluhrActivity.this, editday.class);
                i.putExtra("_id", id);
                startActivity(i);
            }
        });
editday.java:
Code:
public class editday extends Activity
{
    private DbAdapter dbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editday);
        
        TextView TextViewDatum =(TextView) findViewById(R.id.textViewDatum);
       
        long _id = 0;
        // Die ID des Datenbankeintrags übernehmen
        if(getIntent().hasExtra("_id") == true)
        {
          _id = getIntent().getExtras().getLong("_id");
        }
        
        /**
         * Die Datenbank hochfahren
         */
        dbHelper = new DbAdapter(this);
        dbHelper.open();

        // Datenbankabfrage
        Cursor    cursor = dbHelper.fetchEntry(_id);
        if (cursor.getCount()>0)
        {
            cursor.moveToFirst();
             String dateTime         = cursor.getString(cursor.getColumnIndex("savedtime"));        
             String ComeTime         = cursor.getString(cursor.getColumnIndex("cometime"));
             String GoTime             = cursor.getString(cursor.getColumnIndex("gotime"));
             String Overnight        = String.valueOf(cursor.getInt(cursor.getColumnIndex("overnight")));
             TextViewDatum.setText(dateTime + ", " + ComeTime + ", " + GoTime + ", " + Overnight);
        }
        cursor.close();

     }
dbadapter:
Code:
    public Cursor fetchEntry(long rowId) throws SQLException {
        return database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_TIME, KEY_GOTIME, KEY_COMETIME, KEY_OVERNIGHT },
                KEY_ROWID + "=" + rowId, null, null, null, null, null);
    /*    if (mCursor != null) {
            mCursor.moveToFirst();
        }*/
    //    return mCursor;
    }
Das oder die ListView wird korrekt angezeigt, aber wenn ich auf einen Eintrag klicke, soll der bearbeitet werden können. Es wird aber, warum auch immer, der falsche Eintrag in der 2. Activity aus der Datenbank geholt. Wenn ich einen Breakingpoint setzte, sehe ich das _id korrekt ist. Sehe ich den Wald vor lauter Bäumen nicht mehr?
Bitte und Hilfe...
 
Code:
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view,
                int position, long id) {
                Intent i = new Intent(StempeluhrActivity.this, editday.class);
                i.putExtra("_id", id);
                startActivity(i);
            }
        });

ich denke hier liegt dein Fehler: du übergibst die id, was in diesem Falle aber die id des listitems sein müßte. also wenn du das 5. item anklickst steht dann die id 4 drin (0 bis 4 = 5. item).

du willst aber die datenbank id, die du aber, soweit ich das sehen nirgends übergibst. ob du das feld jetzt deinem simple adapter einfach hinzufügen kannst, weiß ich nicht. ich benutze immer eigene adapter, das ist dann alles klar für mich ;-)
 
  • Danke
Reaktionen: MrChao
Erstmal danke für deine Antwort.
Ist es denn so, das wenn man einen SimpleCursorAdapter benutzt, statt einen SimpleAdapter, die id die Datenbank _id währe?
 
um diese id zu nutzen musst du in deinem adapter die methode

getItemID

überschreiben
 
Würdest du bitte posten wie das als Code aussieht?
Ich verstehe es so nicht so ganz:blushing:
 
Am einfachsten ist es, wenn du einen eigenen Adapter erstellst und eine eigene Klasse für deine Daten anlegst (z.b. Stempeluhr), die du dem Adapter dann übergibst.
Oft will man später ja noch dies und das in der Anzeige haben und vielleicht noch ungerade und gerade Zeilen andersfarbig darstellen. Und dann bist du mit dem SimpleAdapter wohl nicht gut beraten. Das Ding heißt ja schon Simple ;-)
 
OK, danke, das werde ich mir mal anschauen wie das geht.
 

Ähnliche Themen

A
Antworten
10
Aufrufe
1.022
swa00
swa00
D
Antworten
9
Aufrufe
1.769
jogimuc
J
M
Antworten
8
Aufrufe
955
deek
D
Zurück
Oben Unten