2015-04-01 63 views
1

我有一個自定義的適配器幾乎完成,除了我想findbyid一個Tablelayout並編程方式將每個孩子變成點擊監聽器。Android如何在我的自定義baseadapter中查找視圖?

package com.mhsquire.adapter; 

import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.mhsquire.R; 
import com.mhsquire.data.Constants; 
import com.mhsquire.data.Product; 

import java.util.ArrayList; 
import java.util.List; 

import butterknife.ButterKnife; 
import butterknife.InjectViews; 

//import butterknife.Views; 

/** 
* Created by owner on 3/27/2015. 
*/ 
public class FlippableProductAdapter extends BaseAdapter { 

    private Activity activity; 
    private String[] data; 
    private LayoutInflater inflater = null; 
    private ArrayList dataList = new ArrayList(); 
    private View targetView; 
    private int repeatCount = 1; 
    private static String TAG = "Product Adapter"; 
    private int mMaxIndex = 8; 
    private int mIndexSize = 4; 
    private static int mIndex = 0; 

    @InjectViews({R.id.imgtxtProduct, R.id.imgtxtProductDescription}) 
    List<TextView> titleTexts; 

    public FlippableProductAdapter(Activity a, String[] d) { 
     activity = a; 
     data = d; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     //imageLoader=new ImageLoader(activity.getApplicationContext()); 
    } 

    public FlippableProductAdapter(Activity a) { 
     activity = a; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public FlippableProductAdapter(Activity a, ArrayList target) { 
     activity = a; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     dataList = target; 

     inflater = LayoutInflater.from(a.getBaseContext()); 
    } 


    private void initializeProductList(ViewHolder holder) { 
     for (int i = 0; i < mMaxIndex; i = i + 2) { 
      if (mIndex + i/2 < Constants.ALLPRODUCTS.size() && !Constants.ALLPRODUCTS.isEmpty()) { 
       holder.textViews.get(i).setText(Constants.ALLPRODUCTS.get(mIndex + i/2).getProductname()); 
       holder.textViews.get(i + 1).setText(Constants.ALLPRODUCTS.get(mIndex + i/2).getProductDescription()); 
      } else { 
       holder.textViews.get(i).setText(""); 
       holder.textViews.get(i + 1).setText(""); 
      } 
     } 
    } 

    View.OnClickListener onClick(final TableRow row, final int col) { 
     return new View.OnClickListener() { 
      public void onClick(View v) { 
       int position = v.getId(); 
       Log.i(TAG, "Clicked Item " + Constants.PRODUCTINDEX); 
       if (Constants.INDEX + position < Constants.ALLPRODUCTS.size()) { 
        titleTexts.get(0).setText(Constants.ALLPRODUCTS.get(Constants.INDEX + position).getProductname()); 
        titleTexts.get(1).setText(Constants.ALLPRODUCTS.get(Constants.INDEX + position).getProductDescription()); 
       } 

      } 
     }; 
    } 

    public static class ViewHolder { 

     public ViewHolder(View view) { 
      ButterKnife.inject(this, view); 
     } 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     ViewHolder holder; 
     final int toofar = 2; 


      Constants.FUTUREPOSITION = position; 

     mIndex = Constants.FUTUREPOSITION * mIndexSize; 

     if (convertView == null) { 
      vi = inflater.inflate(R.layout.activity_main_flippable, null); 
      holder = new ViewHolder(vi); 
      vi.setTag(holder); 
     } else holder = (ViewHolder) vi.getTag(); 

     Product target = (Product) dataList.get(position); 

     initializeProductList(holder); 

     TableRow row; 
     TableLayout group = (TableLayout) activity.findViewById(R.id.TableLayout); // NullPointerException Here 
     for (int i = 0; i < group.getChildCount(); i++) 
     { 
      row = (TableRow) group.getChildAt(i); 
      activity.registerForContextMenu(row); 
      row.setId(i); 
      row.setOnClickListener(onClick(row, i)); 
     } 


     return vi; 
    } 

} 

現在我的問題是,我收到上面一行的NullPointerException。什麼是標識TableLayout的正確方法?

+0

我想你希望你的'TableLayout'' id'是非類型的東西,例如, 「myTableLayout」,或者你在XML中調用它的任何東西。 – DigitalNinja 2015-04-01 22:05:48

+0

你的R.id.TableLayout是哪裏?在'activity_main_flippable.xml'中? – tachyonflux 2015-04-01 22:12:11

+0

我簡單地稱它爲TableLayout。這一切都在主要活動中運作。有聽衆分配給只有一個頁面的問題。是的,R.id.TableLayout位於activity_main_flippable.xml中。 – mhsquire83 2015-04-01 22:13:40

回答

0

創建一個名爲ViewHolder類放在這樣的代碼和OnClck():

public static class ViewHolder { 

     public ViewHolder(View view) { 
      ButterKnife.inject(this, view); 

      TableRow row; 
      TableLayout group = (TableLayout) 
      view.findViewById(R.id.TableLayout); // NullPointerException Here 
      for (int i = 0; i < group.getChildCount(); i++) 
      { 
       row = (TableRow) group.getChildAt(i); 
       activity.registerForContextMenu(row); 
       row.setId(i); 
       row.setOnClickListener(onClick(row, i)); 
      } 
     } 

     View.OnClickListener onClick(final TableRow row, final int col) { 
      return new View.OnClickListener() { 
       public void onClick(View v) { 
        int position = v.getId(); 
        Log.i(TAG, "Clicked Item " + Constants.PRODUCTINDEX); 
        if (Constants.INDEX + position < Constants.ALLPRODUCTS.size()) { 
        titleTexts.get(0).setText(Constants.ALLPRODUCTS.get(Constants.INDEX + position).getProductname()); 
        titleTexts.get(1).setText(Constants.ALLPRODUCTS.get(Constants.INDEX + position).getProductDescription()); 
       } 

      } 
     }; 
    } 
    } 

GetView膨脹認爲它傳遞給ViewHolder,然後可以操縱孩子。

public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     ViewHolder holder; 
     final int toofar = 2; 


      Constants.FUTUREPOSITION = position; 

     mIndex = Constants.FUTUREPOSITION * mIndexSize; 

     if (convertView == null) { 
      vi = inflater.inflate(R.layout.activity_main_flippable, null); 
      holder = new ViewHolder(vi); 
      vi.setTag(holder); 
     } else holder = (ViewHolder) vi.getTag(); 

     Product target = (Product) dataList.get(position); 

     initializeProductList(holder); 




     return vi; 
    } 

} 
0

嘗試更換您的groupparent觀點:

TableRow row; 

for (int i = 0; i < parent.getChildCount(); i++) 
{ 
    row = (TableRow) parent.getChildAt(i); 
    activity.registerForContextMenu(row); 
    row.setId(i); 
    row.setOnClickListener(onClick(row, i)); 
} 
+0

我認爲這是正確的軌道。但是,父級是GLSurfaceView,而不是TableLayout。我什至嘗試了findViewById()方法父包含。沒有運氣。 – mhsquire83 2015-04-02 15:35:33

+0

非常好的建議。我調整了一下,並將你的代碼移入ViewHolder類。然後我改變了父母的觀點。這解決了這個問題。 – mhsquire83 2015-04-02 17:17:05

+0

不錯,隨時發佈你自己的答案,以幫助未來的用戶。 – Groco 2015-04-02 20:04:32

相關問題