2016-07-06 14 views
0

私は拡大可能なリストビューを作成しました。親アイテムと子アイテムを含んでいます。プラスまたはマイナスの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:

は、あなたがループそれをために持ってその後

TextView[] textviewArray = new TextView[10]; // Empty Textview array with size 10 

言ってみましょう:あなたはあなたの必要性に従って空のTextViewアレイを作成する必要が

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

listviewを使用すると、通常のスクロールビューとしてリストビューをスクロールすることができます – Nisarg

+0

あたりとしてのTextViewを追加する必要があります –

+0

としてのTextViewを、私はあなたがその簡単 – Nisarg

答えて

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

 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); 
あなたはリストビューを使用するか、ArrayListの項目
1

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 
} 

ご希望の場合はお手数ですが、 Checkin_activityで

関連する問題