在数据库中使用setOnItemClickListener插入ListView

时间:2015-04-10 13:12:52

标签: android sqlite android-listview arraylist listener

我想将ListView中的EditText数据插入到SQLite数据库中。 Items有一个Button,所以我已经尝试使用

修复(例如:there
  android:focusable="false"

但是setOnItemClickListener不起作用。

我还想过从ActivitiesListAdapter获取onTextChanged函数,但我不知道如何访问MainActivity中的DatabaseHelper对象。

MainActivity中的函数setupActivityListViewAdapter:

private void setupActivityListViewAdapter() {
    activitiesListAdapter = new ActivitiesListAdapter(MainActivity.this, R.layout.item_activities, db.getAllActivityByDay_ID(selected_day.getDay_ID()));
    final ListView ActivitiesListView = (ListView)findViewById(R.id.listView_activities);
    ActivitiesListView.setAdapter(activitiesListAdapter);

    ActivitiesListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Toast.makeText(MainActivity.this,
                    "Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
        }
    });
}

并且setOnItemClickListener不起作用? 我的item_activities.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:inputType="numberDecimal"
        android:id="@+id/activity_hours"
        android:gravity="center_vertical"
        android:hint="@string/activity_hours_hint"
        android:layout_weight="0.10" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/hours_h"
        android:id="@+id/activity_hours_h"
        android:gravity="center_vertical"
        android:textStyle="normal" />

    <EditText
        android:layout_width="5sp"
        android:layout_height="fill_parent"
        android:id="@+id/activity_activity"
        android:inputType="text"
        android:layout_weight="2"
        android:layout_gravity="center"
        android:hint="@string/activity_activity_hint" />

    <ImageButton
        android:layout_width="0sp"
        android:layout_height="fill_parent"
        android:id="@+id/activity_delete"
        android:onClick="removeActivityOnClickHandler"
        android:contentDescription="@string/activity_delete"
        android:src="@android:drawable/ic_menu_delete"
        android:layout_weight="0.3"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

</LinearLayout>

我在ActivitiesListAdapter中有这个功能:

private void setHoursTextListeners(final ActivitiesHolder holder) {
    holder.hours.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try{
                holder.activities.setHours(Integer.valueOf(s.toString()));
            }catch (NumberFormatException e) {
                Log.e(LOG_TAG, "error reading activity hour: " + s.toString());
            }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void afterTextChanged(Editable s) { }
    });
}

那么我如何操作来自其他类的DataBaseHelper对象,即ActivitiesListAdapter类呢? 最好是将对象单独提供给DataBaseHelper而不是作为ArrayList(我认为?),这样我们如何解决问题。

谢谢和亲切的问候。

1 个答案:

答案 0 :(得分:0)

您可以将文字写入商品EditText,并使setOnItemClickListener同时发挥作用。因此,您应该拨打setOnClickListener以获取适配器内的按钮,并从setOnItemClickListener开始执行所有操作。