vendredi 10 avril 2015

Insert ListView with setOnItemClickListener in Database

I want to insert data from a EditText, in a ListView into a SQLite database. The Items have a Button, so I have already tried to fixed (like: there)with



android:focusable="false"


but the setOnItemClickListener not work.


I have also thought to take the function onTextChanged from the ActivitiesListAdapter, but I don't know how I get access to the DatabaseHelper Object in my MainActivity.


The function setupActivityListViewAdapter in MainActivity:



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();
}
});
}


And setOnItemClickListener do not work? My item_activities.xml:



<LinearLayout xmlns:android="http://ift.tt/nIICcg"
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>


And I have this function in my 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) { }
});
}


So how I can manipulate the DataBaseHelper Object from a other class, the class ActivitiesListAdapter? And it is better to give objects single to the DataBaseHelper and not as ArrayList (I think?), so how we solve the problem.


Thanks and Kind regards.


Aucun commentaire:

Enregistrer un commentaire