2016-02-28 133 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">