2016-07-06 5 views
0

확장 가능한 목록보기를 만들었습니다. 상위 항목과 하위 항목을 포함하고 있습니다. 더하기 또는 빼기 imageview를 클릭하면 특정 하위 이름을 받아 다음 체크인 활동으로 넘어갑니다. 동적 텍스트 뷰는 이전 어댑터의 검색된 하위 이름을 기반으로 작성됩니다. 그러나 마지막으로 클릭 한 하위 항목 이름 만 해당 활동에 표시됩니다. 클릭 한 하위 항목 이름을 모두 표시하고 그 항목 이름이 해당 활동에 동적으로 표시됩니다. 모든 항목 이름을 표시하려면 어떻게합니까?android에서 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 

가정 해 봅시다 : 당신은 당신의 필요에 따라 빈 텍스트 뷰 배열를 만들 필요가

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

에 따라 텍스트 뷰를 추가해야 –

+0

로 텍스트 뷰를 당신이 쉬운 – 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에서

관련 문제