Pass checked multiple images from one activity to another - android

I have a custom listview with checkbox imageview and textview and there is a button below listview. On clicking of that button i have to pass all the checked item details to another activity and show in other listview. I am able to pass selected value but not able to pass images. Images are in drawable folder. Please help me thanks.
Here is the code:
subscribe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String data = "";
ArrayList<Item> stList = ((OurServiceAdapter) nAdapter)
.getAllData();
for (int i = 0; i < stList.size(); i++) {
Item singleStudent = stList.get(i);
if (singleStudent.isCheckbox() == true) {
name.add(singleStudent.getName().toString());
img.add(singleStudent.getImage());
data = data + "\n" + singleStudent.getName().toString();
}
}
// byte[] imgs = singleStudent.getImage();
Intent intent = new Intent(ActivityOurServices.this, ActivityServicesForm.class);
intent.putStringArrayListExtra("key", name);
//intent.putIntegerArrayListExtra("img", img);
startActivity(intent);
}
});
}

you can pass all image as Integer arrayList :
//first : for each imageView inside list you must to set tag ,for example ;
imageView0.setTag(R.drawable.image_0);
imageView1.setTag(R.drawable.image_1);
.
.
.
imageViewN.setTag(R.drawable.image_N);
now when you press button :
ArrayList<Integer> checkedImageSrcId = new ArrayLIst<Integer> ;
for (int i = 0; i < stList.size(); i++) {
Item singleStudent = stList.get(i);
if (singleStudent.isCheckbox() == true) {
name.add(singleStudent.getName().toString());
// img.add(singleStudent.getImage());
// you most to get checked imageViews Tag and i dont have an idea about how to get those
checkedImageSrcId.add( getImageViewTagAtPostion(i));
data = data + "\n" + singleStudent.getName().toString();
}
}
// byte[] imgs = singleStudent.getImage();
Intent intent = new Intent(ActivityOurServices.this, ActivityServicesForm.class);
intent.putStringArrayListExtra("key", name);
intent.putIntegerArrayListExtra("img", checkedImageSrcId);
startActivity(intent);

Related

How to add data in array on button click?

int size = mcq.size();
String arr[] = null;
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr[i] = tv1.getText().toString();
// Log.e("Array",arr[i]);
} else if (op2.isPressed()) {
arr[i] = tv2.getText().toString();
//Log.e("Array",arr[i]);
} else if (op3.isPressed()) {
arr[i] = tv3.getText().toString();
// Log.e("Array",arr[i]);
} else if (op4.isPressed()) {
arr[i] = tv4.getText().toString();
//Log.e("Array",arr[i]);
}
I am trying to store the data in an array when the button is pressed,but it always shows null.And when the for loop is over I want to display my array.
here , Arrays in Java have a size so u cannot do like this. Instead of this
use list,
ArrayList<String> arr = new ArrayList<String>();
Inorder to do using String array :
String[] arr = new String[SIZEDEFNE HERE];
For ur answer :
ArrayList<String> arr = new ArrayList<String>();
int i;
{
for (i = 0; i < size; i++) {
if (op1.isPressed()) {
arr.add(tv1.getText().toString());
} else if (op2.isPressed()) {
arr.add(tv2.getText().toString());
} else if (op3.isPressed()) {
arr.add(tv3.getText().toString());
} else if (op4.isPressed()) {
arr.add(tv4.getText().toString());
}
Retrive value using
String s = arr.get(0);
This is because your string array is null. Declare it with the size as
String[] arr = new String[size];
Try Array List. use the following code in your main java
final ArrayList<String> list = new ArrayList<String>();
Button button= (Button) findViewById(R.id.buttonId);
final EditText editText = (EditText) findViewById(R.id.editTextId)
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
list.add(editText.getText().toString());
}
});
To avoid duplication in arraylist .You can use arraylist for faster then String array
ArrayList<String> list = new ArrayList<String>();
list.add("Krishna");
list.add("Krishna");
list.add("Kishan");
list.add("Krishn");
list.add("Aryan");
list.add("Harm");
System.out.println("List"+list);
HashSet hs = new HashSet();
hs.addAll(list);
list.clear();
list.addAll(hs);

