2012-02-29 32 views
0

下面你可以看到代碼,我爲我的listview實現了一個簡單的適配器。但我不能進入onListItemClick。任何人都可以有建議?ListActivity CustomAdapter錯誤,onListItemClick從不工作

實際上它正常顯示列表,但我無法獲取onitemclick事件。提前致謝。

  public class MyListActivity extends ListActivity { 


     @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
       ArrayList<Frame> results = WebOperations 
         .loadList_Frame(); 
       myAdapter = new MyListAdapter(MyListActivity.this); 
       myAdapter.internalList = results; 

       setListAdapter(myAdapter); 
       myAdapter.notifyDataSetChanged(); 

     }; 

     @Override 
      protected void onListItemClick(ListView l, View v, int position, long id) { 
       String item = (String) getListAdapter().getItem(position); 
       Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show(); 
      } 


     public static class MyListAdapter extends BaseAdapter { 

       public ArrayList<Frame> internalList; 
       public LayoutInflater mInflater; 
       public int pageCount = 0; 
       public MyListAdapter(Context context) { 
        mInflater = LayoutInflater.from(context); 
       } 
       public int getCount() { 
        if (internalList == null) 
         return 0; 
        return internalList.size(); 
       } 
       public Object getItem(int position) { 
        if (internalList == null || internalList.size() < position) 
         return null; 
        return internalList.get(position); 
       } 
       public long getItemId(int position) { 
        if (internalList == null || internalList.size() < position) 
         return 0; 
        return internalList.get(position).getId(); 
       } 
       public View getView(int position, View arg1, ViewGroup parent) { 
        View v = arg1; 
        if ((v == null) || (v.getTag() == null)) { 
         v = mInflater.inflate(R.layout.entryrow, null); 
         try { 
          String gunlukText = String.format(" %s ", 
            internalList.get(position).getKeyText().toString()); 
          TextView entry = (TextView) v 
            .findViewById(R.id.textViewEntryText); 
          entry.setText((CharSequence) gunlukText); 
         } catch (Exception e) { 
          Log.d("aaaaaaaaaaaaaaaaaaa", "errorrrrrrrrrrr"); 
         } 
        } 
        return v; 
       } 

      } 
      } 

編輯1:我在下面添加entry_row佈局xml文件。

 <?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/linearLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/textViewEntryText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inputType="textMultiLine" 
       android:padding="5dp" 
       android:paddingLeft="10dp" 
       android:text="@string/Ana_Sayfa" 
       android:textAppearance="?android:attr/textAppearanceMedium" /> 

     </LinearLayout> 
+0

你在該行佈局有什麼看法?它只是一個'TextView'? – Luksprog 2012-02-29 09:02:33

+0

是的,現在只是一個textview。但後來我會添加imageview等 – Yaya 2012-02-29 09:06:33

+0

任何機會你充氣的'TextView'的屬性'android:focusable'或'android:focusableInTouchMode'設置爲'true'? – Luksprog 2012-02-29 09:35:18

回答

1

在返回視圖之前將onclicklistener添加到getView方法中。

v.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Log.w("position", position + ""); 
       } 
      }); 

檢查是否有幫助..

+0

謝謝你,你做了一個很好的建議。在getview方法中,我做了你的建議,它似乎確定。問題現在解決了。但對於我來說,對於列表活動,onlistitemclick永遠不會工作,我無法理解這一點 – Yaya 2012-02-29 09:54:00

2

你應該考慮增加你的聽衆給你的列表視圖:

getListView().setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     String item = (String) getListAdapter().getItem(position); 
     Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show(); 
    } 
}); 
+2

如果您看到,MyListActivity已在擴展ListActivity。 onListItemCLick是當用戶點擊任何列表項時自動調用的回調方法。請閱讀ListActivity的文檔。 – AndroDev 2012-02-29 09:03:04

+0

這就是我自己做的。推測onListItemClick只適用於ListActivity內置的listview。 – 2012-02-29 09:07:17

0

請參閱從http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html定製BaseAdapter的API演示給剛剛覆蓋onListItemCLicked並檢查好例子之一。它工作得很好。嘗試相應地修改您的代碼。

+0

所有的android示例都包含像這個示例一樣的字符串類型的自定義適配器.. – Yaya 2012-02-29 09:27:12

+0

我添加了我的entryrow xml文件。 2我打電話notifydatasetchanged後,我將項目添加到適配器,actully如果你建議別的東西,我可以嘗試? – Yaya 2012-02-29 09:42:07

相關問題