Advertisement
hornix

delete list item

Jan 29th, 2012
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package com.horror.android;
  5.  
  6. import java.io.IOException;
  7. import java.util.Arrays;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import android.app.Activity;
  11. import android.app.AlertDialog;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.content.Intent;
  15. import android.content.SharedPreferences;
  16. import android.database.Cursor;
  17. import android.database.sqlite.SQLiteDatabase;
  18. import android.graphics.Color;
  19. import android.graphics.drawable.ColorDrawable;
  20. import android.os.Bundle;
  21. import android.util.Log;
  22. import android.util.SparseBooleanArray;
  23. import android.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.view.View.OnClickListener;
  27. import android.widget.AdapterView;
  28. import android.widget.BaseAdapter;
  29. import android.widget.Button;
  30. import android.widget.CheckBox;
  31. import android.widget.CheckedTextView;
  32. import android.widget.ImageView;
  33. import android.widget.ListAdapter;
  34. import android.widget.ListView;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37. import android.widget.AdapterView.OnItemClickListener;
  38.  
  39. public class BookmarksJokes extends Activity implements OnClickListener,
  40. OnItemClickListener {
  41. ListView lv;
  42. protected ListAdapter adapter;
  43. TextView title;
  44.  
  45. ImageView img_home;
  46. Button btn_delete;
  47. public String TAG = "horror";
  48. private SQLiteDatabase db;
  49.  
  50. public static final String PREFS_NAME = "MyPreferences";
  51. static String[] tempTitle = new String[100];
  52. static String[] tempBody = new String[100];
  53. static String[] pos = new String[100];
  54. private static boolean bRequiresResponse;
  55.  
  56. private static class EfficientAdapter extends BaseAdapter {
  57. private LayoutInflater mInflater;
  58.  
  59. public EfficientAdapter(Context context) {
  60. mInflater = LayoutInflater.from(context);
  61.  
  62. }
  63.  
  64. public int getCount() {
  65. return tempTitle.length;
  66. }
  67.  
  68. public Object getItem(int position) {
  69. return position;
  70. }
  71.  
  72. public long getItemId(int position) {
  73. return position;
  74. }
  75.  
  76. public View getView(int position, View convertView, ViewGroup parent) {
  77. ViewHolder holder;
  78. if (convertView == null) {
  79. convertView = mInflater.inflate(R.layout.bookmarks_list_item,
  80. null);
  81. holder = new ViewHolder();
  82. holder.text1 = (TextView) convertView
  83. .findViewById(R.id.titleJok);
  84. holder.text2 = (TextView) convertView
  85. .findViewById(R.id.bodyJok);
  86. // holder.checkBox = (CheckBox)
  87. // convertView.findViewById(R.id.checkbox);
  88. holder.checkBox1 = (CheckedTextView) convertView
  89. .findViewById(android.R.id.checkbox);
  90.  
  91. convertView.setTag(holder);
  92. } else {
  93. holder = (ViewHolder) convertView.getTag();
  94. }
  95.  
  96. holder.text1.setText(tempTitle[position]);
  97. holder.text2.setText(tempBody[position]);
  98. // bRequiresResponse = checkBox.isChecked();
  99. // holder.checkBox1.setText(tempTitle[position]);
  100. // holder.checkBox1.setText(tempBody[position]);
  101. /*
  102. * holder.checkBox1.setTextSize(12);
  103. * holder.checkBox1.setTextColor(Color.YELLOW);
  104. */
  105.  
  106. return convertView;
  107. }
  108.  
  109. static class ViewHolder {
  110. TextView text1;
  111. TextView text2;
  112. CheckBox checkBox;
  113. private CheckedTextView checkBox1;
  114.  
  115. }
  116. }
  117.  
  118. @Override
  119. protected void onCreate(Bundle savedInstanceState) {
  120.  
  121. super.onCreate(savedInstanceState);
  122. setContentView(R.layout.bokmarksjoks);
  123.  
  124. try {
  125. db = (new DatabaseHelper(this)).getWritableDatabase();
  126. } catch (IOException e) {
  127. e.printStackTrace();
  128. }
  129. setUpViews();
  130.  
  131. title = (TextView) findViewById(R.id.body);
  132. SharedPreferences pref = getSharedPreferences(PREFS_NAME, 0);
  133.  
  134. String ids = pref.getString("jid", "");
  135. String one = pref.getString("title", "");
  136. String two = pref.getString("body", "");
  137.  
  138. tempTitle = one.split(",");
  139. tempBody = two.split(",");
  140.  
  141. lv.setAdapter(new EfficientAdapter(this));
  142.  
  143. lv.setItemsCanFocus(false);
  144. lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  145.  
  146. lv.setOnItemClickListener(this);
  147. ColorDrawable divcolor = new ColorDrawable(Color.WHITE);
  148. lv.setDivider(divcolor);
  149. lv.setDividerHeight(3);
  150.  
  151. }
  152.  
  153. private void setUpViews() {
  154. lv = (ListView) findViewById(R.id.list);
  155. img_home = (ImageView) findViewById(R.id.bmhome);
  156. img_home.setOnClickListener(this);
  157. btn_delete = (Button) findViewById(R.id.delete);
  158. btn_delete.setOnClickListener(this);
  159. // checkbox = (CheckBox) findViewById(R.id.checkbox);
  160.  
  161. }
  162.  
  163. private void removeJok() {
  164. /*
  165. * int pos= getIntent().getIntExtra("POSITION", 0);
  166. * lv.removeViewAt(pos); notifyAll();
  167. */
  168. int pos = getIntent().getIntExtra("POSITION", 0);
  169. /*
  170. * if(checkBox.isChecked()){ Log.d(TAG, " checked d]"+pos);
  171. * Toast.makeText(this, "checked "+pos, Toast.LENGTH_SHORT).show(); }
  172. */
  173. }
  174.  
  175. public void onClick(View v) {
  176. switch (v.getId()) {
  177. case R.id.bmhome:
  178. BookmarksJokes.this.finish();
  179. break;
  180. case R.id.delete:
  181. AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
  182. alt_bld.setMessage("Are you Sure want to delete all checked jok ?")
  183. .setCancelable(false)
  184. .setPositiveButton("Yes",
  185. new DialogInterface.OnClickListener() {
  186. public void onClick(DialogInterface dialog,
  187. int id) {
  188. // removeJok();
  189.  
  190. }
  191. })
  192. .setNegativeButton("No",
  193. new DialogInterface.OnClickListener() {
  194. public void onClick(DialogInterface dialog,
  195. int id) {
  196. dialog.cancel();
  197. }
  198. });
  199. AlertDialog alert = alt_bld.create();
  200. alert.setTitle("Delete Jokes");
  201. alert.show();
  202. case R.id.checkbox:
  203.  
  204. default:
  205. break;
  206. }
  207.  
  208. }
  209.  
  210. public void onItemClick(AdapterView<?> arg0, View view, int position,
  211. long ids) {
  212.  
  213. try {
  214. SparseBooleanArray sp = lv.getCheckedItemPositions();
  215. /*String str = "";
  216. for (int i = 0; i < sp.size(); i++) {
  217. str += tempTitle[sp.keyAt(i)] + ",";
  218.  
  219. }*/
  220. Log.d(TAG, "pos " + position );
  221. Log.d(TAG, "str==" + str );
  222. System.out.println("str==" + str );
  223. Toast.makeText(this, "..."+str, Toast.LENGTH_SHORT).show();
  224. Intent intent = new Intent(BookmarksJokes.this,
  225. BookmarkJokesDetails.class);
  226.  
  227. intent.putExtra("POSITION", position);
  228. intent.putExtra("ID", ids);
  229. /*Cursor cursor = (Cursor) adapter.getItem(position);
  230.  
  231. intent.putExtra("JOK_ID",
  232. cursor.getInt(cursor.getColumnIndex("_id")));*/
  233.  
  234. startActivity(intent);
  235. } catch (Exception e) {
  236. e.printStackTrace();
  237. }
  238.  
  239. Toast.makeText(BookmarksJokes.this,
  240. "Item in position " + position + " clicked", Toast.LENGTH_LONG)
  241. .show();
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement