Thursday, December 1, 2011

ADD ELEMENT IN THE LIST AND REMOVE IT BY CLICKING IT

First of all make an XML file called listadapter.xml file and insert the following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="230dp" 
android:layout_height="wrap_content" 
android:id="@+id/myEditText"/>
<Button android:text="Add"
android:id = "@+id/Add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background = "@drawable/button_shape"
/>
</LinearLayout>
<ListView
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/MyListView">
</ListView>
</LinearLayout>



AND create the class file called ListAdapter.java and insert the following into it



import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import android.widget.Toast;


public class ListAdapter extends Activity {
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       Toast.makeText(ListAdapter.this, "Redirecting to List Activity", 400000).show();
     //  TextView textview = new TextView(this);
     //  textview.setText("This is the Artists tab");
       setContentView(R.layout.listadapter);
       Button bt = (Button)findViewById(R.id.Add); 
       ListView lv = (ListView)findViewById(R.id.MyListView);
       final EditText myEditText= (EditText)findViewById (R.id.myEditText);
       final ArrayList<String> videolinks = new ArrayList<String>();
       final ArrayAdapter<String> aa;
       
       aa= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,videolinks);
       
       lv.setAdapter(aa);
       
       bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(myEditText.getText().length() != 0)
{
videolinks.add(0, myEditText.getText().toString().trim());
aa.notifyDataSetChanged();
myEditText.setText("");
}
}
       });
       lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
AlertDialog.Builder ad  = new AlertDialog.Builder(ListAdapter.this);
ad.setTitle("Delete?");
ad.setMessage("Are you sure you want to delete " + arg2);
final int positionToRemove = arg2;
ad.setNegativeButton("Cancel", null);
ad.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) 
{
               videolinks.remove(positionToRemove);
               aa.notifyDataSetChanged();
       }
});
ad.show();
}
});
  }
}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.