gpt4 book ai didi

Android ListView 适配器 OnClickListener 问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:53:31 25 4
gpt4 key购买 nike

我为每个 View 创建了一个带有自定义布局的 ListView。我在自定义布局中的 TextView 项目的 ListView 适配器中有几个 onClickListeners。 onClickListeners 在 ListView 适配器中工作,我可以获得 View 的位置,但是当我尝试在 onClickListeners 中调用我 Activity 中的方法时,我得到“无法进行静态引用到非静态方法”错误。所以我开始将东西转换为静态,这让事情按预期工作,但我相信你们都知道这是一个大错误,我终于明白了。

我如何在不将方法设为静态的情况下访问我的主要 Activity 中的方法?我是新手,所以请原谅我的菜鸟问题。谢谢

部分代码 list ...

public class main extends Activity {
private ArrayList<DataItem> dataItems;
private DataItemAdapter aa;
private ListView dataListView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

dataListView = (ListView)findViewById(R.id.dataListView);
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
dataListView.setItemsCanFocus(true);

populateArray();
}

public void populateArray() {
DataItem newItem = new DataItem(
"2008","Ferrari","F430","Red","ASX772"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2008","Ferrari","F430","Black","TZB123"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2009","Ferrari","F430","Red","MIDAS"
);
dataItems.add(0, newItem);

aa.notifyDataSetChanged();
}

public static void modelInfo(int pos) {
Log.i("modelInfo", "=" + pos);
}

public static void makeInfo(int pos) {
Log.i("makeInfo", "=" + pos);
}

public static void assetInfo(int pos) {
Log.i("assetInfo", "=" + pos);
}

}


public class DataItemAdapter extends ArrayAdapter<DataItem> {
private Activity activity;
private int resource;
private static LayoutInflater inflater=null;

public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
super(_activity, _resource, _items);
inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resource = _resource;
activity = _activity;
}

public static class ViewHolder {
TextView carYear;
TextView carMake;
TextView carModel;
TextView carColor;
TextView assetTag;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(resource, null);
holder=new ViewHolder();
holder.carYear = (TextView)vi.findViewById(R.id.carYear);
holder.carMake = (TextView)vi.findViewById(R.id.carMake);
holder.carModel = (TextView)vi.findViewById(R.id.carModel);
holder.carColor = (TextView)vi.findViewById(R.id.carColor);
holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}

DataItem item = getItem(position);

holder.carYear.setText(item.getCarYear());
holder.carMake.setText(item.getCarMake());
holder.carModel.setText(item.getCarModel());
holder.carColor.setText(item.getCarColor());
holder.assetTag.setText(item.getAssetTag());

holder.carYear.setTag(position);
holder.assetTag.setTag(position);

final OnClickListener makeListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.makeInfo(pos);
}
};
holder.carMake.setOnClickListener(makeListener);

final OnClickListener modelListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.modelInfo(pos);

}
};

holder.carModel.setOnClickListener(modelListener);

final OnClickListener assetListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
main.assetInfo(pos);
}
};
holder.assetTag.setOnClickListener(assetListener);

return vi;
}

最佳答案

为什么不在 Activity 中将 onItemClickListener 附加到 ListView,而是从 ListView 中的每个内部 View 执行 ItemClick

        dataListView = (ListView)findViewById(R.id.dataListView);     
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
//attach a listener to the list view
dataListView.setOnItemClickListener (listener);

dataListView.setItemsCanFocus(true);

在 onItemClick 方法的监听器中,您可以访问 Activity 方法。

编辑 1:OnItemClickListener 给你以下参数,AdapterView parent, View view, int position, long

您的个人 TextView 是 View 参数的子项,您可以访问它以获取 childAt... 如下所示:

OnItemClickListener listener = new OnItemClickListener (){

@Override
onItemClick(AdapterView<?> parent, View view, int position, long id){
((TextView)view.findViewById(R.id.yourTextViewId)).getText();
//or do your stuff
}

}

编辑 2:

你的主要 Activity ,记住,按照惯例所有类名都是大写的,所以主类必须是主类

public class Main extends Activity {
private ArrayList<DataItem> dataItems;
private DataItemAdapter aa;
private ListView dataListView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

dataListView = (ListView)findViewById(R.id.dataListView);
dataItems = new ArrayList<DataItem>();
int resID = R.layout.dataitem;
aa = new DataItemAdapter(this, resID, dataItems);
dataListView.setAdapter(aa);
dataListView.setItemsCanFocus(true);

populateArray();
}

public void populateArray() {
DataItem newItem = new DataItem(
"2008","Ferrari","F430","Red","ASX772"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2008","Ferrari","F430","Black","TZB123"
);
dataItems.add(0, newItem);
newItem = new DataItem(
"2009","Ferrari","F430","Red","MIDAS"
);
dataItems.add(0, newItem);

aa.notifyDataSetChanged();
}

public void modelInfo(int pos) {
Log.i("modelInfo", "=" + pos);
}

public void makeInfo(int pos) {
Log.i("makeInfo", "=" + pos);
}

public void assetInfo(int pos) {
Log.i("assetInfo", "=" + pos);
}
}

现在,你的适配器

public class DataItemAdapter extends ArrayAdapter<DataItem> {
private Activity activity;
private int resource;
private LayoutInflater inflater=null;

public DataItemAdapter(Activity _activity,int _resource,List<DataItem> _items) {
super(_activity, _resource, _items);
inflater = (LayoutInflater)_activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//i always do this way, but i dont think this is the error
//inflater = LayoutInflater.from(_activity.getBaseContext());
resource = _resource;
activity = _activity;
}

public static class ViewHolder {
TextView carYear;
TextView carMake;
TextView carModel;
TextView carColor;
TextView assetTag;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(resource, null);
holder=new ViewHolder();
holder.carYear = (TextView)vi.findViewById(R.id.carYear);
holder.carMake = (TextView)vi.findViewById(R.id.carMake);
holder.carModel = (TextView)vi.findViewById(R.id.carModel);
holder.carColor = (TextView)vi.findViewById(R.id.carColor);
holder.assetTag = (TextView)vi.findViewById(R.id.assetTag);
vi.setTag(holder);
} else {
holder=(ViewHolder)vi.getTag();
}

DataItem item = getItem(position);

holder.carYear.setText(item.getCarYear());
holder.carMake.setText(item.getCarMake());
holder.carModel.setText(item.getCarModel());
holder.carColor.setText(item.getCarColor());
holder.assetTag.setText(item.getAssetTag());

holder.carYear.setTag(position);
holder.assetTag.setTag(position);

final OnClickListener makeListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.makeInfo(pos);
((Main)activity).makeInfo(pos);
}
};
holder.carMake.setOnClickListener(makeListener);

final OnClickListener modelListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.modelInfo(pos);
((Main)activity).modelInfo(pos);
}
};

holder.carModel.setOnClickListener(modelListener);

final OnClickListener assetListener = new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout)v.getParent();
TextView tv = (TextView)ll.getChildAt(0);
Integer pos = (Integer) tv.getTag();
//main.assetInfo(pos);
((Main)activity).assetInfo(pos);
}
};
holder.assetTag.setOnClickListener(assetListener);

return vi;
}

希望它有用:)

关于Android ListView 适配器 OnClickListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com