N
N
nikita_chiru2016-05-25 04:45:20
Android
nikita_chiru, 2016-05-25 04:45:20

How to add the following to the database?

How to add text and an image from the list to the database at the click of a button?
Here is the text of the list creation
1) additional adapter
public class ListData{
String title;//Product name
int price;//Product price
int image;
ListData(String _title, int _price, int _image){
title= _title;
price= _price;
image=_image;
}
}
2) List creation
public View getView(int position, View convertView, ViewGroup parent) {
ListData p=((ListData) getItem(position));
View v = inflater.inflate(R.layout.item, parent, false);
((TextView) v.findViewById(R.id.textView1)).setText(p.title);
((TextView) v.findViewById(R.id.textView2)).setText(p.price+"rub");
((ImageView) v.findViewById(R.id.imageView1)).setImageResource(p.image);
returnv;
}
------------------------------------------------- --------------------------------------
------------ -------------------------------------------------- ---------------------------------
------------------------ -------------------------------------------------- ------------
it is necessary that when case 0 is pressed, title, price, image are added to the database
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
btnCorz = (Button) findViewById(R.id.btnCorz);
btnCorz.setOnClickListener(this);
SwipeMenuListView listView = (SwipeMenuListView) findViewById(R.id.listView);
List catalog = new ArrayList<>();
catalog.add(new ListData("first", 199, R.drawable.saiman));
catalog.add(new ListData("second", 299, R.drawable.california));
catalog.add(new ListData("third", 399, R.drawable.dragon));
catalog.add(new ListData("fourth", 499, R.drawable.alkap));
final CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.item, catalog);
listView.setAdapter(adapter);
@Override
public void create(SwipeMenu menu) {
//create an action that will be shown on swiping an item in the list
SwipeMenuItem item1 = new SwipeMenuItem(
getApplicationContext());
item1.setBackground(new ColorDrawable(Color.DKGRAY));
// set width of an option (px)
item1.setWidth(100);
item1.setIcon(R.drawable.ic_action_corz);
item1.setTitleSize(18);
item1.setTitleColor(Color.WHITE);
menu.addMenuItem(item1);
SwipeMenuItem item2 = new SwipeMenuItem(
getApplicationContext());
// set item background
item2.setBackground(new ColorDrawable(Color.RED));
item2.setWidth(100);
item2.setIcon(R.drawable.ic_action_like);
item2.setTitleSize(18);
item2.setTitleColor(Color.WHITE);
menu.addMenuItem(item2);
}
};
//set MenuCreator
listView.setMenuCreator(creator);
// set SwipeListener
listView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
@Override
public void onSwipeStart(int position) {
// swipe start
}
@Override
public void onSwipeEnd(int position) {
// swipe end
}
});
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
String value3 = adapter.getItem(position).title;
int value2 = adapter.getItem(position).price ;
int value1 = adapter.getItem(position).image;
switch(index) {
case 0:
break;
case 1:
Toast.makeText(getApplicationContext(), "Action 2 for " + value3 + value2, Toast.LENGTH_SHORT ).show();
break;
}
return false;
}
});
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question