Why does variables changes depending on android version?

I got ListView with simpleAdapter. If I click on some item in my List view it show me 2 diferent variables.
Some code:
// Listview Data
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(Invoice_numberArr.length);
Map<String, Object> m;
for(int i = 0; i<Invoice_numberArr.length; i++){
int str = Integer.parseInt(Invoice_numberArr[i]);
int str2 = Integer.parseInt(InvoiceNumberForDetails);
if(str == str2){
m = new HashMap<String, Object>();
m.put(ATT_INU, Invoice_numberArr[i]);
m.put(ATT_ANU, Article_numberArr[i]);
m.put(ATT_AMO, AmountArr[i]);
m.put(ATT_PRE, PriceArr[i]);
m.put(ATT_TPR, Total_priceArr[i]);
m.put(ATT_SDE, Sale_designationArr[i]);
data.add(m);
}
}
String[] from = {ATT_INU, ATT_ANU, ATT_AMO, ATT_PRE, ATT_TPR, ATT_SDE};
int[] till = {R.id.txtInvoice_number, R.id.txtArticle_number, R.id.txtAmount, R.id.txtPrice, R.id.txtTotalPrice, R.id.txtSale_designation};
//activities elements
invoices_list_details_view = (ListView)findViewById(R.id.invoices_list_details_view);
invoicesDetailsSAdapter = new SimpleAdapter(InvoicesDetails.this, data, R.layout.invoices_list_item_details_view, from , till);
invoices_list_details_view.setAdapter(invoicesDetailsSAdapter);
Utility Utility = new Utility();
Utility.setListViewHeightBasedOnChildren(invoices_list_details_view);
//if item clicked
invoices_list_details_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String positions = parent.getAdapter().getItem(position).toString();
//int positions = parent.getAdapter().;
parent.getAdapter();
int indexPos = -1; // if doesnt exist
for (int i = 0; i < Invoice_numberArr.length; i++) {
if (positions.contains("article_numbers="+Article_numberArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"amounts="+AmountArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"prices="+PriceArr[i]+", "+"total_prices="+Total_priceArr[i])) {
indexPos = i;
break;
} else if (positions.contains("prices="+PriceArr[i]+", "+"article_numbers="+Article_numberArr[i]+", "+"amounts="+AmountArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"total_prices="+Total_priceArr[i])){
indexPos = i;
break;
}
}
//some int to transfer it to position class
int integerr = indexPos;
Intent invoices_details_position = new Intent(InvoicesDetails.this, InvoicesDetailsPosition.class);
invoices_details_position.putExtra("saleDesignationForPosition", Sale_designationArr[indexPos]);
invoices_details_position.putExtra("articleNumberForPosition", Article_numberArr[indexPos]);
invoices_details_position.putExtra("sequenceNumberForPosition", Sequence_numberArr[indexPos]);
invoices_details_position.putExtra("amountForPosition", AmountArr[indexPos]);
invoices_details_position.putExtra("warehouseForPosition", WarehouseArr[indexPos]);
invoices_details_position.putExtra("containerDescriptionForPosition", Container_descriptionArr[indexPos]);
invoices_details_position.putExtra("priceForPosition", PriceArr[indexPos]);
invoices_details_position.putExtra("discountForPosition", DiscountArr[indexPos]);
invoices_details_position.putExtra("totalPriceForPosition", Total_priceArr[indexPos]);
startActivity(invoices_details_position);
}
});
So if clicked on my item from ListView under android 4.3 till 5.0 varible
String positions = parent.getAdapter().getItem(position).toString();
shows me
string " {prices=10.8, article_numbers=0736, amounts=1, sale_designations=Pfirsichlikör, invoice_numbers=1, total_prices=10.8} "
And if I clicked under Android version 5.1 its shows me
string " {article_numbers=0736, sale_designations=Pfirsichlikör, amounts=1, invoice_numbers=1, prices=10.8, total_prices=10.8} "
Because that I need to check it twice with that
for (int i = 0; i < Invoice_numberArr.length; i++) {
if (positions.contains("article_numbers="+Article_numberArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"amounts="+AmountArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"prices="+PriceArr[i]+", "+"total_prices="+Total_priceArr[i])) {
indexPos = i;
break;
} else if (positions.contains("prices="+PriceArr[i]+", "+"article_numbers="+Article_numberArr[i]+", "+"amounts="+AmountArr[i]+", "+"sale_designations="+Sale_designationArr[i]+", "+"invoice_numbers=" + Invoice_numberArr[i]+", "+"total_prices="+Total_priceArr[i])){
indexPos = i;
break;
}
}
So my question now why does it changes depending on Android version ?
Thx for help.
Thank #Mike M. LinkedHashMap s instead HashMap s works for all Android

