2016-04-26 10 views
1

私は一日中立ち往生しました。私の間違いを見つけることができませんでした。カスタムリストビューをクリックできません

私はカスタムリストビューを作成しましたが、それをクリックしてHourListViewで新しいインテントを起動できるようにしたいだけです。

public class CustomList extends ArrayAdapter<String> { 

private final ArrayList<String> hourList; 

private final Activity context; 

public CustomList(Activity context, 
       ArrayList<String> hourList) { 
    super(context, R.layout.hour_list_adapter, hourList); 
    this.context = context; 
    this.hourList = hourList; 


} 


@Override 
public View getView(int position, View view, ViewGroup parent) { 
    View rowView = view; 

    ViewHolder viewHolder = null; 


    // LayoutInflater inflater = context.getLayoutInflater(); 
    // View rowView= inflater.inflate(R.layout.hour_list_adapter, parent, false); 



    if(rowView == null) 
    { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     rowView = inflater.inflate(R.layout.hour_list_adapter, parent, false); 

     viewHolder = new ViewHolder(); 

     viewHolder.type_frequenceTV = (TextView) rowView.findViewById(R.id.type_frequence); 
     viewHolder.dateTV = (TextView) rowView.findViewById(R.id.date); 
     viewHolder.dureeTV = (TextView) rowView.findViewById(R.id.duree); 
     viewHolder.commentaireTV = (TextView) rowView.findViewById(R.id.commentaire); 

     viewHolder.simuIV = (ImageView) rowView.findViewById(R.id.simulateur); 
     viewHolder.doubleIV = (ImageView) rowView.findViewById(R.id.doubleFrequence); 



     rowView.setTag(viewHolder); 
    } 
    else 
    { 
     viewHolder = (ViewHolder)rowView.getTag(); 
    } 



    viewHolder.type_frequenceTV.setText(HourListActivity.hourObjects.get(position).get("type_frequence").toString()); 

    viewHolder.dureeTV.setText(HourListActivity.hourObjects.get(position).get("duree").toString()); 
    viewHolder.commentaireTV.setText(HourListActivity.hourObjects.get(position).get("comment").toString()); 


    Date dateAndTime = (Date) HourListActivity.hourObjects.get(position).get("date"); 

    SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yy à HH:mm"); 
    String formatted = format1.format(dateAndTime.getTime()); 
    viewHolder.dateTV.setText("Le " + formatted); 



    if ((Boolean) HourListActivity.hourObjects.get(position).get("double")) 
    { 
     viewHolder.doubleIV.setVisibility(View.VISIBLE); 
    } 
    else 
    { 
     viewHolder.doubleIV.setVisibility(View.GONE); 
    } 

    if ((Boolean) HourListActivity.hourObjects.get(position).get("simulateur")) 
    { 
     viewHolder.simuIV.setVisibility(View.VISIBLE); 
    } 
    else 
    { 
     viewHolder.simuIV.setVisibility(View.GONE); 
    } 


    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

     } 
    }); 


    return rowView; 



} 

static class ViewHolder 
{ 
    TextView type_frequenceTV; 
    TextView dateTV ; 
    TextView dureeTV ; 
    TextView commentaireTV ; 
    ImageView simuIV ; 
    ImageView doubleIV ; 

} 

}

その後、これは私がクリックできるようにしたいHourListActivityです。

