我有一個自定義的適配器幾乎完成,除了我想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的正確方法?
我想你希望你的'TableLayout'' id'是非類型的東西,例如, 「myTableLayout」,或者你在XML中調用它的任何東西。 – DigitalNinja 2015-04-01 22:05:48
你的R.id.TableLayout是哪裏?在'activity_main_flippable.xml'中? – tachyonflux 2015-04-01 22:12:11
我簡單地稱它爲TableLayout。這一切都在主要活動中運作。有聽衆分配給只有一個頁面的問題。是的,R.id.TableLayout位於activity_main_flippable.xml中。 – mhsquire83 2015-04-01 22:13:40