2016-05-18 3 views
1

Ich habe eine Liste von Elementen, jedes Element hat einige Bilder und Textansichten. Ich habe viele Lösungen gegooglet, aber keine von ihnen hat mir geholfen. Ich bin sehr neu in Android-Entwicklung, ich denke, ich verliere etwas offensichtlich. Ich nehme auch an, dass ich vielleicht zu viele Lösungen ausprobiert habe und sie sich irgendwie blockieren. Bitte hilf mir. hier ist mein Code:Listview setOnItemClickListener funktioniert nicht

public class MyActivity extends Activity { 
private List<Lesson> lessons = new ArrayList<Lesson>(); 

private int loop_count = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.my_layout); 

    FillLessonList(); 
    FillListView(); 
} 
    private void FillListView() 
{ 
    ArrayAdapter<Lesson> adapter = new MyListAdapter(); 
    ListView list = (ListView) findViewById(R.id.lessonsList); 
    list.setAdapter(adapter); 

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name)); 

      Toast.makeText(getApplicationContext(), "Your toast message.", 
        Toast.LENGTH_SHORT).show(); 

      Intent intent = new Intent(view.getContext(), LessonActivity.class); 
      startActivity(intent); 
     } 
    }); 
} 

private class MyListAdapter extends ArrayAdapter<Lesson> 
{ 
    MyListAdapter() 
    { 
     super(MyActivity.this, R.layout.lesson_item, lessons); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View lessonView = convertView; 
     if(lessonView == null) 
     { 
      lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false); 
     } 

     Lesson currentLesson = lessons.get(position); 

     //here I add all the needed info into the item. 
     //This part works fine and displays everything I need properly. 

     return lessonView; 
    } 

Das einzige, was wirklich mit jedem Klick wird protokolliert ist:

05-18 17:10:57.158 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch down: touch mode = 0,mScrollY = 0,y = 122.7923,mFirstPosition = 0,mActivePointerId = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList} 
    05-18 17:10:57.258 29101-29101/h2d.drawteam.howtodraw D/AbsListView: CheckForTap:mFirstPosition = 0,mMotionPosition = 0,child = android.widget.RelativeLayout{3506a91f V.E..... ........ 0,0-960,225} 
    05-18 17:10:57.293 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch up: touch mode = 1,mScrollY = 0,mLastY = -2147483648,mMotionPosition = 0,mFirstPosition = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList} 

Hier ist meine Haupt-Layout XML-Datei:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/bg_clear" 
android:orientation="vertical" 
android:paddingTop="10dp" 
android:paddingBottom="10dp"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/container_button_back" 
    android:paddingRight="25dp" 
    android:paddingLeft="25dp" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:weightSum="1"> 

    <Button 
     android:layout_width="64dp" 
     android:layout_height="31dp" 
     android:id="@+id/button_back" 
     android:background="@drawable/back_button_fit" 
     android:maxHeight="50dp" 
     android:maxWidth="80dp" 
     android:adjustViewBounds="true" 
     android:longClickable="false" 
     android:enabled="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:singleLine="false" /> 

</LinearLayout> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:src="@drawable/separator_1" /> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/container_list" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:paddingBottom="5dp"> 

    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/lessonsList" 
     android:dividerHeight="0dp" 
     android:divider="@null" 
     android:descendantFocusability="blocksDescendants"/> 
</LinearLayout> 

Und das ist meine Liste Artikel XML-Datei:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:clickable="false" 
android:paddingLeft="0dp" 
android:paddingRight="10dp"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/lesson_icon" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:src="@drawable/abc_btn_check_material" 
    android:minHeight="75dp" 
    android:minWidth="75dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 



<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="@string/default_lesson_name" 
    android:id="@+id/lesson_text_lesson_name" 
    android:layout_marginTop="17dp" 
    android:layout_alignParentTop="true" 
    android:textSize="18sp" 
    android:maxLines="1" 
    android:layout_toLeftOf="@+id/lesson_rate" 
    android:layout_toRightOf="@+id/lesson_icon" 
    android:layout_toEndOf="@+id/lesson_icon" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="@string/default_steps" 
    android:id="@+id/lesson_text_steps" 
    android:layout_below="@+id/lesson_text_lesson_name" 
    android:layout_toRightOf="@+id/lesson_icon" 
    android:layout_toEndOf="@+id/lesson_icon" 
    android:textSize="12sp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 

<ImageButton 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/lesson_rate" 
    android:src="@drawable/rate1" 
    android:baselineAlignBottom="false" 
    android:background="#00000000" 
    android:layout_above="@+id/lesson_text_steps" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="false" 
    android:paddingRight="10dp" 
    android:adjustViewBounds="true" 
    android:cropToPadding="false" 
    android:scaleType="centerInside" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 

<ImageButton 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/lesson_separator" 
    android:src="@drawable/separator" 
    android:background="#00000000" 
    android:adjustViewBounds="true" 
    android:layout_alignBottom="@+id/lesson_icon" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:clickable="false"/> 

+0

Protokolle werden auf Klicks ausgeführt, also was genau funktioniert nicht? – user1930106

+0

android: demcendantFocusability = "blocksDescendants" entfernen Sie diese Zeile aus der Listenansicht und versuchen Sie es.Hoffe es funktioniert. –

+0

@Samstone versuchen Toast-Nachricht auch anstelle von Protokoll – Dhiraj

Antwort

1

versuchen, diese

list.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
             @Override 
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name)); 

      Toast.makeText(getApplicationContext(), "Your toast message.", 
        Toast.LENGTH_SHORT).show(); 

      Intent intent = new Intent(view.getContext(), LessonActivity.class); 
      startActivity(intent); 

} 
}); 

oder

list.setOnItemClickListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name)); 

     Toast.makeText(getApplicationContext(), "Your toast message.", 
       Toast.LENGTH_SHORT).show(); 

     Intent intent = new Intent(view.getContext(), LessonActivity.class); 
     startActivity(intent); 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

Alternative Methode

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View lessonView = convertView; 
     if(lessonView == null) 
     { 
      lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false); 
     } 

     Lesson currentLesson = lessons.get(position); 

     //here I add all the needed info into the item. 
     //This part works fine and displays everything I need properly. 

     lessonView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.e(""testing123,"testing"); 
      Toast.makeText(MyActivity.this, "Your toast message.", 
       Toast.LENGTH_SHORT).show(); 

     Intent intent = new Intent(MyActivity.this, LessonActivity.class); 
     startActivity(intent); 
     } 
    }); 

     return lessonView; 
    } 
+0

Androis Studio musste AdapterView.OnItemClickListener() hinzufügen, aber es funktioniert sowieso nicht. Vielleicht habe ich etwas falsch mit meinen Importen? –

+0

@SamStone was ist der Fehler? –

+0

"Kann das Symbol OnItemClickListener nicht auflösen" –

1

Da Sie einen benutzerdefinierten Adapter haben, müssen Sie die OnClickListener haben in Seite von getView(). Also, wenn Sie etwas Aktion möchten, wenn der Benutzer das ImageView-lection_icon sagt, dann möchten Sie vielleicht etwas wie schreiben:

lessonView.findViewById(R.id.lesson_icon).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(MainActivity.this, "Image clicked", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
+0

Ja, das war das Problem, Vishwash Jainkuniya löste das früher. Vielen Dank. –

+0

super ... Prost! – hrushi