ListView OnClickItemListener使用textview和delete按钮

时间:2016-01-14 02:21:50

标签: android sqlite listview

我有ListView,其中每行都有一个textview和一个删除按钮。列表的值来自sqlite数据库。我已成功填充数据库中的列表。我的问题是我不知道如何正确实现textview和删除按钮的onClickItemListener。我想要的是,当我单击textview时,它将转向另一个意图(基于其来自数据库的特定id)。如果我单击删除按钮,它将删除该项目。

这是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/7047141

见这篇文章。

Xml也有onclick属性。所以尝试使用它。