gpt4 book ai didi

android - 更新 RecyclerView 的 EditText 组件中的文本

转载 作者:行者123 更新时间:2023-11-30 00:39:36 26 4
gpt4 key购买 nike

我有一个 RecyclerView,每个项目都包含一个 EditText。我使用 startActivityForResult 开始另一个 Activity 。从 Activity 返回后,我想用一些文本更新列表中该项目的 EditText。我怎样才能做到这一点?

最佳答案

请试试这个.....我发布了您要求的完整示例。

<强>1。 activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

<强>2。 row_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="@dimen/activity_vertical_margin"
app:cardElevation="8dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<ImageView
android:id="@+id/food_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="16dp" />

<TextView
android:id="@+id/food_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/food_preview"
android:text="Row Title"
android:textSize="30sp" />

<EditText
android:id="@+id/food_order_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/food_name"
android:layout_toRightOf="@+id/food_preview"
android:hint="Order Count" />

</RelativeLayout>

</android.support.v7.widget.CardView>

<强>3。 RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private List<Data> list = Collections.emptyList();
private Context context;
private OnItemClick onItemClick;

public RecyclerViewAdapter(List<Data> list, Context context, OnItemClick onItemClick) {
this.list = list;
this.context = context;
this.onItemClick = onItemClick;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
ViewHolder holder = new ViewHolder(v);
return holder;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
holder.food_name.setText(list.get(position).food_name);
holder.food_order_count.setText(list.get(position).food_order_count);
holder.food_preview.setImageResource(R.mipmap.ic_launcher);

//animate(holder);

}

@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}

// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, Data data) {
list.add(position, data);
notifyItemInserted(position);
}

// Remove a RecyclerView item containing a specified Data object
public void remove(Data data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

CardView cv;
TextView food_name;
EditText food_order_count;
ImageView food_preview;

ViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
food_name = (TextView) itemView.findViewById(R.id.food_name);
food_order_count = (EditText) itemView.findViewById(R.id.food_order_count);
food_preview = (ImageView) itemView.findViewById(R.id.food_preview);

itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
if(onItemClick != null){
onItemClick.onClick(v, list.get(getAdapterPosition()), getAdapterPosition());
}
}
}

public interface OnItemClick{
void onClick(View view, Data data, int pos);
}
}

4.MainActivity.java

public class MainActivity extends AppCompatActivity {
private List<Data> list;
private int position;
private RecyclerViewAdapter adapter;

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

list = fill_with_data();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
adapter = new RecyclerViewAdapter(list, this, mOnItemClick);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

}

RecyclerViewAdapter.OnItemClick mOnItemClick = new RecyclerViewAdapter.OnItemClick() {
@Override
public void onClick(View view, Data data, int pos) {
position = pos;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, 2);
}
};

public List<Data> fill_with_data() {

List<Data> data = new ArrayList<>();

data.add(new Data("Food 1", " ", ""));
data.add(new Data("Food 2", " ", ""));
data.add(new Data("Food 3", "", ""));
data.add(new Data("Food 4", "", ""));
data.add(new Data("Food 5", "", ""));
data.add(new Data("Food 6", "", ""));
data.add(new Data("Food 7", "", ""));
data.add(new Data("Food 8", "", ""));
data.add(new Data("Food 9", "", ""));
data.add(new Data("Food 10", "", ""));
data.add(new Data("Food 11", "", ""));
data.add(new Data("Food 12", "", ""));
data.add(new Data("Food 13", "", ""));
data.add(new Data("Food 14", "", ""));
return data;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
String count = data.getStringExtra("COUNT");
Data update_data = list.get(position);
update_data.setFood_order_count(count);
// **This line do magic** which your asked.
adapter.notifyItemChanged(position, update_data);
}
}
}

<强>5。 activity_second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>

6. SecondActivity.java

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String count = "10";//Hardcoded value; replace your runtime value;
Intent intent = new Intent();
intent.putExtra("COUNT", count);
setResult(2, intent);
finish();
}
});
}
}

7. AndroidManifest.xml

    <activity android:name=".MainActivity">
.......
.......
</activity>

<activity android:name=".SecondActivity"/>

关于android - 更新 RecyclerView 的 EditText 组件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42748353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com