Swipeable-Cards Adapter get Item Info

I m using this framework, I already trying many times for making this problem, but I cant do it. I already asking on stackoverflow but no one cant help me. Actually I m tried.
I m using this framework : https://github.com/kikoso/Swipeable-Cards
And I m using SimpleCardStackAdapter like this :
for (int i = 0; i < user.length(); i++) {
final JSONObject c = user.getJSONObject(i);
// Storing JSON item in a Variable
String id = c.getString(user_id);
String name = c.getString(username);
final String email = c.getString(text);
String image1 = c.getString(imageUrl);
String range1 = c.getString(range);
String msgId = c.getString(postId);
// adapter.add(new CardModel(name, email, image1));
//Set JSON Data in TextView
Log.i("image1image1image1image1", image1);
// CardModel cardModel = new CardModel(" cardModel", " CardModel", r.getDrawable(R.drawable.picture1));
card = new CardModel(name, email, image1);
card.setOnClickListener(new CardModel.OnClickListener() {
#Override
public void OnClickListener() {
Log.i("Swipeable Cards", "I am pressing the card");
// Intent no = new Intent(HomeListview.this, YayDetailActivity.class);
/// startActivity(no);
}
});
card.setOnCardDimissedListener(new CardModel.OnCardDimissedListener() {
#Override
public void onLike(CardModel card) {
Log.i("Swipeable Cards", "I dislike the card");
}
#Override
public void onDislike(CardModel card) {
Log.i("Swipeable Cards", "I like the card");
// new sendNewYay().execute(sharedToken, card.getTitle());
Toast.makeText(getApplicationContext(), card.getDescription(), Toast.LENGTH_SHORT).show();
}
});
// I m added adapter
adapter.add(card);
mCardContainer.setAdapter(adapter);
}
At the onDislike method, I need to get item name.
in this line : new sendNewYay().execute(sharedToken, name);
I send the item name, But it dont work.
1.How can I get the item name, in this method?
2.I have two button, one of them for onLike method, another one for onDislike Method. Ho can I triggered this two method with my button?
Thank you.
Decleare two variable global as string
String itemname;
try {
JSONArray c = new JSONArray(user.toString());
for (int i = 0 ; i < c.length();i++) {
String id = c.getString(user_id);
String name = c.getString(username);
final String email = c.getString(text);
String image1 = c.getString(imageUrl);
String range1 = c.getString(range);
String msgId = c.getString(postId);
System.out.println("Position : " + "" + i + ""+ c.getString(i));
itemname = name.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Final itemname is " + itemname);

EditText array of existing EditTexts won't be declared right

I am creating an app, in which I create some EditTexts dynamically with ID number.
I want to pass the information from the EditTexts, so I tried to create a EditText array with these and then use the .getText().toString() to save them in a String Array, which I want pass to the next activity.
It seems like it won't create the "editArray[]" in second code part correctly.
Thanks in advance.
Here's my code (EnterNames.java) - Creation of EditTexts -> Succesful
protected void NumberOfEditText()
{
View VertLayout = (LinearLayout) findViewById(R.id.VertLayout);
String SpinValue = getIntent().getExtras().getString("SpinValue");
int intSpinValue = Integer.valueOf(SpinValue);
editTextCount = intSpinValue;
EditText[] editTextArray = new EditText[editTextCount];
for (int i = 0; i < editTextCount; i++)
{
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setId(i+1000);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((LinearLayout) VertLayout).addView(editTextArray[i]);
}
}
Second code (EnterNames.java) - Passing data to next activity -> Failure.
By testing I think the problem is the for-loops (the editArray returns null)
public void Go(View view)
{
setContentView(R.layout.activity_enter_names);
String NameArray [] = new String [editTextCount];
EditText editArray [] = new EditText [editTextCount];
Intent intent = new Intent(this, RandomGeneration.class);
for (int i = 0; i < editTextCount; i++)
{
editArray[i] = (EditText) findViewById(i+1000);
}
for (int i = 0; i < editTextCount; i++)
{
NameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", NameArray);
intent.putExtras(extras);
startActivity(intent);
}
According to Activity.findViewById() documentation, this method will search for views in your XML, not in views added in Java.
The simple way to do what you want:
class EnterNames extends Activity {
private int editTextCount;
private EditText[] editTextArray;
protected void NumberOfEditText() {
LinearLayout vertLayout = (LinearLayout) findViewById(R.id.VertLayout);
editTextCount = Integer.valueOf(getIntent().getExtras().getString("SpinValue"));
editTextArray = new EditText[editTextCount];
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
for (int i = 0; i < editTextCount; i++) {
String Name = "Name " + (i+1);
editTextArray[i] = new EditText(this);
editTextArray[i].setText(Name);
editTextArray[i].setTextSize(20);
editTextArray[i].setFilters( new InputFilter[] { new InputFilter.LengthFilter(15) } );
editTextArray[i].setSelectAllOnFocus(true);
editTextArray[i].setSingleLine(true);
editTextArray[i].setLayoutParams(params);
vertLayout.addView(editTextArray[i]);
}
}
[…]
public void Go(View view)
{
// This will delete views added to your LinearLayout, don't use it!
//setContentView(R.layout.activity_enter_names);
String nameArray[] = new String[editTextCount];
for (int i = 0; i < editTextCount; i++) {
nameArray[i] = editArray[i].getText().toString();
}
Bundle extras = new Bundle();
extras.putInt("NumberofNames", editTextCount);
extras.putStringArray("NameArray", nameArray);
Intent intent = new Intent(this, RandomGeneration.class);
intent.putExtras(extras);
startActivity(intent);
}
}
Why you are calling setContentView(R.layout.activity_enter_names); in public void Go(View view)? You overide Activities layout in which you previously has added EditText views and all your findById() returns null.
Your code should work if you remove setContentView(R.layout.activity_enter_names); from Go function.
setContentView(R.layout.activity_enter_names); should be called single time in Activities onCreate function.

Creating dynamic variable in Android

I have multiple strings like st1, st2, st3, st4, st5, st6, st7. now i want to get that strings dynamically through for loop using its counter(in my case int i). like "st"+ i , but the android doesnot accept it so what should i do to get the string dynamically. for more info here is the chunk of the code
String image1 = cData.getString(cData.getColumnIndex("fld_image_url1"));
String image2 = cData.getString(cData.getColumnIndex("fld_image_url2"));
String image3 = cData.getString(cData.getColumnIndex("fld_image_url3"));
String image4 = cData.getString(cData.getColumnIndex("fld_image_url4"));
ArrayList<String> imagesArray = new ArrayList<String>();
//for adding the string in the arraylist dynamically
for(int i=1;i<=4;i++){
if("image"+i!=null){
imagesArray.add(String.valueOf("image"+ i));
}
}
How about something like:
List<String> imagesArray = new ArrayList<String>();
for (int i = 1; i <= 4; i++) {
String image = cData.getString(cData.getColumnIndex("fld_image_url" + i));
if (image != null) {
imagesArray.add(image);
}
}
if you want to get value from cursor dynamically then change your code as:
//for adding the string in the arraylist dynamically
for(int i=1;i<=4;i++){
int count = cData.getColumnIndex("fld_image_url"+i));
if(count != -1)
imagesArray.add(cData.getString(count));
}

Categories

Resources