- Forum-Beiträge: 609
06.11.2019, 18:13:30 via Website
06.11.2019 18:13:30 via Website
Hallo zusammen
Ich komme in einem Problem nicht mehr weiter. In meiner Activity habe ich eine ListView mit einem Adapter verknüpft. Wenn ich auf eine Zeile klicke soll eine weitere Activity geöffnet werden. Aber die Methode vom OnItemClickListener wird nie aufgerufen.
Meine Activity:
package ch.robbisoft.sakristanhilfe;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
public class eingabe extends Activity {
public static final String APPNAME = "SAKRISTAN";
public static final String FELDNAME = "TEXT";
public static final String DATNAME = "STRDAT";
private Context ctx;
private Button btn_speichern;
private TextView edt_datum;
private ListView liste;
private listAdapter adapter;
private List<Eintrag> EintragListe;
private Button btn_suche;
private Button btn_minus;
private Button btn_plus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eingabe);
ctx = getApplication();
//macht das die Tastatur nicht gliech angezeigt wird
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
EintragListe = new ArrayList<Eintrag>();
inital();
edt_datum = (TextView) findViewById(R.id.edt_datum);
edt_datum.setText(get_datum());
liste = (ListView) findViewById(R.id.livi_text);
adapter = new listAdapter(ctx, 0);
liste.setAdapter(adapter);
//Liste aufbauen
adapter.notifyDataSetInvalidated();
//Klick Ereignisse
liste.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("sakristanhilfe", "LongClickEreignis");
//Langes Klicken auf ein Feld in der ListView
return true;
}
});
liste.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("sakristanhilfe", "ClickEreignis");
startActivity(new Intent(ctx, work.class));
}
});
btn_speichern = (Button) findViewById(R.id.btn_speichern);
btn_speichern.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btn_suche = (Button) findViewById(R.id.btn_suchen);
btn_suche.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btn_plus = (Button) findViewById(R.id.btn_minus);
btn_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickMinus();
}
});
}
/**********************************************************************************************
* Löscht einen Eintrag aus der Stringliste
*
* @autor robbisoft @version 19. Februar 2019
* @param
* @return
**********************************************************************************************/
private void clickMinus() {
try {
int n_pos = 1;
String str_text;
// str_text = stringListe.get(n_pos);
// n_pos = stringListe.indexOf((String) str_text);
// str_text = adapter.getItem(n_pos);
// Log.d((String) str_text);
// n_pos = adapter.getPosition("Eintrag 2");
}catch (Exception e){
// e.getMessage();
}
}
/**********************************************************************************************
* Nimt die initialisierung des Fensters vor
*
* @autor robbisoft @version 9. Februar 2019
* @param
* @return
**********************************************************************************************/
private void inital(){
Date jetzt;
String str_jetzt;
Calendar zeitstempel = Calendar.getInstance();
SimpleDateFormat datumsformat = new SimpleDateFormat("d. MMMM yyyy");
jetzt = zeitstempel.getTime();
str_jetzt = datumsformat.format(jetzt);
Eintrag eingabe = new Eintrag();
eingabe.dat_datum = jetzt;
eingabe.str_text = "Eintrag Nummer 1";
EintragListe.add(eingabe);
}
/**********************************************************************************************
* Ermittelt das aktuelle Datum und gibt es als String zurück
*
* @autor robbisoft @version 2.02.2019
* @param
* @return aktuelles Datum als String
**********************************************************************************************/
private String get_datum(){
String d_jetzt;
//Zeitermitteln und ausgeben
Calendar zeitstempel = Calendar.getInstance();
SimpleDateFormat datumsformat = new SimpleDateFormat("d. MMMM yyyy");
d_jetzt = datumsformat.format(zeitstempel.getTime());
return d_jetzt;
}
/**********************************************************************************************
* Legt einen Eintrag in den Speicher
*
* @autor robbisoft @version 13.02.2019
* @param "String zum Speichern"
* @return
**********************************************************************************************/
private void speichern(List<String> d_datum, List<String> str_text){
SharedPreferences pref = getSharedPreferences(APPNAME, 0);
SharedPreferences.Editor edit = pref.edit();
edit.putStringSet(DATNAME, (Set<String> d_datum);
edit.putStringSet(FELDNAME, (Set<String> str_text);
edit.commit();
}
/**********************************************************************************************
* Liest einen Eintrag aus dem Speicher
*
* @autor robbisoft @version 10.02.2019
* @param "String zum Speichern"
* @return
**********************************************************************************************/
private List<String> lesen(){
List<String> str_text;
SharedPreferences pref = getSharedPreferences(APPNAME, 0);
// str_text = pref.getString(FELDNAME, "Fehler");
str_text = (List) pref.getStringSet(FELDNAME, null);
return str_text;
}
/**********************************************************************************************
* Adapter um die Besonderheiten einzugeben
*
* @autor robbisoft @version 6. November 2019
* @param "Liste der Besonderheitn"
* @return
**********************************************************************************************/
class listAdapter extends ArrayAdapter<Eintrag>{
public listAdapter( Context context, int resource) {
super(context, resource);
}
@Override
public int getCount(){
int n_anzahl = EintragListe.size();
return n_anzahl;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = getLayoutInflater().inflate(R.layout.list_element, null);
}
Eintrag eintrag;
eintrag = EintragListe.get(position);
TextView datum = (TextView) convertView.findViewById(R.id.edt_datum);
datum.setText(eintrag.dat_datum.toString());
TextView text = (TextView) convertView.findViewById(R.id.edt_text);
text.setText(eintrag.str_text);
return convertView;
}
}
}
Das Layout dazu:
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lay_eingabeElemente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".eingabe">
<LinearLayout
android:id="@+id/lay_oben"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edt_datum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:ems="10"
android:inputType="date" />
<Button
android:id="@+id/btn_suchen"
style="@style/Widget.AppCompat.Button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:background="@android:drawable/ic_search_category_default" />
</LinearLayout>
<ListView
android:id="@+id/livi_text"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:cacheColorHint="@color/colorTransparent" />
<LinearLayout
android:id="@+id/lay_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_minus"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_weight="1"
android:background="@android:drawable/ic_delete" />
<Button
android:id="@+id/btn_speichern"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:drawable/ic_menu_save" />
<Button
android:id="@+id/btn_plus"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="1"
android:background="@android:drawable/ic_input_add" />
</LinearLayout>
Das Layout vom Adapter
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/lay_eingabe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/edt_datum"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:ems="10"
android:inputType="date"
android:longClickable="false" />
<TextView
android:id="@+id/edt_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end" />
Warum wird die Methode nie aufgerufen? Was mache ich falsch?
Gruss Renato
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.