Advertisement
Guest User

gdfg

a guest
Mar 26th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. // The full activity + custom cursor adapter
  2. public class CursorAdaptersClicks extends ListActivity {
  3.  
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         String[] columns = new String[] { "_id", "name" };
  8.         MatrixCursor mc = new MatrixCursor(columns);
  9.  
  10.         for (int i = 0; i < 32; i++) {
  11.             long id = i;
  12.             mc.addRow(new Object[] { id, "Name Here" + i });
  13.         }
  14.         setListAdapter(new CustomCursorAdapter(this, mc, true));
  15.     }
  16.  
  17.     @Override
  18.     protected void onListItemClick(ListView l, View v, int position, long id) {
  19.         Toast.makeText(this, "You clicked on the " + position + "th row!",
  20.                 Toast.LENGTH_SHORT).show();
  21.     }
  22.  
  23.     class CustomCursorAdapter extends CursorAdapter {
  24.  
  25.         private LayoutInflater mInflater;
  26.         private Context ctx;
  27.  
  28.         public CustomCursorAdapter(Context context, Cursor c,
  29.                 boolean autoRequery) {
  30.             super(context, c, autoRequery);
  31.             mInflater = LayoutInflater.from(context);
  32.             ctx = context;
  33.         }
  34.        
  35.         @Override
  36.         public void bindView(View view, final Context context, Cursor cursor) {
  37.             ViewHolder holder = (ViewHolder) view.getTag();
  38.             String txt = cursor.getString(cursor.getColumnIndex("name"));
  39.             holder.text.setText(txt);
  40.             holder.button.setText(txt);
  41.             int currentPosition = cursor.getPosition();
  42.             holder.text.setTag(new Integer(currentPosition));
  43.             holder.button.setTag(new Integer(currentPosition));
  44.            
  45.             OnClickListener listener = new OnClickListener() {
  46.  
  47.                 @Override
  48.                 public void onClick(View v) {
  49.                     int thePosition = (Integer) v.getTag();
  50.                     if (v.getId() == R.id.button1) {
  51.                         Toast.makeText(ctx,
  52.                                 "You clicked the Button from the row "
  53.                                         + thePosition + "!", Toast.LENGTH_SHORT).show();
  54.                     } else if (v.getId() == R.id.textView1) {
  55.                         Toast.makeText(ctx,
  56.                                 "You clicked the TextView from the row "
  57.                                         + thePosition + "!", Toast.LENGTH_SHORT).show();
  58.                     }              
  59.                 }
  60.             };
  61.             holder.text.setOnClickListener(listener);
  62.             holder.button.setOnClickListener(listener);
  63.         }
  64.  
  65.         @Override
  66.         public View newView(Context context, Cursor cursor, ViewGroup parent) {
  67.             View v = mInflater.inflate(R.layout.adapters_cursor_adapter_clicks,
  68.                     null);
  69.             ViewHolder holder = new ViewHolder();
  70.             holder.text = (TextView) v.findViewById(R.id.textView1);
  71.             holder.button = (Button) v.findViewById(R.id.button1);
  72.             v.setTag(holder);
  73.             return v;
  74.         }
  75.  
  76.         class ViewHolder {
  77.             TextView text;
  78.             Button button;
  79.         }
  80.  
  81.     }
  82.  
  83. }
  84.  
  85. //xml layout
  86. <?xml version="1.0" encoding="utf-8"?>
  87. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  88.     android:layout_width="match_parent"
  89.     android:layout_height="match_parent" >
  90.  
  91.  
  92.     <TextView
  93.         android:id="@+id/textView1"
  94.         android:layout_width="wrap_content"
  95.         android:layout_height="wrap_content"
  96.         android:layout_gravity="left" />
  97.  
  98.     <Button
  99.         android:id="@+id/button1"
  100.         android:layout_width="wrap_content"
  101.         android:layout_height="wrap_content"
  102.         android:layout_gravity="right"
  103.         android:focusable="false" />
  104.  
  105. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement