You are on page 1of 5

// MainActivity.

java

package com.mycompany.mark;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity


{

String listItem[] = {};


MyDbHandler dbHandler;
int i = 0;
String m = "<<>>";
static int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHandler = new MyDbHandler(this, null, null, 1);

List values = new ArrayList();


for (int i = 0; i < listItem.length; i++)
{
values.add(listItem[i]);
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,


android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
//*****///
ListView lv = getListView();
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String food =
String.valueOf(parent.getItemAtPosition(position));

Toast.makeText(MainActivity.this, food,
Toast.LENGTH_SHORT).show();

Intent i1 = new Intent(MainActivity.this, Third.class);


String s = food;
i1.putExtra("m1", s);
startActivity(i1);
}
}

);

lv.setOnItemLongClickListener(
new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View
view, int position, long id)
{
final String food =
String.valueOf(parent.getItemAtPosition(position));

//Toast.makeText(MainActivity.this, food+"long",
Toast.LENGTH_SHORT).show();

AlertDialog.Builder alert = new


AlertDialog.Builder(MainActivity.this);
alert.setTitle("Alert!!");
alert.setMessage("Are you sure to delete record");
alert.setPositiveButton("YES", new
DialogInterface.OnClickListener()
{

@Override
public void onClick(DialogInterface dialog, int
which)
{
Toast.makeText(MainActivity.this, "long",
Toast.LENGTH_SHORT).show();
deletereminder(food);
deleteservice(food);
dialog.dismiss();

}
});
alert.setNegativeButton("NO", new
DialogInterface.OnClickListener()
{

@Override
public void onClick(DialogInterface dialog, int
which)
{

dialog.dismiss();
}
});

alert.show();
return true;
}
}

);
if (count == 0)
show();
count++;
Bundle received_data = getIntent().getExtras();
if (received_data == null)
return;
m = received_data.getString("m1");
dothings();

void deletereminder(String str)


{
String mar = str;
dbHandler.deleteProduct(mar);
bulldog();
deleteall();
show();
}

void deleteservice(String str)


{
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("string_id", str);
editor.commit();
}
void deleteall()
{
List values1 = new ArrayList();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values1);
setListAdapter(adapter1);
adapter1.notifyDataSetChanged();
}

void show()
{
Products p1 = dbHandler.print();
String str = p1.get_name();
i = 0;
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
List myList = new ArrayList();
for (String parts : str.split("#"))
{
if (i != 0)
{
if (p1.get_name() != null)
myList.add(parts);
adapter.add(parts);
adapter.notifyDataSetChanged();
}
++i;
}

public void bulldog()


{
Products p1 = dbHandler.print();

ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();

void dothings()
{

String res1 = m.substring(m.indexOf("~") + 1, m.indexOf("!"));


String res2 = m.substring(m.indexOf("!") + 1, m.indexOf("^"));
String res3 = m.substring(m.indexOf("^") + 1, m.indexOf("<"));
String res4 = m.substring(m.indexOf("<") + 1, m.indexOf(">"));

Products p = new Products(res1, res2, res3, res4);


dbHandler.addProduct(p);
show();
}
public void onClick(View view)
{

switch (view.getId())
{
case R.id.addItem:

Intent i = new Intent(this, Second.class);


startActivity(i);

break;
/*case R.id.exit:
//finish();
List values1 = new ArrayList();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values1);
setListAdapter(adapter1);
adapter.notifyDataSetChanged();
break;*/
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is
present.
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}