我的网格视图内有一个按钮,我想在单击该按钮(位于网格视图内)时将数据发送到另一个 activity
I have a button inside my grid view, I want to send data to another activity when the button (which is inside grid view) is clicked
我google了一下,发现自定义适配器可以解决这个问题,但是无法在这段代码中实现,谁能帮我解决一下。
我所有的网格视图都在这个片段中工作。
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private GridView gridView;
String canWaterName[] = {"Bisleri","Aqua Gold","Kinley"};
String canWaterPrice[] = {"Rs 90","Rs 45","Rs 110"};
int canWaterImage[] = {R.drawable.canwater,R.drawable.canwater,R.drawable.canwater};
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
gridView = root.findViewById(R.id.gridview);
CustomAdapter customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), OrderSummary.class);
intent.putExtra("name",canWaterName[i]);
intent.putExtra("price",canWaterPrice[i]);
intent.putExtra("image",canWaterImage[i]);
startActivity(intent);
}
});
return root;
}
public class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return canWaterImage.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
TextView name = view1.findViewById(R.id.canwaterName);
TextView price = view1.findViewById(R.id.canwaterPrice);
ImageView image = view1.findViewById(R.id.images);
Button buy = view1.findViewById(R.id.buyButton);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
name.setText(canWaterName[position]);
price.setText(canWaterPrice[position]);
image.setImageResource(canWaterImage[position]);
return view1;
}
}
}
fragment_Home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gridBackground"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="15dp"
android:clickable="true"
android:numColumns="2"
app:layout_constraintBottom_toBottomOf="parent"></GridView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的网格视图模板。
row_data.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/gridviewdata"
android:layout_width="wrap_content"
android:background="#fff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content">
<TextView
android:id="@+id/canwaterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bisleri"
android:textSize="14dp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/images"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/canwaterPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Rs 90"
android:textSize="14dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.463"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canwaterName" />
<ImageView
android:id="@+id/images"
android:layout_width="140dp"
android:layout_height="140dp"
android:src="@drawable/canwater"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buyButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:text="BUY"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.14"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canwaterPrice"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintWidth_percent="0.45" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想在单击网格视图中的按钮时向此 activity 发送数据。
activity_ordered_summary.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderSummary">
<ImageView
android:id="@+id/canWaterOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/canwater" />
<TextView
android:id="@+id/canWaterNameOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Bisleri"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canWaterOrder" />
<TextView
android:id="@+id/canWaterPriceOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="120dp"
android:text="Rs 90"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.485"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canWaterNameOrder"
app:layout_constraintVertical_bias="0.29000002" />
</androidx.constraintlayout.widget.ConstraintLayout>
OrderedSummary.java
public class OrderSummary extends AppCompatActivity {
TextView canWaterName,canWaterPrice;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_summary);
canWaterName = findViewById(R.id.canWaterNameOrder);
canWaterPrice = findViewById(R.id.canWaterPriceOrder);
image = findViewById(R.id.canWaterOrder);
Intent intent = getIntent();
canWaterName.setText(intent.getStringExtra("name"));
canWaterPrice.setText(intent.getStringExtra("price"));
image.setImageResource(intent.getIntExtra("image",0));
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
if(view.getId() == R.id.buyButton) {
Intent intent = new Intent(getActivity(), OrderSummary.class);
intent.putExtra("name", canWaterName[i]);
intent.putExtra("price", canWaterPrice[i]);
intent.putExtra("image", canWaterImage[i]);
startActivity(intent);
}
}
});
内部自定义适配器
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
final TextView name = view1.findViewById(R.id.canwaterName);
final TextView price = view1.findViewById(R.id.canwaterPrice);
final ImageView image = view1.findViewById(R.id.images);
name.setText(canWaterName[position]);
price.setText(canWaterPrice[position]);
image.setImageResource(canWaterImage[position]);
final Button buy = view1.findViewById(R.id.buyButton);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});
return view1;
}
我google了一下,发现自定义适配器可以解决这个问题,但是无法在这段代码中实现,谁能帮我解决一下。
我所有的网格视图都在这个片段中工作。
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private GridView gridView;
String canWaterName[] = {"Bisleri","Aqua Gold","Kinley"};
String canWaterPrice[] = {"Rs 90","Rs 45","Rs 110"};
int canWaterImage[] = {R.drawable.canwater,R.drawable.canwater,R.drawable.canwater};
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
gridView = root.findViewById(R.id.gridview);
CustomAdapter customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), OrderSummary.class);
intent.putExtra("name",canWaterName[i]);
intent.putExtra("price",canWaterPrice[i]);
intent.putExtra("image",canWaterImage[i]);
startActivity(intent);
}
});
return root;
}
public class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return canWaterImage.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
TextView name = view1.findViewById(R.id.canwaterName);
TextView price = view1.findViewById(R.id.canwaterPrice);
ImageView image = view1.findViewById(R.id.images);
Button buy = view1.findViewById(R.id.buyButton);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
name.setText(canWaterName[position]);
price.setText(canWaterPrice[position]);
image.setImageResource(canWaterImage[position]);
return view1;
}
}
}
fragment_Home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gridBackground"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="15dp"
android:clickable="true"
android:numColumns="2"
app:layout_constraintBottom_toBottomOf="parent"></GridView>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的网格视图模板。
row_data.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/gridviewdata"
android:layout_width="wrap_content"
android:background="#fff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content">
<TextView
android:id="@+id/canwaterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bisleri"
android:textSize="14dp"
android:textStyle="normal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.456"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/images"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/canwaterPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Rs 90"
android:textSize="14dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.463"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canwaterName" />
<ImageView
android:id="@+id/images"
android:layout_width="140dp"
android:layout_height="140dp"
android:src="@drawable/canwater"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/buyButton"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:text="BUY"
android:textColor="@color/white"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.14"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canwaterPrice"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintWidth_percent="0.45" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想在单击网格视图中的按钮时向此 activity 发送数据。
activity_ordered_summary.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OrderSummary">
<ImageView
android:id="@+id/canWaterOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/canwater" />
<TextView
android:id="@+id/canWaterNameOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Bisleri"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canWaterOrder" />
<TextView
android:id="@+id/canWaterPriceOrder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="120dp"
android:text="Rs 90"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.485"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/canWaterNameOrder"
app:layout_constraintVertical_bias="0.29000002" />
</androidx.constraintlayout.widget.ConstraintLayout>
OrderedSummary.java
public class OrderSummary extends AppCompatActivity {
TextView canWaterName,canWaterPrice;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_summary);
canWaterName = findViewById(R.id.canWaterNameOrder);
canWaterPrice = findViewById(R.id.canWaterPriceOrder);
image = findViewById(R.id.canWaterOrder);
Intent intent = getIntent();
canWaterName.setText(intent.getStringExtra("name"));
canWaterPrice.setText(intent.getStringExtra("price"));
image.setImageResource(intent.getIntExtra("image",0));
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
{
if(view.getId() == R.id.buyButton) {
Intent intent = new Intent(getActivity(), OrderSummary.class);
intent.putExtra("name", canWaterName[i]);
intent.putExtra("price", canWaterPrice[i]);
intent.putExtra("image", canWaterImage[i]);
startActivity(intent);
}
}
});
内部自定义适配器
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
final TextView name = view1.findViewById(R.id.canwaterName);
final TextView price = view1.findViewById(R.id.canwaterPrice);
final ImageView image = view1.findViewById(R.id.images);
name.setText(canWaterName[position]);
price.setText(canWaterPrice[position]);
image.setImageResource(canWaterImage[position]);
final Button buy = view1.findViewById(R.id.buyButton);
buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});
return view1;
}