2016-02-28 111 views
1

創建具有自定義適配器和行佈局的listView,但無法正確設置onClickListener。 onClickListener正在工作時,它是在建立列表中的android,而不是一個自定義的..不知道爲什麼它現在不工作。 總之,這裏的代碼自定義ListView onItemClickListener

列表

public class VjezbeLista extends AppCompatActivity { 
    ListView listaVjezbe; 
    ArrayAdapter<String> adapter; 

    String[] misicneSkupine = {"Prsa", 
      "Leđa", 
      "Ramena", 
      "Biceps", 
      "Triceps", 
      "Trbuh", 
      "Kvadriceps", 
      "Loža", 
      "Listovi"}; 

    Integer[] imageId = {R.drawable.ic_vjezbe_prsa, 
      R.drawable.ic_vjezbe_ledja, 
      R.drawable.ic_vjezbe_ramena, 
      R.drawable.ic_vjezbe_biceps, 
      R.drawable.ic_vjezbe_triceps, 
      R.drawable.ic_vjezbe_trbuh, 
      R.drawable.ic_vjezbe_kvadriceps, 
      R.drawable.ic_vjezbe_loza, 
      R.drawable.ic_vjezbe_listovi}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_vjezbe_lista); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 



     AdapterListaVjezbe adapter = new AdapterListaVjezbe(VjezbeLista.this, misicneSkupine, imageId); 
     listaVjezbe = (ListView) findViewById(R.id.listaVjezbe); 
     listaVjezbe.setAdapter(adapter); 
     listaVjezbe.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       if (position == 0){ 
        Intent intent = new Intent(VjezbeLista.this, VjezbeListaPrsa.class); 
        startActivity(intent); 
       } // Here will go else if etc... 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_pretraga, menu); 
     return true; 
    } 

} 

適配器

public class AdapterListaVjezbe extends ArrayAdapter<String> { 
    private final Activity context; 
    private final String[] web; 
    private final Integer[] imageId; 


    public AdapterListaVjezbe(Activity context, 
         String[] web, Integer[] imageId) { 
     super(context, R.layout.layout_lista_vjezbe_2, web); 

     this.context = context; 
     this.web = web; 
     this.imageId = imageId; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView= inflater.inflate(R.layout.layout_lista_vjezbe_2, null, true); 
     TextView txtTitle = (TextView) rowView.findViewById(R.id.textLista); 

     ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewListaVjezbe2); 
     txtTitle.setText(web[position]); 

     imageView.setImageResource(imageId[position]); 
     return rowView; 
    } 
} 

和行佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/btnVjezbePrsaPotisakSKlupe" 
     android:minHeight="72dp" 
     android:clickable="true" 
     android:background="?attr/selectableItemBackground"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 

       <ImageView 
        android:layout_width="56dp" 
        android:layout_height="56dp" 
        android:id="@+id/imageViewListaVjezbe2" 
        android:layout_marginLeft="10dp" 
        android:layout_gravity="center_vertical" 
        android:src="@drawable/ic_potisak_s_klupe" /> 
      </LinearLayout> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="0.9" 
       android:gravity="center_vertical" 
       android:layout_marginBottom="0dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="10dp" 
       android:layout_marginRight="10dp"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textAppearance="?android:attr/textAppearanceLarge" 
        android:text="@string/potisakSKlupe" 
        android:id="@+id/textLista" 
        android:textSize="16sp" 
        android:layout_marginLeft="10dp" 
        android:layout_weight="0.9" 
        android:layout_marginBottom="10dp" /> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/imageVisdfsdew19" 
        android:background="@drawable/ic_strelica" 
        android:layout_marginBottom="10dp" 
        android:minHeight="36dp" 
        android:minWidth="36dp" 
        android:layout_marginLeft="10dp" /> 

      </LinearLayout> 

     </TableRow> 

    </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

因此,如果任何人都可以步行通過我的錯誤,我將不勝感激.. 謝謝!!

+0

也許你的物品不附加到你的rootparent。你試過查看rowView = inflater.inflate(R.layout.layout_lista_vjezbe_2,parent,true); ? – king

+0

它給java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)沒有在適配器視圖錯誤支持 – DaxHR

+0

的'機器人:可點擊= 「真」'的'機器人:ID = 「@ + ID/btnVjezbePrsaPotisakSKlupe」'RelativeLayout的看起來很可疑。 – frozenkoi

回答

3

您可以在列表視圖填日期之後加入這行

listaVjezbe.setAdapter(適配器); adapter.notifyDataSetChanged();

0

刪除您指定的行佈局中ID爲「btnVjezbePrsaPotisakSKlupe」的線性佈局中的android:clickable =「true」。因爲觸發了線性佈局的點擊事件而不是整個列表項目佈局。

0

刪除android:clickable =「true」 from btnVjezbePrsaPotisakSKlupe LineareLayout。那麼它會正常工作(測試)

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/linearLayout"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/btnVjezbePrsaPotisakSKlupe" 
     android:minHeight="72dp" 

     android:clickable="true" 

     android:background="?attr/selectableItemBackground"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical">