Abserve Tech Abserve Tech - 4 months ago 37
Android Question

Add textview dynamically during onclick in android

I have created the expandable list view.It contain parent and child items.If i click the plus or minus imageview it should take the particular child name and passed on to the next checkin-activity. Dynamic textview is created based on the retrieved child name from previous adapter.But only the last clicked child item name is displayed in that activity.I want to display all clicked child item name and that item name is displayed dynamically in that activity.What to do for displaying all item name?

public class ExpandListAdapter extends BaseExpandableListAdapter {

public Context context;
private ArrayList<Group> groups;
ArrayList<Child> ch_list=new ArrayList<Child>();

public ArrayList<Movie> movieList=new ArrayList<Movie>();
TextView itemname;
LinearLayout checkin_layout,checkin;
private ViewHolder viewHolder; // make it global

public int count1=0;
public class ViewHolder {

TextView tv ;
ImageView food_image;
ImageView minus,plus ;
TextView item_count,amount;

}


public ExpandListAdapter(Context context, ArrayList<Group> groups, TextView itemname, LinearLayout checkin_layout,LinearLayout checkin) {
this.context = context;
this.groups = groups;
this.itemname=itemname;
this.checkin_layout=checkin_layout;
this.checkin=checkin;

}


@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final Child child = (Child) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater inflator = LayoutInflater.from(parent.getContext());
convertView = inflator.inflate(R.layout.detail_list, null);
viewHolder = new ViewHolder();
viewHolder.amount = (TextView) convertView.findViewById(R.id.amount);
viewHolder.tv = (TextView) convertView.findViewById(R.id.type);
viewHolder.food_image = (ImageView) convertView.findViewById(R.id.food_image);
viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus);
viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus);
viewHolder.item_count = (TextView) convertView.findViewById(R.id.count);


convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(child.getName());

viewHolder.item_count.setText(Integer.toString(child.getCount()));
if(count1==0){
checkin_layout.setVisibility(View.GONE);
}

viewHolder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Child modelChild = groups.get(groupPosition).getItems().get(childPosition);
checkin_layout.setVisibility(View.VISIBLE);
if(modelChild.getCount()>=0) // set your count default 0 when you bind data initially
{
int count = (modelChild.getCount()) + 1;
modelChild.setCount(count);
count1=count1+1;
itemname.setText(Integer.toString(count1)+"items");





String name=modelChild.getName();
Movie movie = new Movie();
movie.setcheckin_name(name);
movieList.add(movie);

}
// set your other items if any like above
groups.get(groupPosition).getItems().set(childPosition, modelChild);
notifyDataSetChanged();
}
});
viewHolder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkin_layout.setVisibility(View.VISIBLE);
Child modelChild = groups.get(groupPosition).getItems().get(childPosition);
if(modelChild.getCount()>0) // set your count default 0 when you bind data initially
{ int count = modelChild.getCount() - 1;
modelChild.setCount(count);
count1=count1-1;
itemname.setText(Integer.toString(count1)+"items");
// Checkinpage. ar = new ArrayList<String>();
// String name=modelChild.getName();
// Checkinpage.ar.add(name);
}

// modelChild.setChildName(modelChild.getChildName());
// set your other items if any like above
groups.get(groupPosition).getItems().set(childPosition, modelChild);
notifyDataSetChanged();

}
});


checkin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(context,Checkinpage.class);
ArrayList<Movie> anotherList =new ArrayList<Movie>();

anotherList .addAll(movieList);
Gson gsonMovie = new Gson();
String strMovie = gsonMovie.toJson(anotherList);
Bundle bundle = new Bundle();
bundle.putString("Movies", strMovie);
i.putExtras(bundle);
context.startActivity(i);
}
});

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> chList = groups.get(groupPosition).getItems();
return chList.size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getName());
ExpandableListView eLV = (ExpandableListView) parent;
int count = getGroupCount();
if(count<1){
eLV.expandGroup(groupPosition);
// eLV.setGroupIndicator(null);
}


return convertView;
}



@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}


Checkin_activity:

public class Checkinpage extends Activity {


public static ArrayList<String> ar=new ArrayList<String>();
public static ArrayList<String> name;
public static ArrayList<Movie> movieList;

LinearLayout linearLayout;
checkinadapter adapter;
RecyclerView recycleview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkinpage);
Log.d("name12--", String.valueOf(name));
linearLayout=(LinearLayout)findViewById(R.id.linear);
recycleview= (RecyclerView) findViewById(R.id.listview);
adapter = new checkinadapter(this, movieList);

recycleview.setAdapter(adapter);

LinearLayoutManager horizontalLayoutManagaer
= new LinearLayoutManager(Checkinpage.this, LinearLayoutManager.VERTICAL, false);
recycleview.setLayoutManager(horizontalLayoutManagaer);

}


public class checkinadapter extends RecyclerView.Adapter<checkinadapter.MyViewHolder> {

private final Context mcontext;
private final ArrayList<Movie> movieItems;


public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView amount,type,item_count;
public ImageView minus,plus;
private AdapterView.OnItemClickListener onItemClickListener;

public MyViewHolder(View view) {
super(view);

amount = (TextView) view.findViewById(R.id.amount);
type = (TextView) view.findViewById(R.id.type);

minus = (ImageView) view.findViewById(R.id.minus);
plus = (ImageView) view.findViewById(R.id.plus);
item_count = (TextView) view.findViewById(R.id.count);
}

}

public checkinadapter(Context mcontext, ArrayList<Movie> movieItems) {

this.mcontext = mcontext;
this.movieItems = movieItems;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.checkin_list, parent, false);


return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {


final Movie m = movieItems.get(position);


if (m.getcheckin_name()!=null)
{
if (!m.getcheckin_name().equals("null"))
{
holder. type.setText(m.getcheckin_name());
}}
// if (m.getRes_deliverytime()!=null)
// {
// if (!m.getRes_deliverytime().equals("null"))
// {
// holder.time.setText(m.getRes_deliverytime()+ " mins");
// }}
////

}


@Override
public int getItemCount() {
return movieItems.size();
}
}



}

Answer
Add this in to your project dependencies (build.gradle)
compile 'com.google.code.gson:gson:2.2.4'


checkin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    Intent i=new Intent(context,Checkinpage.class);
    Gson gsonMovie = new Gson();
    String strMovie = gsonMovie.toJson(movieList);
    Bundle bundle = new Bundle();
    bundle.putString("Movies", strMovie);
    intent.putExtras(bundle);    
    context.startActivity(i);
        }
});

In Checkin_activity:

        ArrayList<Movie> movieList=new  ArrayList<Movie>();
        Bundle bundle = this.getIntent().getExtras();
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<Movie>>() {
        }.getType();
        movieList = gson.fromJson(
                bundle.getString("Movies"), type);
Comments