在数据库中使用 setOnItemClickListener 插入 ListView
Insert ListView with setOnItemClickListener in Database
我想将数据从 ListView 中的 EditText 插入到 SQLite 数据库中。这些项目有一个按钮,所以我已经尝试用
修复(如: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) { }
});
}
那么如何从另一个 class、class ActivitiesListAdapter 操作 DataBaseHelper 对象?
并且最好将对象单独提供给 DataBaseHelper 而不是 ArrayList(我认为?),那么我们如何解决这个问题。
谢谢和亲切的问候。
您不能将文本写入项目 EditText
并同时使 setOnItemClickListener
工作。因此,您应该为适配器内部的按钮调用 setOnClickListener
,并从 setOnItemClickListener
.
进行所有操作
我想将数据从 ListView 中的 EditText 插入到 SQLite 数据库中。这些项目有一个按钮,所以我已经尝试用
修复(如: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) { }
});
}
那么如何从另一个 class、class ActivitiesListAdapter 操作 DataBaseHelper 对象? 并且最好将对象单独提供给 DataBaseHelper 而不是 ArrayList(我认为?),那么我们如何解决这个问题。
谢谢和亲切的问候。
您不能将文本写入项目 EditText
并同时使 setOnItemClickListener
工作。因此,您应该为适配器内部的按钮调用 setOnClickListener
,并从 setOnItemClickListener
.