1

CustomAdapter.classリストビューsetonitemclicklistenerが動作していなくても、私は流れの上にスタック上で利用できるほとんどすべてのソリューションを適用

public class CustomAdapter extends ArrayAdapter<String> { 
    Context context; 
    int[] images; 
    String[] titleArray; 
    String[] descriptionArrayl; 
    public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl) { 
super(c,R.layout.row,R.id.etTitle,memeTitles); 
     this.context=c; 
     this.images = imgs; 
     this.titleArray = memeTitles; 
     this.descriptionArrayl=descriptionArrayl; 
    } 
    //Sub class of CustomAdapter 
    class MyViewHolder{ 
     ImageView myImage; 
     TextView myTitle; 
     TextView myDescription; 
     MyViewHolder(View v){ 

      myImage= (ImageView) v.findViewById(R.id.imageView); 
      myTitle = (TextView) v.findViewById(R.id.etTitle); 
      myDescription = (TextView) v.findViewById(R.id.etDescriptions); 
     } 
    } 

    //Get View Method of CustomAdapter class 
    public View getView(int position, View convertView, ViewGroup parent){ 
     View row=convertView; 
     MyViewHolder myViewHolder =null; 
     if (row==null) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.row, parent, false); 
      myViewHolder = new MyViewHolder(row); 
      Log.d("List View","Creating New Row"); 
      row.setTag(myViewHolder); 
     }else { 
      myViewHolder = (MyViewHolder) row.getTag(); 
      Log.d("List View","Recycling Stuff"); 

     } 
     myViewHolder.myImage.setImageResource(images[position]); 
     myViewHolder.myTitle.setText(titleArray[position]); 
     myViewHolder.myDescription.setText(descriptionArrayl[position]); 


     return row; 
    } 
} 

MainActivity.class

package com.faisal.listviewtask; 

import android.content.res.Resources; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    ListView listView; 
    String[] memeTitles; 
    String[] memeDescriptions; 
    CustomAdapter customAdapter; 
    int[] image = {R.drawable.meme1, R.drawable.meme2, 
      R.drawable.meme3, R.drawable.meme4, 
      R.drawable.meme5, R.drawable.meme6, 
      R.drawable.meme7, R.drawable.meme8, 
      R.drawable.meme9, R.drawable.meme10, 
      R.drawable.meme1, R.drawable.meme2, 
      R.drawable.meme3, R.drawable.meme4, 
      R.drawable.meme5, R.drawable.meme6, 
      R.drawable.meme7, R.drawable.meme8, 
      R.drawable.meme9, R.drawable.meme10}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.listview); 


     Resources res = getResources(); 
     memeTitles = res.getStringArray(R.array.titles); 
     memeDescriptions = res.getStringArray(R.array.descriptions); 

     customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions); 
     listView.setAdapter(customAdapter); 

     listView.setItemsCanFocus(false); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show(); 
       Log.d("onitemclick Listener","called"); 
      } 
     }); 

    } 
} 

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:descendantFocusability="blocksDescendants" 
    tools:context="com.faisal.listviewtask.MainActivity"> 
    <ListView 
     android:clickable="true" 
     android:id="@+id/listview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     > 
    </ListView> 

</RelativeLayout> 

row layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <ImageView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/imageView" 
     android:layout_width="46dp" 
     android:layout_height="46dp" 
     android:layout_margin="10dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     /> 

    <TextView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/etTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Title" 
     android:textStyle="bold" 
     android:textSize="25sp" 
     android:layout_alignTop="@+id/imageView" 
     android:layout_toRightOf="@id/imageView" 
     android:layout_alignParentRight="true" 
     /> 

    <TextView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/etDescriptions" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Name" 
     android:textSize="15sp" 
     android:layout_below="@+id/etTitle" 
     android:layout_alignParentEnd="true" 
     android:layout_toEndOf="@+id/imageView" /> 
</RelativeLayout> 

ご迷惑をかけます。 これはすべて私のコードです。 android:focusable="false"android:focusableInTouchMode="false"のようなすべてのタグをリストビューの行に追加し、android:descendantFocusability="blocksDescendants"をリストビューのルートレイアウトに追加し、listView.setItemsCanFocus(false);listView.setOnItemClickListenerのコードに追加しましたが、このonitemのクリックリスナーdoeは機能していません。

