2017-12-09 4 views
1

RecyclerView을 사용하여 android에서 작업 중이며, 사용자 정의 행에 2 TextView이 있다고 가정하면 TextView에있는 텍스트를 동적으로 변경하고 싶습니다. 어떻게해야합니까? ?RecyclerView에서 특정보기를 동적으로 업데이트

나는 나의 어댑터 클래스

public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> { 
    Context mContext; 
    public List<ModelClientInformation> onlineList; 

    public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){ 
     this.mContext = mContext; 
     this.onlineList = modelOnlineList; 
    } 

    public class TheViewHolder extends RecyclerView.ViewHolder { 

     TextView text1, text2; 

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

      text1 = (TextView)view.findViewById(R.id.text1); 
      text2 = (TextView)view.findViewById(R.id.text2); 
     } 
    } 

    @Override 
    public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.recyclerview_row, parent, false); 
     return new TheViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(TheViewHolder holder, int position) { 
     final ModelClientInformation info = onlineList.get(position); 

     holder.text1.setText(holder.getText1()); 
     holder.text1.setText(holder.getText2()); 
    } 


    /**Function to Return the size of List**/ 
    @Override 
    public int getItemCount() { 
     return onlineList.size(); 
    } 

    /**Function to Clear the List**/ 
    public void clear(){ 
     onlineList.clear(); 
    } 


    /**Possibly way to update one of the TextView here**/ 
    public void updateTextView(int position){ 
     //what should I do to update the TextView 
    } 

    /*Get the position of item inside data list base on the given ID*/ 
    public int getPositionBaseOnItemID(String theID) { 

    int length = onlineList.size(); 
    for (int i = 0; i < length; i ++) { 
     if(onlineList.get(i).getItemID().equals(theID)) { 
      return i; 
     } 
    } 
    return -1; //Item not found 
} 
} 

을하고 뽀조 내 MainActivity에

public class MainActivity extends AppCompatActivity { 

    private RecyclerView recyclerView; 
    private AdaptersOnline adaptersOnline; 
    private RecyclerView.LayoutManager mLayoutManager; 
    private List<ModelClientInformation> modelOnlineLists = new ArrayList<>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mContext = getApplicationContext(); 

     mLayoutManager = new LinearLayoutManager(this); 
     adaptersOnline = new AdaptersOnline(this, modelOnlineLists); 
     recyclerView = (RecyclerView)findViewById(R.id.recyclerView); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(adaptersOnline); 
    } 

    //call this to new row 
    public void initializeClient(String id, String data1, String data2){ 
     this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2)); 
     adaptersOnline.notifyDataSetChanged(); 
    } 

    //Call this method to update textview 
    public void updateSpecificViewItem(String theID){ 
     //get position base on the ID   
     adaptersOnline.updateTextView(
     adaptersOnline.getPositionBaseOnItemID(theID)); 
    } 

} 

를 다음 코드가 할

public ModelClientInformation class{ 

    private String theID, text1, text2; 

    public ModelClientInformation(String theID, String text1, String text2){ 
     this.theID = theID; 
     this.text1 = text1; 
     this.text2 = text2; 
    } 

    public String getItemID(){ 
     return theID; 
    } 

    public String getText1(){ 
     return text1; 
    } 

    public String getText2(){ 
     return text2; 
    } 

} 

내가 어떤 생각이없는거야 그것을하기 위해. . . 누구든지 나를 도울 수 있습니까?

UPDATE :

1, 내 변화를보고하십시오 : 나는 중 하나를 업데이트 할 TextViewupdateSpecificViewItem("theID")를 호출하여 클래스 MainActivity 내부.

2 : 주어진 id에있는 항목의 위치를 ​​getPositionBaseOnItemID("theID")으로 가져옵니다.

3 : 마지막으로 특정 항목을 업데이트하려면 updateTextView(int position) 메서드를 호출하고 싶습니다.

지금 당장 직면 한 유일한 문제는 number 3입니다. 전체 항목이 아닌 text2 만 어떻게 업데이트 할 수 있습니까?

+0

더 설명 할 수 있습니까? –

+0

어디에서 updateTextView 메서드를 호출하고 있습니까? 어떤 액션에서 텍스트를 변경하는 메소드를 호출해야합니까? – Chithra

+0

늦게 답변을 드려 죄송합니다. mainActivity 클래스에서 updateTextView 메서드를 호출하고 싶습니다. – Polar

답변

0

어댑터

public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> { 
    Context mContext; 
    public List<ModelClientInformation> onlineList; 

    public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){ 
     this.mContext = mContext; 
     this.onlineList = modelOnlineList; 
    } 

    public class TheViewHolder extends RecyclerView.ViewHolder { 

     TextView text1, text2; 

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

      text1 = (TextView)view.findViewById(R.id.text1); 
      text2 = (TextView)view.findViewById(R.id.text2); 
     } 
    } 

    @Override 
    public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.recyclerview_row, parent, false); 
     return new TheViewHolder(itemView); 
    } 

    @Override 
    public void onBindViewHolder(TheViewHolder holder, int position) { 
     final ModelClientInformation info = onlineList.get(position); 

     holder.text1.setText(holder.getText1()); 
     holder.text1.setText(holder.getText2()); 
    } 


    /**Function to Return the size of List**/ 
    @Override 
    public int getItemCount() { 
     return onlineList.size(); 
    } 

    /**Function to Clear the List**/ 
    public void clear(){ 
     onlineList.clear(); 
    } 

    /**Function to return the Data List**/ 
    public List<ModelClientInformation> getOnlineList(){ 
     return this.onlineList; 
    } 

    /**Function to update the specific Item**/ 
    public void updateTextView(int position, ModelClientInformation newItem){ 
     onlineList.set(position, newItem); //set the item 
     notifyItemChanged(position, newItem); //notify the adapter for changes 
    } 

    /*Get the position of item inside data list base on the given ID*/ 
    public int getPositionBaseOnItemID(String theID) { 

     int length = onlineList.size(); 
     for (int i = 0; i < length; i ++) { 
      if(onlineList.get(i).getItemID().equals(theID)) { 
       return i; 
      } 
     } 
     return -1; //Item not found 
    } 
} 

