mardi 29 septembre 2015

Save Items in ListView in to the Database

I'm developing an android application using SQLite Database. I have a itemList which user can enter food items into the List. I want to save the items of each listview seperately (one listview data in a line). And i'm using an Arraylist to store data which user enters into the System. Can someone help me to do this. Below i've posted my XML page and Java Class. Thanks in advance!! :)

manualListScr.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/itmName"
    android:hint="Enter Item"
    android:textSize="24dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addItems"
    android:text="Add Item"
    android:layout_below="@+id/itmName"
    android:layout_alignParentLeft="true"/>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Manual Item List"
    android:id="@+id/itmHeader"
    android:layout_below="@+id/addItems"
    android:background="#5e5e5e"
    android:textColor="#FFFFFF"
    android:textSize="14dp"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/itmList"
    android:layout_below="@+id/itmHeader"
    android:layout_centerHorizontal="true">

</ListView>


manualList.java

public class manualInput extends Activity implements View.OnClickListener{
private Button addButton;
private EditText editText;
private ListView listView;
ArrayList<String> itmList = new ArrayList<String>();
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.manualinputscr);
    addButton = (Button) findViewById(R.id.addItems);
    addButton.setOnClickListener(this);
    editText = (EditText) findViewById(R.id.itmName);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,itmList);
    listView = (ListView) findViewById(R.id.itmList);
    listView.setAdapter(adapter);
    registerForContextMenu(listView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    //return super.onContextItemSelected(item);
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    switch (item.getItemId()){
        case R.id.editItem:
            //
            return true;
        case R.id.deleteItem:
            adapter.remove(adapter.getItem(info.position));
            Toast.makeText(this, "Item Deleted", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

public void onClick(View view){
    String input = editText.getText().toString();
    if(input.length() > 0){
        adapter.add(input);
    }
}
}

Aucun commentaire:

Enregistrer un commentaire