//CREATE A CLASS
public class Inputdata {
private String title ="";
private String message ="";
public void setTitle(String title)
{
this.title=title;
}
public void setMessage(String message)
{
this.message=message;
}
public String getTitle() {
return (title);
}
public String getMessage() {
return (message);
}
// to convert result into string format
public String toString(){
return (getTitle());
}
}
//USE ARRAYLIST TO ADD VALUES
public class MyDiariesActivity extends TabActivity {
ArrayList<Inputdata> model = new ArrayList<Inputdata>();
// ArrayAdapter<Inputdata>adapter = null;
CustomDiaries adapter = null;
EditText et_title = null;
EditText et_desc = null;
Inputdata current = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_save = (Button) findViewById(R.id.btn_save);
ListView lv_diaries = (ListView) findViewById(R.id.diaries);
// adapter = new
// ArrayAdapter<Inputdata>(this,android.R.layout.simple_list_item_1,model);
adapter = new CustomDiaries();
lv_diaries.setAdapter(adapter);
// ---------------Setting Tabs in
// Main.xml----------------------------Starts here-----------
TabHost.TabSpec spec = getTabHost().newTabSpec("tag1");
spec.setContent(R.id.diaries);
spec.setIndicator("All Diaries",
getResources().getDrawable(R.drawable.alldiaries));
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Add Diaries",
getResources().getDrawable(R.drawable.categories));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
// -----------Setting Tabs in Main.xml-----------------Ends
// here-------------
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Inputdata UserData = new Inputdata();
current = new Inputdata();
et_title = (EditText) findViewById(R.id.et_title);
et_desc = (EditText) findViewById(R.id.et_describtion);
current.setTitle(et_title.getText().toString());
current.setMessage(et_desc.getText().toString());
adapter.add(current);
}
});
// -----When click the list--------------------starts here--
lv_diaries.setOnItemClickListener(onlistclick);
// -----When click the list--------------------ends here------------
}
// ------------creating a view of clicked list------------------Starts
// here---
private AdapterView.OnItemClickListener onlistclick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
current = model.get(position);
et_title.setText(current.getTitle());
et_desc.setText(current.getMessage());
getTabHost().setCurrentTab(1);
}
};
// --------------ends here---------------
//Create Options Menu---------Starts here--------------
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.option,menu);
return (super.onCreateOptionsMenu(menu));
}
//--------------ends here------------------------------
@Override
public boolean onOptionsItemSelected(MenuItem Item)
{
if(Item.getItemId()==R.id.toast)
{
String message="No Describtion selected";
if(current!=null){
message=current.getMessage();
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return (true);
}
return (super.onOptionsItemSelected(Item));
}
// ---------Dynamic List Items--------Starts here------------------
class CustomDiaries extends ArrayAdapter<Inputdata> {
CustomDiaries() {
super(MyDiariesActivity.this, android.R.layout.simple_list_item_1,
model);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.diarylist, null);
}
Inputdata input = model.get(position);
((TextView) row.findViewById(R.id.title)).setText(input.getTitle());
((TextView) row.findViewById(R.id.message)).setText(input
.getMessage());
return row;
}
}
//////MORE ENCAPSULATION/////////////////////////
public View getView(int position,View ConvertView,ViewGroup parent) {
View row = convertView;
MydiariesHolder holder = null;
if(row==null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.diarylist, parent, false);
holder = new MydiariesHolder(row);
//list.setItemsCanFocus(true);
row.setTag(holder);
}
else
{
holder=(MydiariesHolder)row.getTag();
}
holder.populateFrom(model.get(position));
return (row);
}
}
static class MydiariesHolder {
private TextView title = null;
private TextView message = null;
private ImageView icon = null;
private TextView Created_date =null;
private Button btn_delete=null;
//------------To display it in dairylist.xml--------------
MydiariesHolder(View row) {
title = (TextView) row.findViewById(R.id.title123);
message = (TextView) row.findViewById(R.id.message123);
Created_date=(TextView)row.findViewById(R.id.tv_date);
//btn_delete =(Button)row.findViewById(R.id.btn_delete);
}
void populateFrom(InputData data) {
Log.e("checkkkk", "Inside populate from");
title.setText(data.getTitle());
message.setText(data.getMessage());
Created_date.setText(data.getDate());
/*btn_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("button click", "CLick");
}
});*/
}
public class Inputdata {
private String title ="";
private String message ="";
public void setTitle(String title)
{
this.title=title;
}
public void setMessage(String message)
{
this.message=message;
}
public String getTitle() {
return (title);
}
public String getMessage() {
return (message);
}
// to convert result into string format
public String toString(){
return (getTitle());
}
}
//USE ARRAYLIST TO ADD VALUES
public class MyDiariesActivity extends TabActivity {
ArrayList<Inputdata> model = new ArrayList<Inputdata>();
// ArrayAdapter<Inputdata>adapter = null;
CustomDiaries adapter = null;
EditText et_title = null;
EditText et_desc = null;
Inputdata current = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn_save = (Button) findViewById(R.id.btn_save);
ListView lv_diaries = (ListView) findViewById(R.id.diaries);
// adapter = new
// ArrayAdapter<Inputdata>(this,android.R.layout.simple_list_item_1,model);
adapter = new CustomDiaries();
lv_diaries.setAdapter(adapter);
// ---------------Setting Tabs in
// Main.xml----------------------------Starts here-----------
TabHost.TabSpec spec = getTabHost().newTabSpec("tag1");
spec.setContent(R.id.diaries);
spec.setIndicator("All Diaries",
getResources().getDrawable(R.drawable.alldiaries));
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Add Diaries",
getResources().getDrawable(R.drawable.categories));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
// -----------Setting Tabs in Main.xml-----------------Ends
// here-------------
btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Inputdata UserData = new Inputdata();
current = new Inputdata();
et_title = (EditText) findViewById(R.id.et_title);
et_desc = (EditText) findViewById(R.id.et_describtion);
current.setTitle(et_title.getText().toString());
current.setMessage(et_desc.getText().toString());
adapter.add(current);
}
});
// -----When click the list--------------------starts here--
lv_diaries.setOnItemClickListener(onlistclick);
// -----When click the list--------------------ends here------------
}
// ------------creating a view of clicked list------------------Starts
// here---
private AdapterView.OnItemClickListener onlistclick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
current = model.get(position);
et_title.setText(current.getTitle());
et_desc.setText(current.getMessage());
getTabHost().setCurrentTab(1);
}
};
// --------------ends here---------------
//Create Options Menu---------Starts here--------------
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
new MenuInflater(this).inflate(R.menu.option,menu);
return (super.onCreateOptionsMenu(menu));
}
//--------------ends here------------------------------
@Override
public boolean onOptionsItemSelected(MenuItem Item)
{
if(Item.getItemId()==R.id.toast)
{
String message="No Describtion selected";
if(current!=null){
message=current.getMessage();
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return (true);
}
return (super.onOptionsItemSelected(Item));
}
// ---------Dynamic List Items--------Starts here------------------
class CustomDiaries extends ArrayAdapter<Inputdata> {
CustomDiaries() {
super(MyDiariesActivity.this, android.R.layout.simple_list_item_1,
model);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.diarylist, null);
}
Inputdata input = model.get(position);
((TextView) row.findViewById(R.id.title)).setText(input.getTitle());
((TextView) row.findViewById(R.id.message)).setText(input
.getMessage());
return row;
}
}
//////MORE ENCAPSULATION/////////////////////////
public View getView(int position,View ConvertView,ViewGroup parent) {
View row = convertView;
MydiariesHolder holder = null;
if(row==null)
{
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.diarylist, parent, false);
holder = new MydiariesHolder(row);
//list.setItemsCanFocus(true);
row.setTag(holder);
}
else
{
holder=(MydiariesHolder)row.getTag();
}
holder.populateFrom(model.get(position));
return (row);
}
}
static class MydiariesHolder {
private TextView title = null;
private TextView message = null;
private ImageView icon = null;
private TextView Created_date =null;
private Button btn_delete=null;
//------------To display it in dairylist.xml--------------
MydiariesHolder(View row) {
title = (TextView) row.findViewById(R.id.title123);
message = (TextView) row.findViewById(R.id.message123);
Created_date=(TextView)row.findViewById(R.id.tv_date);
//btn_delete =(Button)row.findViewById(R.id.btn_delete);
}
void populateFrom(InputData data) {
Log.e("checkkkk", "Inside populate from");
title.setText(data.getTitle());
message.setText(data.getMessage());
Created_date.setText(data.getDate());
/*btn_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("button click", "CLick");
}
});*/
}
0 comments: