2017-01-01 72 views
0

我有一個ListView,我正在使用自定義適配器和OnItemClickListener方法不起作用。ListView OnItemClickListener不能使用自定義適配器

我試着在StackOverFlow上搜索,但沒有任何答案似乎工作。

這裏是我的代碼:

row.xml:

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


    <TextView 
     android:id="@+id/name" 
     android:layout_height="wrap_content" 
     android:textSize="18sp" 
     android:layout_gravity="left|center_vertical" 
     android:layout_marginLeft="10sp" 
     android:layout_width="150sp" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:layout_marginRight="15sp" /> 

    <TextView 
     android:id="@+id/record" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15sp" 
     android:layout_gravity="right" 
     android:textSize="18sp" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:layout_width="150sp" /> 

</LinearLayout> 

CustomAdapter:

package com.stackoverflow; 

import java.util.ArrayList; 

import android.content.Context; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

public class CustomAdapter extends ArrayAdapter<DataItem> { 
    private ArrayList<DataItem> objects; 

    public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem>  objects) { 
     super(context, textViewResourceId, objects); 
     this.objects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View v = convertView; 
     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.row, null); 
     } 


     DataItem i = objects.get(position); 

     if (i != null) { 
      TextView nameView = (TextView) v.findViewById(R.id.name); 
      TextView recordView = (TextView) v.findViewById(R.id.record); 

      if (nameView != null){ 
       nameView.setText(i.getname()); 
       nameView.setTextSize(18); 
       nameView.setTextColor(Color.WHITE); 
      } 
      if (recordView != null){ 
       recordView.setText(i.getrecord()); 
       recordView.setTextSize(18); 
       recordView.setTextColor(Color.WHITE); 
      } 

     } 

     return v; 
    } 
} 

的DataItem:

public class DataItem { 
    private String name; 
    private String record; 

    public DataItem() { 

    } 

    public DataItem(String n, String r) { 
     this.name = n; 
     this.record = r; 
    } 

    public String getname() { 
     return name; 
    } 

    public void setname(String name) { 
     this.name = name; 
    } 

    public String getrecord() { 
     return record; 
    } 

    public void setrecord(String record) { 
     this.record = record; 
    } 

MainActivity:

listView = (ListView) findViewById(R.id.listView); 
     DataItem obj1 = new DataItem("Example 1", "$12"); 
     DataItem obj2 = new DataItem("Example 2", "$23"); 
     data.add(obj1); 
     data.add(obj2); 
     CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data); 
     listView.setAdapter(adapter); 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       Log.i("App Info: ", "Clicked!"); 

      } 
     }); 

基本上,這裏的問題是,我不能讓我的onItemClickListener工作;當我點擊listView中的一個項目時,沒有任何反應。有誰能夠幫助我?

感謝, KPS

+0

也許你是Android的新手,但很長一段時間沒有人使用ListView,因爲它太粗糙和過時。看看RecyclerView(https://developer.android.com/training/material/lists-cards.html),它更好,更現代,更靈活。 –

+0

它的工作....我已經測試你的代碼..我已經把listview上onItemClick..t烤麪包和吐司出現,當我點擊listview項目...無論問題是我米仍然張貼您的代碼作爲答案。 –

+0

感謝VortexHeatKiller的想法。我其實並不知道RecyclerViews,因爲我接受Udemy的課程從未涉及過他們。謝謝! – kps2501

回答

0

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    ListView listView; 
    ArrayList<DataItem> data; 

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

     listView = (ListView) findViewById(R.id.listView); 
     data = new ArrayList<>(); 
     DataItem obj1 = new DataItem("Example 1", "$12"); 
     DataItem obj2 = new DataItem("Example 2", "$23"); 
     data.add(obj1); 
     data.add(obj2); 
     CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data); 
     listView.setAdapter(adapter); 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       Log.i("App Info: ", "Clicked!"); 
       Toast.makeText(MainActivity.this, "Clicked"+i, Toast.LENGTH_SHORT).show(); 

      } 
     }); 
    } 
} 

剩下的代碼是一樣的,你code..Try this..because它爲我工作

+0

有趣的是,對我來說這是行不通的。 – kps2501

+0

讓我附上屏幕截圖 –

+0

它仍然不適合我。 ;( – kps2501

0

見本屏幕截圖,

enter image description here

您正在打印日誌onItemClick,因此請檢查您是否打印日誌列表項目單擊或不是。

+1

而不是張貼另一個答案,更好地編輯你的第一個答案! – Piyush

0

現在正在工作。這只是一個佈局問題。感謝您的支持!

相關問題