我有一个自定义操作栏,如下图所示:
我也有列表视图。在列表的每一行中,它包含加号和减号图像按钮。我想显示当我按下加号按钮时,操作栏textView应显示为1,如果我再按下加号按钮,则应增加1并显示2。
请检查下面的代码并告诉我如何解决此问题。
public class MainMenu extends AppCompatActivity {
String title;
ListView listView;
ImageView imageView,imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbarlayout, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
title = getIntent().getStringExtra("title");
listView =(ListView)findViewById(R.id.listView);
imageView = (ImageView)findViewById(R.id.plus);
imageView1 = (ImageView)findViewById(R.id.minus);
}
public void plusClick(View v)
{
if (listView.getPositionForView(v)==0)
{
Toast.makeText(MainMenu.this, "this is Plus1", Toast.LENGTH_SHORT).show();
}
else if (listView.getPositionForView(v)==1)
{
Toast.makeText(MainMenu.this, "this is Plus2", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
super.onResume();
if (getSupportActionBar() != null){
getSupportActionBar().setTitle(title);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return false;
}
}
以下是我的类适配器
public class ListViewAdapter extends ArrayAdapter<ItemData> {
protected ListView mListView;
private final List<ItemData> items;
private LayoutInflater inflater;
public ListViewAdapter(Context context, int resource, List<ItemData> items) {
super(context, resource, items);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
@Override
public int getCount() {
return items != null ? items.size() : 0;
}
@Override
public ItemData getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View v = convertView;
ViewHolder holder = null;
if (v == null) {
holder = new ViewHolder();
int layout = R.layout.item_layout;
v = inflater.inflate(layout, null);
holder.tt1 = (TextView) v.findViewById(R.id.team);
holder.tt12 = (TextView) v.findViewById(R.id.textView1);
holder.img = (ImageView) v.findViewById(R.id.icon);
holder.img1 = (ImageView) v.findViewById(R.id.plus);
holder.img2 = (ImageView) v.findViewById(R.id.minus);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
if (position %2==0){
v.setBackgroundResource(R.drawable.color1);
}
else {
v.setBackgroundResource(R.drawable.color2);
}
ItemData p = items.get(position); //getItem(position);
holder.tt1.setText(p.getTitle());
holder.tt12.setText(p.getDetails());
holder.img.setImageDrawable(p.getImage());
holder.img1.setImageDrawable(p.getImageplus());
holder.img2.setImageDrawable(p.getImageminus());
Log.i("fff", "getView: image = " + p.getImage());
//load image directly
return v;
}
static class ViewHolder {
TextView tt1, tt12;
ImageView img,img1,img2;
}
}
答案 0 :(得分:0)
尝试使用此
更新您的代码public class MainMenu extends AppCompatActivity {
String title;
ListView listView;
ListViewAdapter adapter;
ArrayList<ItemData> search = new ArrayList<ItemData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbarlayout, null); // layout which contains your button.
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
title = getIntent().getStringExtra("title");
listView =(ListView)findViewById(R.id.listView);
adapter = new ListViewAdapter(getApplication(), search,listView, title);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return false;
}
}
<强> ListViewAdapter 强>
public class ListViewAdapter extends ArrayAdapter<ItemData> {
protected ListView mListView;
private final List<ItemData> items;
private LayoutInflater inflater;
TextView title;
private static int checkBoxCounter = 0;
public ListViewAdapter(Context context, int resource, List<ItemData> items,ListView listview, TextView title) {
super(context, resource, items);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
this.title = title;
}
@Override
public int getCount() {
return items != null ? items.size() : 0;
}
@Override
public ItemData getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View v = convertView;
ViewHolder holder = null;
if (v == null) {
holder = new ViewHolder();
int layout = R.layout.item_layout;
v = inflater.inflate(layout, null);
holder.tt1 = (TextView) v.findViewById(R.id.team);
holder.tt12 = (TextView) v.findViewById(R.id.textView1);
holder.img = (ImageView) v.findViewById(R.id.icon);
holder.img1 = (ImageView) v.findViewById(R.id.plus);
holder.img2 = (ImageView) v.findViewById(R.id.minus);
holder.imag1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
checkBoxCounter ++;
}
});
holder.imag2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
checkBoxCounter --;
}
});
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
ItemData p = items.get(position); //getItem(position);
holder.tt1.setText(p.getTitle());
holder.tt12.setText(p.getDetails());
holder.img.setImageDrawable(p.getImage());
holder.img1.setImageDrawable(p.getImageplus());
holder.img2.setImageDrawable(p.getImageminus());
Log.i("fff", "getView: image = " + p.getImage());
//load image directly
return v;
}
static class ViewHolder {
TextView tt1, tt12;
ImageView img,img1,img2;
}
答案 1 :(得分:0)
有几种方法可以做到,但我建议通过界面:
您可以在活动中实现一个接口,该接口将事件从适配器传递到Activity,例如:
public interface OnButtonClicked {
public void updateUI();
}
在MainMenu活动中实现此界面:
public class MainMenu extends AppCompatActivity implements onButtonClicked
创建适配器时,将活动对象传递给适配器:
适配器内的声明私有变量:
onButtonClicked interface;
并在适配器构造函数中初始化它:
ListViewAdapter(OnButtonClicked interface)
{
this.interface = interface;
}
最后,当在适配器中检测到click事件时,使用onButtonClicked updateUI方法更新父活动ui(在UI线程上运行)
推荐阅读有关接口的内容。
或者,您始终可以尝试将适配器上下文强制转换为调用活动:
MainMenu activity = (MainMenu) context;
activity.updateUI();