2016-09-08 52 views
0

ID這是我的第一個應用程序,我有一些問題絲毫的ListViewSQLite的:得到的ListView

如何點擊一個項目後,我可以在數據庫中的ID? 我無法使用位置,因爲ID可能不連續,即使它有時也不會返回正確的ID。

這是我的代碼,謝謝您的幫助:

import android.content.Intent; 
import android.database.Cursor; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.ActionMode; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AbsListView; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 


public class livello1 extends AppCompatActivity { 
DatabaseHelper myDb; 
Button next; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    myDb = new DatabaseHelper(this); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.livello1); 
    populateListView(); 
    registerClick(); 
} 




private void registerClick(){ 
    ListView list =(ListView)findViewById(R.id.listViewMain); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { 



      Intent i = new Intent(livello1.this, bossi.note.Edit.class); 

      i.putExtra("id", position); 

      //i.putExtra("id", id); 
      startActivity(i); 

     } 
    }); 

} 

private void populateListView(){ 

    Cursor res = myDb.getAllData(); 
    String[] myItems = new String[myDb.numRow()]; 
    int cont = 0; 
    if(res.getCount() == 0){ 
     // show message 
     return; 
    } 
    while(res. moveToNext()){ 
     myItems[cont] = res.getString(1); 
     cont ++; 
    } 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems); 
    ListView list =(ListView)findViewById(R.id.listViewMain);} 
+0

請寫,如何將清單。這就是'getView'方法 – Vyacheslav

回答

0

有很多的解決辦法。

例如,您可以在ListView初始化之前存儲Id的如果ArrayList

也就是說,執行"SELECT id FROM mytable"

然後在ArrayList中存儲的同時單擊方法使用。

例子:

//in class declaration 
private  ArrayList<Long> ar_ids = new ArrayList<Long>; 



// 
     String sql = "SELECT id FROM table"; 
      Cursor cur = db.rawQuery(sql, null); 
      String out = ""; 
      ArrayList<Long> ar_ids = new ArrayList<Long>; 
      if (cur.moveToFirst()) { 
       do { 

        ar.add(cur.getString(0)); 
        } while (cur.moveToNext()); 
        }else{ 

        } 
} 
     cur.close(); 

點擊事件:

@Override 
     public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { 



      Intent i = new Intent(livello1.this, bossi.note.Edit.class); 

      i.putExtra("id", ar_id.get(position)); 

//this is awsome! 
      startActivity(i); 

     } 

並初始化此類似:

private void myfunc() { 
    String sql = "SELECT id FROM table"; 
    Cursor cur = myDb.rawQuery(sql, null); 
    String out = ""; 
    ArrayList<Long> ar_ids = new ArrayList<Long>; 
    if (cur.moveToFirst()) { 
     do { 

      ar.add(cur.getString(0)); 
      } while (cur.moveToNext()); 
      }else{ 
       throw new NullPointerException(); 
      } 
} 
cur.close(); 
} 

private void populateListView(){ 
    myfunc(); 
    Cursor res = myDb.getAllData(); 
    String[] myItems = new String[myDb.numRow()]; 
    int cont = 0; 
    if(res.getCount() == 0){ 
     // show message 
     return; 
    } 
    while(res. moveToNext()){ 
     myItems[cont] = res.getString(1); 
     cont ++; 
    } 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.da_item, myItems); 
    ListView list =(ListView)findViewById(R.id.listViewMain);} 
0

好,而不是使用一個ArrayAdapter你可以擴展BaseAdapterListAdapter甚至ArrayAdapter,使您的自定義適配器。首先,創建一個普通的java類來將您的數據庫數據映射到對象,包括id。然後自定義BaseAdapter而不是使用ArrayAdapter。在您的BaseAdapter類中,您必須覆蓋各種方法,包括getItemgetItemId方法。現在,您可以從getItem方法返回整個映射對象,並通過在Activity或Fragment類中使用listView.getSelectedItem()方法獲取ListView的選定項的對象。一旦你得到的對象,你可以輕鬆地訪問該ID。

你可以找到一個很好的例子,自定義適配器here

關於數據庫結果映射到一個對象的解釋,你可以得到一些概念here

+0

擴展'ArrayAdapter'沒有任何問題。你提到的所有這些方法都可以被覆蓋。 –

+0

我不告訴他不要擴展ArrayAdapter。我只告訴他不要簡單地使用ArrayAdapter而不擴展它 –

+0

反正我現在也添加了ArrayAdapter:p –

0

在這裏,你可以代替你的代碼類這一塊,我已添加cursorAdapter並將其與您的ListView附加在適配器中我添加了一個獲取Id的方法,即c所有通過點擊偵聽器從光標檢索ID,您將不得不從下面的MyCursorAdapter類的public void getId(int position)中設置_ID的列號。

import android.content.Intent; 
import android.database.Cursor; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.ActionMode; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AbsListView; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 


public class livello1 extends AppCompatActivity { 
DatabaseHelper myDb; 
Button next; 
private MyCursorAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    myDb = new DatabaseHelper(this); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.livello1); 
    populateListView(); 
    registerClick(); 
} 




private void registerClick(){ 
    ListView list =(ListView)findViewById(R.id.listViewMain); 
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { 

      Intent i = new Intent(livello1.this, bossi.note.Edit.class); 

      //retrieve id from cursor 
     int _id = adapter.getId(position) 
      i.putExtra("id", _id); 

      //i.putExtra("id", id); 
      startActivity(i); 

     } 
    }); 

} 

private void populateListView(){ 

    Cursor res = myDb.getAllData(); 
    adapter = new MyCursorAdapter(this, res, 0); 
    ListView list =(ListView)findViewById(R.id.listViewMain); 
    list.setAdapter(adapter); 
} 


//adapter for list view 
class MyCursorAdapter extends CursorAdapter { 

    Cursor cursor; 

    // Default constructor 
    public MyCursorAdapter(Context context, Cursor cursor, int flags) { 
     super(context, cursor, flags); 
     this.cursor = cursor; 
    } 

    public void bindView(View view, Context context, Cursor cursor) { 
     String text = cursor.getString(1); 

     //make sure the TextView id is "@+id/text1" in da_item.xml 
     TextView tvText = (TextView) view.findViewById(R.id.text1); 
     tvText.setText(text); 
    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflator inflater = (LayoutInflater) context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
     return Inflater.inflate(R.layout.da_item, parent, false); 
    } 

    public int getId(int position){ 
     cursor.moveToPosition(position); 
     int colId = //set id column number here 
     int id = cursor.getLong(colId); 
     return id; 
    } 
} 

由於代碼是未經測試,你可能會面臨構建問題的開端,但它應該給在代碼中發生了什麼事的想法。隨時提出任何問題,如果你面對任何問題編譯