public class HourListActivity extends AppCompatActivity { 

//ArrayAdapter<String> arrayAdapter; 
CustomList adapter; 
ListView hourListView; 
static ArrayList<String> hourList; 
static ArrayList<String> hourListID; 
static ArrayList<ParseObject> hourObjects = new ArrayList<ParseObject>(); 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_hour_list); 

    hourList = new ArrayList<>(); 
    hourListID = new ArrayList<>(); 

    hourListView = (ListView) findViewById(R.id.listView); 

    adapter = new CustomList(HourListActivity.this,hourList); 

    //arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, hourList); 

    hourListView.setAdapter(adapter); 

    hourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

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

      System.out.println("CLIC !!!!!!!!!!!!!!!!!!!!!"); 
      ParseQuery<ParseObject> query = ParseQuery.getQuery("Frequence"); 

      query.getInBackground(hourListID.get(position), new GetCallback<ParseObject>() { 

       public void done(ParseObject object, ParseException e) { 
        if (e == null) { 


         Intent intent = new Intent(getApplicationContext(), ModifyHourActivity.class); 
         intent.putExtra("objectID", object.getObjectId()); 
         startActivity(intent); 


        } else { 
         // something went wrong 
        } 
       } 
      }); 


     } 
    }); 


    ParseQuery<ParseObject> query = ParseQuery.getQuery("Frequence"); 

    query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername()); 

    System.out.println("username: " + ParseUser.getCurrentUser().getUsername()); 

    query.orderByDescending("date"); 



    query.findInBackground(new FindCallback<ParseObject>() { 
     @Override 
     public void done(List<ParseObject> objects, ParseException e) { 
      if (e == null) 
      { 


       if (objects.size() > 0) 
       { 

        hourObjects.clear(); 

        for (ParseObject frequence : objects) 
        { 
         String result = ""; 
         result += frequence.get("date") + " - " 
           + frequence.get("type_frequence") + " - " 
           + frequence.get("duree") + " - " 
           + frequence.get("comment") + " - " 
           + frequence.get("double") + " - " 
           + frequence.get("simulateur") + " - "; 


         System.out.println("Résultat : " + result); 

         hourList.add(result.toString()); 
         hourListID.add(frequence.getObjectId()); 

         ParseObject newHour = new ParseObject("Temp"); 

         newHour.put("username", frequence.get("username")); 
       //  newHour.put("id", frequence.get("objectId")); 
         newHour.put("type_frequence", frequence.get("type_frequence")); 
         newHour.put("date", frequence.get("date")); 
         newHour.put("duree", frequence.get("duree")); 
         newHour.put("comment", frequence.get("comment")); 
         newHour.put("double", frequence.get("double")); 
         newHour.put("simulateur", frequence.get("simulateur")); 

         hourObjects.add(newHour); 



        } 

        Toast.makeText(HourListActivity.this, "Liste des heures récupérée !", 
          Toast.LENGTH_LONG).show(); 

        adapter.notifyDataSetChanged(); 

       } 



      } 
      else 
      { 


      } 

     } 
    }); 

Aカスタムビューのために最後に私のxmlファイル:任意の助けを事前に

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    android:orientation="horizontal" 
    android:layout_margin="5dp" 
    > 

    <TextView 
     android:id="@+id/type_frequence" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="@color/colorPrimary" 
     android:gravity="center" 
     android:text="DEP S" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/buttonTextColor" 
     android:paddingLeft="10dp" 
     android:paddingRight="10dp" 


     /> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="35dp" 
      android:orientation="horizontal"> 



      <TextView 
       android:id="@+id/date" 
       android:layout_width="wrap_content" 
       android:textSize="15dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_vertical" 
       android:text="12 avril 2016 - 08:00" 
       android:layout_marginLeft="5dp" 


       /> 



      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 
       <TextView 
        android:id="@+id/duree" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@color/colorPrimary" 

        android:text="1.00" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:textColor="@color/buttonTextColor" 
        android:layout_alignParentEnd="true" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" 


        /> 

      </RelativeLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="35dp" 
      android:orientation="horizontal"> 


      <TextView 
       android:id="@+id/commentaire" 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_height="wrap_content" 
       android:text="Commentaire" 
       /> 

      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical" 
       > 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentEnd="true"> 
       <ImageView 
        android:id="@+id/doubleFrequence" 
        android:layout_width="35dp" 
        android:layout_height="35dp" 

        android:layout_column="1" 



        android:src="@drawable/double_96" /> 
       <ImageView 
        android:id="@+id/simulateur" 
        android:layout_width="35dp" 
        android:layout_height="35dp" 
        android:layout_column="1" 

        android:src="@drawable/simu_96" 



        android:layout_toStartOf="@+id/doubleFrequence" /> 
       </LinearLayout> 
      </RelativeLayout> 
     </LinearLayout> 

    </LinearLayout> 
</LinearLayout> 

ありがとう! :)

+0

setOnItemClickListenerを使用する場合は、getViewで設定したクリックリスナーを削除してください。アイテムのレイアウト内でクリック可能なビューを使用しないでください。 – Cata

+0

getViewで設定したクリックリスナーを削除し、rowviewのすべての子ビューでandroid:focusableInTouchMode = "false" android:clickable = "false" android:focusable = "false"を無効にしてみてください – USKMobility

+0

http://stackoverflow.com/質問/ 36814378/baseadapter-onitemclicklistener-never-called/36814478#36814478 – USKMobility

答えて

0

setOnClickListenerでエラーが発生した場合は、これを試すことができます。 ツール - > Gradleと同期します。

私が意図の問題にこだわったとき、それは私を助けました!

+0

実際に働いた! :) ありがとう! –

+0

あなたの問題が解決したと聞いてうれしいです。 :) –

1

アダプターでこの部品を削除するとどうなりますか?私は推測

rowView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

    } 
}); 

は何が起こるかビューは、あなたのアダプター内の1つのことで、実行時に上書きされてClickListener、その結果、あなたの活動にItemClickListenerを定義した後を作成していることです。

+1

お返事ありがとうございました。私はあなたの回答とあまりにも助けてくれたと思っています:) –

関連する問題