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, 내 변화를보고하십시오 : 나는 중 하나를 업데이트 할 TextView
updateSpecificViewItem("theID")
를 호출하여 클래스 MainActivity
내부.
2 : 주어진 id
에있는 항목의 위치를 getPositionBaseOnItemID("theID")
으로 가져옵니다.
3 : 마지막으로 특정 항목을 업데이트하려면 updateTextView(int position)
메서드를 호출하고 싶습니다.
지금 당장 직면 한 유일한 문제는 number 3
입니다. 전체 항목이 아닌 text2
만 어떻게 업데이트 할 수 있습니까?
더 설명 할 수 있습니까? –
어디에서 updateTextView 메서드를 호출하고 있습니까? 어떤 액션에서 텍스트를 변경하는 메소드를 호출해야합니까? – Chithra
늦게 답변을 드려 죄송합니다. mainActivity 클래스에서 updateTextView 메서드를 호출하고 싶습니다. – Polar