我创建了可扩展列表视图。它包含父项和子项。如果我单击加号或减号imageview,它应该采用特定的子名称并传递给下一个checkin-activity。动态文本视图是基于从先前的适配器检索到的子名称创建的。但是只有最后一次单击的子项名称显示在该活动中。我想显示所有单击的子项名称,并且该项名称在该活动中动态显示。用于显示所有项目名称?
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();
}
}
}
答案 0 :(得分:1)
您需要根据需要创建空TextView数组:
让我们说
TextView[] textviewArray = new TextView[10]; // Empty Textview array with size 10
然后你必须循环它以在LinearLayout中动态添加Textview:
for (int i = 0; i < 10; i++) {
TextView tv = new TextView(this); // Create Textview
tv.setText("Set Child Name Here As Per i Value");
linearLayout.addView(tv);
textviewArray [i] = tv; // save reference of your Textview
}
希望它会对你有所帮助。
答案 1 :(得分:1)
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);
}
});
在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);