我有一个几乎完成的自定义适配器,除了我想找到一个Tablelayout并以编程方式将每个子节点转换为单击侦听器。
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 :(得分:0)
尝试用group
视图替换parent
:
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));
}
答案 1 :(得分: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;
}
}