答えて

0

あなた、あなたのすべてのコードが正しく、また例外があなたのコードに残っていない。しかしItemClickListenerであなたのListViewコントロールを使用すると、Androidのスタジオで利用可能な
再起動/無効のキャッシュオプションを使用する必要がその後、機能していない、次に実行する場合Listviewプロジェクト、OnItemClickリスナーが正常に動作します。 アンドロイドスタジオの左上にある[ファイルオプション]をクリックし、[無効にするキャッシュ/再起動]オプションを選択します。

0
 add this in Activity [updated] 

     customAdapter = new CustomAdapter(this, memeTitles, image, memeDescriptions,new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
     int i=(Integer)view.getTag(); 
         Toast.makeText(MainActivity.this, "you clicked" + i, Toast.LENGTH_LONG).show(); 
         Log.d("onitemclick Listener","called"); 
        } 


       }); 
       listView.setAdapter(customAdapter); 

     And add id as rootLayout in row xml 

     public class CustomAdapter extends ArrayAdapter<String> { 
      Context context; 
      int[] images; 
      String[] titleArray; 
      String[] descriptionArrayl; 
    OnClickListener onClickListener; 
      public CustomAdapter(Context c,String[] memeTitles,int[] imgs,String[] descriptionArrayl,OnClickListener onClickListener) { 
     super(c,R.layout.row,R.id.etTitle,memeTitles); 
       this.context=c; 
       this.images = imgs; 
       this.titleArray = memeTitles; 
       this.descriptionArrayl=descriptionArrayl; 
       this.onClickListener=onClickListener; 
      } 
      //Sub class of CustomAdapter 
      class MyViewHolder{ 
       ImageView myImage; 
       TextView myTitle; 
       TextView myDescription; 
       RelativeLayout rootLayout; 
       MyViewHolder(View v){ 
     rootLayout=(RelativeLayout)v.findViewById(R.id.rootLayout); 
        myImage= (ImageView) v.findViewById(R.id.imageView); 
        myTitle = (TextView) v.findViewById(R.id.etTitle); 
        myDescription = (TextView) v.findViewById(R.id.etDescriptions); 
       } 
      } 

      //Get View Method of CustomAdapter class 
      public View getView(int position, View convertView, ViewGroup parent){ 
       View row=convertView; 
       MyViewHolder myViewHolder =null; 
       if (row==null) { 
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); 
        row = inflater.inflate(R.layout.row, parent, false); 
        myViewHolder = new MyViewHolder(row); 
        Log.d("List View","Creating New Row"); 
        row.setTag(myViewHolder); 
       }else { 
        myViewHolder = (MyViewHolder) row.getTag(); 
        Log.d("List View","Recycling Stuff"); 

       } 
       myViewHolder.myImage.setImageResource(images[position]); 
       myViewHolder.myTitle.setText(titleArray[position]); 
       myViewHolder.myDescription.setText(descriptionArrayl[position]); 
     rootLayout.setTag(position); 
     rootLayout.setOnClickListener(onClickListener); 

       return row; 
      } 
     } 


row layout.xml 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
<RelativeLayout 
android:id="@+id/rootLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <ImageView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/imageView" 
     android:layout_width="46dp" 
     android:layout_height="46dp" 
     android:layout_margin="10dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     /> 

    <TextView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/etTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Title" 
     android:textStyle="bold" 
     android:textSize="25sp" 
     android:layout_alignTop="@+id/imageView" 
     android:layout_toRightOf="@id/imageView" 
     android:layout_alignParentRight="true" 
     /> 

    <TextView 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:id="@+id/etDescriptions" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:text="Name" 
     android:textSize="15sp" 
     android:layout_below="@+id/etTitle" 
     android:layout_alignParentEnd="true" 
     android:layout_toEndOf="@+id/imageView" /> 
</RelativeLayout> 
</RelativeLayout> 
+1

this.onClickListener = onClickListener;この1つのショーのエラー を追加する方法を 、view.OnClickListener onClickListener はparamerterに –

+0

@FaisalShabbirはちょうどこの –

+0

@FaisalShabbirをクラス変数onClickListenerを宣言して、ビューショー赤い線で使用されていますアプリケーションを実行しようとしています –

関連する問題