我已經創建了可展開的列表視圖。它包含父項和子項目。如果我單擊加號或減號imageview,它應該採用特定的子名稱並傳遞到下一個簽入活動。動態文本視圖是基於從前一個適配器檢索到的子名稱創建的,但只有最後一次點擊的子項名稱顯示在該活動中。我想顯示所有已單擊的子項名稱,並且該項目名稱在該活動中動態顯示。是否顯示所有項目名稱?在android中的onclick動態添加textview
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();
}
}
}
你應該使用列表視圖或添加的TextView按ArrayList的項目 – Nisarg
如何創建的TextView作爲ArrayList的項目 –
我建議你使用的ListView它容易 – Nisarg