mercredi 13 janvier 2016

ListView OnClickItemListener using textview and delete button

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://ift.tt/nIICcg"
    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>

Aucun commentaire:

Enregistrer un commentaire