如何在主要活动上单击按钮时更改自定义适配器内的ImageView图像?

时间:2017-10-22 13:04:08

标签: android

现在,我为sizeof() = 48 Max_Size = 329406144173384850

中的每个项listView设置了图片
MainActivity
获取private void setBackground(int position, int drawable){ ViewListItem item = adapter.getItem(itemID); switch (position){ case 0: item.ivPlayerAnswer1 = (ImageView)findViewById(R.id.ivPlayerAnswer1); item.ivPlayerAnswer1.setImageResource(drawable); break; case 1: item.ivPlayerAnswer2 = (ImageView)findViewById(R.id.ivPlayerAnswer2); item.ivPlayerAnswer2.setImageResource(drawable); break; case 2: item.ivPlayerAnswer3 = (ImageView)findViewById(R.id.ivPlayerAnswer3); item.ivPlayerAnswer3.setImageResource(drawable); break; case 3: item.ivPlayerAnswer4 = (ImageView)findViewById(R.id.ivPlayerAnswer4); item.ivPlayerAnswer4.setImageResource(drawable); break; } } 点击的

onClick 方法将设置位置图像的背景(button ...)

image1: position = 0, image2: position = 1,

@Override public void onClick(View view) { switch (view.getId()){ case R.id.ivChoice1: setBackground(position, intRes[0]); break; case R.id.ivChoice2: setBackground(position, intRes[1]); break; case R.id.ivChoice3: setBackground(position, intRes[2]); break; case R.id.ivChoice4: setBackground(position, intRes[3]); break; case R.id.ivChoice5: setBackground(position, intRes[4]); break; case R.id.ivChoice6: setBackground(position, intRes[5]); break; } position++; //postion (0, 1, 2, 3...) of each image on item of listview (position is from left to right) if(position > 3){ itemID++; //id of listview 0, 1, 2, 3... position = 0; } } 方法如下:

getView

这仅针对第一项@Override public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = ((Activity) context).getLayoutInflater(); ViewListItem item; //item of listview if(view==null) { view = inflater.inflate(R.layout.item_list, null); item = new ViewListItem(this.context); item.ivSuggestAnswer1 = (ImageView)view.findViewById(R.id.ivSuggestAnswer1); item.ivSuggestAnswer2 = (ImageView)view.findViewById(R.id.ivSuggestAnswer2); item.ivSuggestAnswer3 = (ImageView)view.findViewById(R.id.ivSuggestAnswer3); item.ivSuggestAnswer4 = (ImageView)view.findViewById(R.id.ivSuggestAnswer4); item.ivPlayerAnswer1 = (ImageView)view.findViewById(R.id.ivPlayerAnswer1); item.ivPlayerAnswer2 = (ImageView)view.findViewById(R.id.ivPlayerAnswer2); item.ivPlayerAnswer3 = (ImageView)view.findViewById(R.id.ivPlayerAnswer3); item.ivPlayerAnswer4 = (ImageView)view.findViewById(R.id.ivPlayerAnswer4); view.setTag(item); } else { item = (ViewListItem)view.getTag(); } return view; } } 设置,listView的另一项无法为其设置。

0 个答案:

没有答案