Barry O'Reilly Barry O'Reilly - 13 days ago 5
Android Question

Custom Action_Bar with textview android

I have a custom action bar as shown in the image below:

Image

I also have list view. In each row of the list, it contains plus and minus images button. I want to show when I press plus button, the action bar textView should be show 1, and if i press plus button again this should increase by one and show 2.

Please check my code below and let me know how to solve this issue.

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;
}
}


Below is my class adapter

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;

}


}

AI. AI.
Answer

Try update your code with this

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;   
}
Comments