Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The full activity + custom cursor adapter
- public class CursorAdaptersClicks extends ListActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- String[] columns = new String[] { "_id", "name" };
- MatrixCursor mc = new MatrixCursor(columns);
- for (int i = 0; i < 32; i++) {
- long id = i;
- mc.addRow(new Object[] { id, "Name Here" + i });
- }
- setListAdapter(new CustomCursorAdapter(this, mc, true));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- Toast.makeText(this, "You clicked on the " + position + "th row!",
- Toast.LENGTH_SHORT).show();
- }
- class CustomCursorAdapter extends CursorAdapter {
- private LayoutInflater mInflater;
- private Context ctx;
- public CustomCursorAdapter(Context context, Cursor c,
- boolean autoRequery) {
- super(context, c, autoRequery);
- mInflater = LayoutInflater.from(context);
- ctx = context;
- }
- @Override
- public void bindView(View view, final Context context, Cursor cursor) {
- ViewHolder holder = (ViewHolder) view.getTag();
- String txt = cursor.getString(cursor.getColumnIndex("name"));
- holder.text.setText(txt);
- holder.button.setText(txt);
- int currentPosition = cursor.getPosition();
- holder.text.setTag(new Integer(currentPosition));
- holder.button.setTag(new Integer(currentPosition));
- OnClickListener listener = new OnClickListener() {
- @Override
- public void onClick(View v) {
- int thePosition = (Integer) v.getTag();
- if (v.getId() == R.id.button1) {
- Toast.makeText(ctx,
- "You clicked the Button from the row "
- + thePosition + "!", Toast.LENGTH_SHORT).show();
- } else if (v.getId() == R.id.textView1) {
- Toast.makeText(ctx,
- "You clicked the TextView from the row "
- + thePosition + "!", Toast.LENGTH_SHORT).show();
- }
- }
- };
- holder.text.setOnClickListener(listener);
- holder.button.setOnClickListener(listener);
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- View v = mInflater.inflate(R.layout.adapters_cursor_adapter_clicks,
- null);
- ViewHolder holder = new ViewHolder();
- holder.text = (TextView) v.findViewById(R.id.textView1);
- holder.button = (Button) v.findViewById(R.id.button1);
- v.setTag(holder);
- return v;
- }
- class ViewHolder {
- TextView text;
- Button button;
- }
- }
- }
- //xml layout
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="left" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:focusable="false" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement