0

I have ListView in which in each row it has a textview and a delete button. The values for the list is from the sqlite database. I have successfully populate list from the database. My problem is that i do not know how to correctly implement the onClickItemListener for the textview and delete button. What i want is that when i click the textview, it will go another intent (based on its specific id from the database). And if i click the delete button, it will delete the item.

This is my code:

ListView listView ;
ArrayList<String> list;

public int goal_id;
int i = 0;

//database variables
MyDBAdapter dbhandler;


protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_act);
        TypefaceProvider.registerDefaultIconSets();

        Bundle extras = getIntent().getExtras();

        if (extras == null) {
            return;
        }

        goal_id = Integer.parseInt(extras.getString("goalid"));

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setSubtitle("Activities List");
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.ic_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Added new activity", Snackbar.LENGTH_LONG)
                        .setAction("Added new activity", null).show();

                onCLick_addAct();
            }
        });

        dbhandler = new MyDBAdapter(this);
        populateListView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Long temp = listView.getItemIdAtPosition(position);
                MessageTo.message(SetActActivity.this, Long.toString(temp));
            }
        });

    }

    public void populateListView(){
        Cursor cursor = dbhandler.getAllActivitiesByGoalCursor(goal_id);
        String[] actlist = new String[] {dbhandler.dbhandler.COLUMN_ACTIVITY_NAME};
        int[] actNames = new int[] {R.id.list_item_string};

        SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(SetActActivity.this,R.layout.act_list,cursor,actlist,actNames,0);
        //handle listview and assign adapter
        listView = (ListView)findViewById(R.id.list);
        // Attach cursor adapter to the ListView
        listView.setAdapter(myAdapter);

    }

 public void onCLick_addAct(){
        i++;
        //to create activities
        final Activities act = new Activities(goal_id,"Activity " + i, 0,1,1,1,0, false,0,0,0,0);
        long temp = dbhandler.createActivity(act);
        String temp2 = Long.toString(temp);
        final int act_id = Integer.parseInt(temp2);
        populateListView();
    }

act_list.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:clickable="false"
        android:focusable="false"
        android:id="@+id/list_item_string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="15dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_row="0"
        android:layout_column="0"
        android:paddingRight="30dp" />

    <Button
        android:clickable="false"
        android:focusable="false"
        android:id="@+id/delete_btn"
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:layout_row="0"
        android:layout_column="1"
        android:background="@drawable/ic_delete" />

</GridLayout>
Ruby
  • 241
  • 1
  • 6
  • 14
  • In your custom adapter class getview method implements onclicklistener for both textview and delete button.... – Nik Jan 14 '16 at 02:31
  • I didnt use custom adapter. I use the SimpleCursorAdapter so that i could populate the list with database values @user1259670 – Ruby Jan 14 '16 at 02:33

1 Answers1

0

https://stackoverflow.com/a/7047141

See this post.

Xml also has onclick property. So try using it.

Community
  • 1
  • 1
Keshav
  • 577
  • 3
  • 10
  • i am using the SimpleCursorAdapter. Its a predefined adapter class for those who wants to populate the list with database values. I cant edit that. I didn't use a custom adapter cause it wont populate the list with database values. – Ruby Jan 14 '16 at 02:35
  • i couldn't access the getView() method on my adapter because its not a custom adapter. – Ruby Jan 14 '16 at 02:50
  • Use the xml property onclick in your list row items and then access them. See the link. – Keshav Jan 14 '16 at 02:51
  • @ruby See the new link in the answer. I posted the wrong link last time. – Keshav Jan 14 '16 at 03:14
  • i am still confused on what u trying to say. – Ruby Jan 14 '16 at 03:42
  • i already know about onClick. what i am confused about is that if it wont distinguish whether i clicked the textview or the delete button. if i click either the textview or delete button -> it will implement this -> listView.setOnItemClickListener(). I cannot use the Onclick method u suggested because each row is unique. For example if i click TextView1 , it will go to textView1 details. and textView2 will go to textVIew2 details. – Ruby Jan 14 '16 at 03:59
  • i tried the other onClick implementation. and it ended up in errors. – Ruby Jan 14 '16 at 04:00