2012-02-25 5 views
0

私はSQLiteDatabaseを作成しました。「表示」ボタンをクリックするとそのアイテムを見ることができ、データベース全体をListViewにします。このListViewでは、アイテムをクリック可能にしたいと思います。そのアイテムをクリックすると、同じ名前の別のIDを持つ新しいエントリを追加したいと思います。これまでの私のコード:Clickable ListViewアイテムにはSQL関数の新しいアイテムが必要です。

ListView lv; 
ArrayList<String> todoItems = new ArrayList<String>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 


     setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, todoItems)); 
      ListView lv = getListView(); 
      lv.setTextFilterEnabled(true); 

     hornot info = new hornot(this); 
     info.open(); 

     Cursor c = info.getAllTitles(); 
     if (c.moveToFirst()) 
     { 
     do{ 


      todoItems.add(c.getString(0) + " " + c.getString(1) + " " + c.getString(2)); 
      }while (c.moveToNext()); 
     } 
      if (todoItems.size() > 0) 
      { 
     lv.setAdapter(new ArrayAdapter<String>(sqview.this,android.R.layout.simple_list_item_1, todoItems)); 
      } 


      lv.setOnItemClickListener(new OnItemClickListener(){ 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

         //-///////////////////////// 
        } 
        }); 

      info.close(); 

     } 

見ての通り、私はその後、私はListViewに入れて、配列にデータベースのアイテムを置きます。私はアイテムを追加することができるアップグレードボタンを使用して、私はここで何か同じことをする必要があると思う。 dbhelpercreateEntry機能で :

私は EditTexts Stringsに、データベースに値を入れて変換 main.java
public long createEntry(String name, String hotness) { 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_NAME, name); 
     cv.put(KEY_HOTNESS, hotness); 
     return ourDatabase.insert(DATABASE_TABLE, null, cv);   
    } 

String name = sqlName.getText().toString(); 
    String hotness = sqlHotness.getText().toString(); 

    hornot entry = new hornot(dbhelp.this); 
    entry.open(); 
    entry.createEntry(name, hotness); 
    entry.close(); 

だから私はonItemClick機能のために書くべきか?

答えて

0

onItemClick()メソッドでは、クリックされたリスト行の位置(アダプタ内)を示すpositionパラメータがあります。あなたは、その行からデータを取得するためにListViewgetItemAtPositionを使用することもできます。

lv.setOnItemClickListener(new OnItemClickListener(){ 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
    String rowData = (String) lv.getItemAtPosition(position); //you'll need to make the lv as final 
    String[] data = rowData.split(" "); //split the result using the spaces (so you could obtain the name, hotness and the other string you use) 
    entry.createEntry(data[0], data[1]); 
} 

私はあなたのユニークなidについて何かをお勧めすることはできませんので、私はあなたのデータベース構造を知りません。

+0

エラーが発生しました:データベースが行内に開いていません: info.createEntry(data [0]、data [1]); しかし、私はsetonitemclickの後に閉じてしまうので、それはdefinitly openです。 –

+0

@JaniBelaその行の前に 'entry.open();'を追加します。 ' – Luksprog

+0

追加: info.open(); info.createEntry(データ[0]、データ[1]); \t info.close();それは今大丈夫です –

関連する問題