뽀조

012,314,700

public class MainActivity extends AppCompatActivity { 

    private RecyclerView recyclerView; 
    private AdaptersOnline adaptersOnline; 
    private RecyclerView.LayoutManager mLayoutManager; 
    private List<ModelClientInformation> modelOnlineLists = new ArrayList<>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mContext = getApplicationContext(); 

     mLayoutManager = new LinearLayoutManager(this); 
     adaptersOnline = new AdaptersOnline(this, modelOnlineLists); 
     recyclerView = (RecyclerView)findViewById(R.id.recyclerView); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(adaptersOnline); 
    } 

    //call this to new row 
    public void initializeClient(String id, String data1, String data2){ 
     this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2)); 
     adaptersOnline.notifyDataSetChanged(); 
    } 

    //Call this method to update specific Item 
    public void updateSpecificViewItem(String theID, String newText){  

     int position = adaptersOnline.getPositionBaseOnItemID(theID); // get position base on the ID 
     ModelClientInformation oldItem = adaptersOnline.getOnlineList().get(position); // From my Adapter I created a new method `getOnlineList()` that returns the list item of specific position. 

     ModelClientInformation newItem = new ModelClientInformation(
        oldItem.ItemID(), // get and add the old Item ID 
        oldItem.getText1(), // Get and add the old Text1 
        newText // add the new text for text2 
      ); 

     adaptersOnline.updateTextView(position, newItem); // call updateTextView() from the adapter and pass the position and the newItem. 
    } 

} 

MainActivity 내 문제 해결

이것이 어떻게 수행되어야하는지는 확실치 않지만 특정 목록 항목을 업데이트 할 수 있으며 위치에 대한 뷰베이스를 업데이트 할 수 있습니다.

나에게 아이디어를 준 모두에게 감사합니다 notifyItemChanged()! 이 또한 다른 도움을 바랍니다.

0

당신은 onBindViewHolder (TheViewHolder 홀더, INT 위치 목록 페이로드)

@Override 
    public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) { 

     if (payloads.isEmpty()) { 
      super.onBindViewHolder(holder, position , payloads); 
     }else{ 

      for (Object payload : payloads) { 
       if (payload instanceof String) { 
        holder.textView.setText(payload.toString) 
       } 
       } 
      } 

    } 

을 무시할 그리고 당신은

adapter.notifyItemChanged(position , "an string for example")

를 호출 할 필요가 당신의 텍스트 뷰를 업데이트해야 이것은 당신에게 부분을 제공합니다 보기 업데이트.

희망이 도움이됩니다. 어댑터에서

0

,

String textToUpdate; 

@Override 
public void onBindViewHolder(TheViewHolder holder, int position) { 
    final ModelClientInformation info = onlineList.get(position); 

    holder.text1.setText(holder.getText1());// update the textview u want by setting the "textToUpdate" variable 
    holder.text1.setText(holder.getText2()); 
} 

public void updateTextView(String text){ 
    textToUpdate = text; 
    notifyDatasetChanged(); 
} 

는 희망이 도움이!

0

RecylerView 클래스에서 public static을 만들려면 해당 메서드에 텍스트를 할당하기 만하면됩니다. 그런 다음, 리사이클 러 뷰 인스턴스에 연결하는 클래스에서 onclick 및 ontouch 인터페이스를 추가하여 선택한 텍스트 행의 위치를 ​​저장하십시오. 리사이클 러 뷰 인스턴스를 사용하여 notifyItemChanged을 호출하십시오.

다음은 Documentation의 설명입니다.

notifyItemChanged는 22.1 버전에 추가되었습니다.0

공극 notifyItemChanged (INT 위치)

위치의 항목이 변경되었음을, 등록 관찰자에게 알림. notifyItemChanged (position, null) 호출과 동일합니다.

이것은 구조 변경 이벤트가 아닌 항목 변경 이벤트입니다. 그것은 위치에있는 데이터의 반영이 오래되어 업데이트되어야 함을 나타냅니다. 위치에있는 항목은 동일한 ID를 유지합니다.

+0

내 'DataList'에 '10 item'이 있으면 어떻게 되나요? 특정 항목에서 '항목 중 하나만 변경'하고 싶습니다. – Polar

+0

예, 작동합니다. onTouch 또는 Onclick 인터페이스로 특정 위치를 지정하면됩니다. 그런 다음 해당 위치가있는 메소드를 호출하고 위에서 언급 한대로 어댑터 변경 사항을 알립니다. – Remario

관련 문제