permanently save new entered data in list view [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i make a sales puchase app like olx. In which user post free add. Problem is when use post add it appeared in listview but when user again login with their device the add will disappear.....
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_adds_layout, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView detailedMessage = (TextView) rowView.findViewById(R.id.detailed_message);
TextView ownerName = (TextView) rowView.findViewById(R.id.owner);
TextView ownerEmail = (TextView) rowView.findViewById(R.id.owner_email);
TextView price = (TextView) rowView.findViewById(R.id.price);
ImageView image = (ImageView) rowView.findViewById(R.id.imageView10);
Button btn33 = (Button) rowView.findViewById(R.id.buybutton);
btn33.setBackgroundResource(R.drawable.buy_corner);
btn33.setOnClickListener(this);
Advertisement dataObj = values[position];
title.setText(dataObj.getTitle());
detailedMessage.setText(dataObj.getDetailedMessage());
ownerName.setText(dataObj.getOwnerName());
ownerEmail.setText(dataObj.getOwnerEmail());
price.setText(String.valueOf(dataObj.getPrice()));
btn33.setTag(price.getText());
if(dataObj.getImageId().contains(">>>")){
String picturePath = dataObj.getImageId().replace(">>>", "");
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}else {
setDrawable(image, dataObj.getImageId());
}
return rowView;
}
private void setDrawable(ImageView image, String drawableName) {
AssetManager manager = image.getContext().getAssets();
// Read a Bitmap from Assets
InputStream open = null;
try {
open = manager.open(drawableName+".jpg");
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (open != null) {
try {
open.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And here is my Xml
<LinearLayout
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="410dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="66dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Ad Title!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
<EditText
android:id="#+id/editTextTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="Enter Title Here" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="66dp" >
<EditText
android:id="#+id/editTextDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Description Here" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Ad\nDescription!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="62dp" >
<EditText
android:id="#+id/editTextOwnerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Name Here" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextOwnerName"
android:layout_alignParentLeft="true"
android:text="Owner\nName!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp" >
<EditText
android:id="#+id/editTextOwnerEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Email id Here" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Owner\nEmail"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="62dp" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Ad Price!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
<EditText
android:id="#+id/editTextPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Enter Price Here" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp" >
<Button
android:id="#+id/buttonAddImage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Add Image"
android:textStyle="bold|italic"
android:typeface="serif" />
<ImageView
android:id="#+id/creatae"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<Button
android:id="#+id/buttonSaveAdd"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Save"
android:textStyle="bold|italic"
android:typeface="serif" />
</LinearLayout>
By click on save add
case R.id.buttonSaveAdd:
EditText editTextTitle = (EditText) findViewById(R.id.editTextTitle);
EditText editTextDes = (EditText) findViewById(R.id.editTextDescription);
EditText editTextOwner = (EditText) findViewById(R.id.editTextOwnerName);
EditText editTextOwnerEmail = (EditText) findViewById(R.id.editTextOwnerEmail);
EditText editTextPrice = (EditText) findViewById(R.id.editTextPrice);
//populating data object from the values received
//from view
String title = editTextTitle.getText().toString();
String description = editTextDes.getText().toString();
String ownerName = editTextOwner.getText().toString();
String ownerEmail = editTextOwnerEmail.getText().toString();
String pricce = editTextPrice.getText().toString();
Advertisement object = new Advertisement(title, description,
ownerName, ownerEmail, currentImageName, Integer.parseInt(pricce), 100);
Button_mak.ads.add(object);
this.finish();
break;

Save them in a local database using SQLite and then reload them back into the listview when the user opens that activity again. I made a blog post recently on using SQLite within Android. It can be found here

Related

Error when generating dynamic content

I'm trying to generate form elements from a template when the user clicks on a button. I created the template and the container layout for the new forms with XML. Its successfully generating the first form where I'm telling it to generate, but when I try to generate more forms beyond the first one its giving me an error: "the specified child already has a parent. You must call removeView() on the child's first parent". Any clue as to what I should do so as to generate more than one element? I've tried changing the id of the newly created forms but that's giving me a null pointer exception and crashing. Thank you.
public class MakeQuestion extends Activity implements OnClickListener{
private static final int MY_BUTTON = 9000;
int templateID = 1;
Button b;
Button target;
View insertPoint;
Button testTemplate;
View v1;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.make_question);
Initialize();
}
public void Initialize(){
//button for adding new forms
b = (Button) findViewById(R.id.makeLayoutButton);
b.setOnClickListener(this);
//get the template form to be duplicated
vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = vi.inflate(R.layout.form_template, null);
//set the params for the element that will have dynamically generated content below it
templateParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//container where forms will be contained in
insertPoint = findViewById(R.id.questionsContainer);
//view where new form will go below
View belowContainer = findViewById(R.id.questionTemplateFake);
//set id for layout params of view
belowContainer.setId(1);
//set rule for new forms to go below view 'belowContainer'
templateParams.addRule(RelativeLayout.BELOW, belowContainer.getId());
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.makeLayoutButton:
v1 = vi.inflate(R.layout.form_template, null);
//add view to the insertPoint
((LinearLayout) insertPoint).addView(v1);
break;
}
}
}
Added forms should go in "questionsContainer" which is set to be below "questionTemplateFake"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/grey_background" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#8B459A"
android:baselineAligned="false"
android:clipToPadding="false" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/logo_small" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="#drawable/settings" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/search" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/relativeLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relative12"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="WHAT IS YOUR QUESTION?" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/save_button"
android:layout_alignBottom="#+id/save_button"
android:layout_alignRight="#+id/textView1"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" />
<Button
android:id="#+id/save_button"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/save_button"
android:layout_marginTop="25dp"
android:text="CREATE AN ANSWER" />
<RelativeLayout
android:id="#+id/questionTemplateFake"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
<Button
android:id="#+id/Button02eew"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<EditText
android:id="#+id/EditText02"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="#+id/Button02"
android:layout_alignParentLeft="true"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/Button02"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/questionsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_below="#+id/questionTemplateFake"
>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/makeLayoutButton"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/scrollView1"
android:layout_marginRight="17dp"
android:layout_marginTop="79dp"
android:background="#drawable/purplebutton"
android:text="MORE OPTIONS"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
</RelativeLayout>
You are trying to add the same instance over and over again. Thus the message that "specific child already exists". You would have to create a new template layout instance with different ID (I suppose) and then try to add it in.
So rather than:
((RelativeLayout) insertPoint).addView(v1, templateParams);
and adding v1 again. Create a new instance
v1 = vi.inflate(R.layout.form_template, null);
set the id for good measures and then add it again in view.
I suppose you don't need to set the id, but you can read more about how id's work here

Wrong textview is being updated

In the onCreate() method I am updating 2 TextViews to be blank. For some reason it is setting two other TextViews to be blank. I have followed my logic and can not see what is going wrong.
here is my onCreate() method.
import android.widget.TextView;
public class MainActivity extends Activity {
private int purpleDrinks; //number of purple drinks
private int greenDrinks; //number of green drinks
private int redDrinks; //number of red drinks
private int blueDrinks; //number of blue drinks
private TextView purpleCount; //number of purple drinks display
private TextView greenCount; //number of green drinks display
private TextView redCount; //number of red drinks display
private TextView blueCount; //number of blue drinks display
private TextView purchaseConfirm; //displays what the user bought
private TextView outOfStock; //lets the user know when the item is out of stock.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( savedInstanceState == null ) // the app just started running
{
purpleDrinks = 10;
greenDrinks = 10;
redDrinks = 10;
blueDrinks = 10;
} // end if
purpleCount = (TextView) findViewById(R.id.purpleCount);
greenCount = (TextView) findViewById(R.id.greenCount);
redCount = (TextView) findViewById(R.id.redCount);
blueCount = (TextView) findViewById(R.id.blueCount);
purchaseConfirm = (TextView) findViewById(R.id.purchaseConfirm);
outOfStock = (TextView) findViewById(R.id.outOfStock);
purchaseConfirm.setText("");
outOfStock.setText("");
purpleCount.setText(purpleDrinks + "");
greenCount.setText(greenDrinks + "");
redCount.setText(redDrinks + "");
blueCount.setText(blueDrinks + "");
}
here is the xml code: the ids are correct.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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:background= "#D5E3EA"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/greenCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_marginTop="26dp"
android:src="#drawable/greencan" />
<ImageButton
android:id="#+id/redCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/greenCann"
android:layout_marginRight="25dp"
android:src="#drawable/redcan" />
<ImageButton
android:id="#+id/purpleCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/redCann"
android:layout_marginRight="25dp"
android:src="#drawable/purplecan" />
<ImageButton
android:id="#+id/blueCann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/purpleCann"
android:layout_marginRight="25dp"
android:src="#drawable/bluecan" />
<Button
android:id="#+id/restockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp"
android:text="#string/restock" />
<TextView
android:id="#+id/purpleCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/purpleCann"
android:layout_marginBottom="18dp"
android:layout_toLeftOf="#+id/purpleCann"
android:text="TextView" />
<TextView
android:id="#+id/redCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/redCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/redCann"
android:text="TextView" />
<TextView
android:id="#+id/greenCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/greenCann"
android:text="TextView" />
<TextView
android:id="#+id/blueCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/blueCann"
android:layout_marginBottom="20dp"
android:layout_toLeftOf="#+id/blueCann"
android:text="TextView" />
<TextView
android:id="#+id/outOfStock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/restockButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="TextView" />
<TextView
android:id="#+id/purchaseConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/outOfStock"
android:layout_alignBottom="#+id/outOfStock"
android:layout_alignLeft="#+id/outOfStock"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/greenCount"
android:layout_toLeftOf="#+id/restockButton"
android:text="#string/greenSoda2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/redCount"
android:layout_alignBottom="#+id/redCount"
android:layout_alignLeft="#+id/textView5"
android:text="#string/redSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/purpleCount"
android:layout_alignBottom="#+id/purpleCount"
android:layout_alignLeft="#+id/textView6"
android:text="#string/purpleSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/blueCount"
android:layout_alignBottom="#+id/blueCount"
android:layout_alignLeft="#+id/textView7"
android:text="#string/blueSoda"
android:textAppearance="?android:attr/textAppearanceMedium" />
here is a screenshot of the app:
does anybody know what is going on?

Inflated items are gone when device orientation changes

Hi i'm using eclipse for deveoping this android app. I'm inflating a layout using inflator and everything works fine.. I'm adding those inflated items one by one to another layout, so when the device's orientation is changed, all those inflated items are gone. The application seems like it has been restarted. But some values are still remaining.. Please help..
public void addNewItem() {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item = li.inflate(R.layout.activity_order_items, null);
orderItemFrameView.add(item);
itemsLayout.addView(item);
setAdditionalFonts(item);
}
private void setAdditionalFonts(View view) {
TextView lblNameTak = (TextView)view.findViewById(R.id.tak_itemNo);
AutoCompleteTextView txtNameTak = (AutoCompleteTextView)view.findViewById(R.id.tak_itemName);
ImageView barScan = (ImageView)view.findViewById(R.id.barcode_scan);
TextView lblRate = (TextView)view.findViewById(R.id.tak_lblRate);
TextView lblQty = (TextView)view.findViewById(R.id.tak_lblQty);
TextView lblTotal = (TextView)view.findViewById(R.id.tak_lblTotal);
TextView rate = (TextView)view.findViewById(R.id.tak_Rate);
EditText2 qty = (EditText2)view.findViewById(R.id.tak_Qty);
TextView total = (TextView)view.findViewById(R.id.tak_Total);
qty.totTextView = total;
qty.rateTextView = rate;
qty.orderTake = this;
qty.index = orderItemFrameView.size() - 1;
qty.sqlDb = SqlDb;
qty.itemName = txtNameTak;
setupBarcodeScan(barScan);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ubuntu-l.ttf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/calibri.ttf");
Typeface tf3 = Typeface.createFromAsset(getAssets(), "fonts/ubu-r.ttf");
Typeface tf4 = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
lblNameTak.setTypeface(tf3);
txtNameTak.setTypeface(tf2);
lblRate.setTypeface(tf2);
lblQty.setTypeface(tf2);
lblTotal.setTypeface(tf2);
lblNameTak.setText("Item " + (qty.index + 1));
DbAdapterItem dbItem;
TextAdapterItem txtItem;
dbItem = new DbAdapterItem(this, SqlDb);
txtItem = new TextAdapterItem(dbItem, this, qty);
txtNameTak.setAdapter(txtItem);
txtNameTak.setOnItemClickListener(txtItem);
txtNameTak.requestFocus();
}
all these items created by addNewItem() are vanished...
here is the layout from which i'm inflating...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom ="22dp"
tools:context=".OrderItems" >
<FrameLayout
android:layout_width="match_parent"
android:background= "#888888"
android:layout_height="98dp" >
<TextView
android:id="#+id/tak_itemNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginTop="2.7dp"
android:text="Item 1"
android:layout_gravity="right"
android:textColor="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="22dp"
android:orientation="vertical"
android:background= "#FFFFFF"
android:layout_height="75dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<AutoCompleteTextView
android:id="#+id/tak_itemName"
android:imeOptions="actionUnspecified"
android:imeActionLabel="search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
android:layout_weight="1.06"
android:completionThreshold="1"
android:ems="10"
android:hint="Item Name / Barcode"
android:inputType="textCapWords"
android:singleLine="true"
android:textColorHint="#DDDDDD" />
<ImageView
android:id="#+id/barcode_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="6dp"
android:src="#android:drawable/ic_menu_camera" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
>
<TextView
android:id="#+id/tak_lblRate"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Rate" />
<TextView
android:id="#+id/tak_Rate"
android:layout_width="match_parent"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<TextView
android:id="#+id/tak_lblQty"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginRight="7dp"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Qty" />
<com.sayka.ordergadget.EditText2
android:id="#+id/tak_Qty"
android:layout_width="match_parent"
android:layout_marginBottom="-4dp"
android:layout_marginTop="4dp"
android:inputType="numberDecimal"
android:imeOptions="actionNext"
android:layout_marginLeft="12dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp"
android:text="0.0" />
</FrameLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
>
<TextView
android:id="#+id/tak_lblTotal"
android:layout_width="wrap_content"
android:layout_marginLeft="7dp"
android:layout_gravity="left"
android:layout_height="wrap_content"
android:textColor="#555555"
android:textSize="10sp"
android:text="Total" />
<TextView
android:id="#+id/tak_Total"
android:layout_width="match_parent"
android:layout_gravity="right"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="16sp"
android:text="0.00" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
It's normal behaviour and you are correct. the activity is recreated from scratch on rotation.
If you want something to persist. store it during onSaveInstanceState in the provided bundle.
then during onCreate check to see if the bundle is null, if it isn't, pull your data out and add the view elements again.
Alternatively you can fudge it and state in the manifest that your activity will handle orientation/configuration changes.

How to select a textview having an id from a custom listview in android?

I have a custom listview having the following layout:
I have defined an onClick method for 'qadd' button and 'qsub' button in xml using android:onClick property.
In my Activity class how do I select the 'quantity' textView for current position?
I tried this but it only updates 'quantity' textView at first position.
public void addClicked(View v) {
final int position = list.getPositionForView((View) v.getParent().getParent());
long rec_id = adapter.getItemId(position);
JSONObject o = (JSONObject) fullList.get(rec_id + "");
TextView q = (TextView)findViewById(R.id.quantity);
int quantity = Integer.parseInt((String) q.getText());
quantity++;
q.setText(quantity + "");
try {
String price = o.getString("price");
subtotal = subtotal + (Integer.parseInt(price) * quantity);
total_text = (TextView) findViewById(R.id.subtotal);
total_text.setText(subtotal + "");
// Log.d("position", price);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code goes wrong at this line:
TextView q = (TextView)findViewById(R.id.quantity);
It always selects the textView at first position.
Please ask if any other piece of code is needed.
Entire custom_list_layout.xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/checkBox1" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/item_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:orientation="vertical" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item name"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/item_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item description"
android:textColor="#343434"
android:textSize="10dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/rupee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Rs. "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="0 "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom|right" >
<Button
android:id="#+id/qadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="addClicked"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/qsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="subClicked"
android:text="-" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Link to my entire project.
Your textview to address should be the one within the list item row you specify in XML for the list adapter.
If you're using a custom adapter, within getView you should wire up the textview as follows:
// create a new TextView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
View listView = convertView;
if (listView == null) { // if it's not recycled, initialize some attributes
// get layout from xml
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.browse_product_item, null);
}
final ListProductHolder holder;
if (listView.getTag() != null) {
holder = (listProductHolder) listView.getTag();
} else {
holder = new listProductHolder();
listView.setTag(holder);
holder.text= (TextView) listView.findViewById(R.id.quantity);
One you have the proper TextView from within each item in the listView, then it's a simple process to wire up an OnClickListener for that view and respond to those click events.

Force close error in Android app debugging

I get no compilation errors or even significant warnings, yet when I run the program, it force closes and the log tells me there's some kind of error on a line. It succeeded once, but I don't know how or when.
onClick method:
public TextView equation;
public TextView answer11;
public TextView answer12;
public TextView or1;
public TextView or2;
public void SolveOnClick(View view){
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
double A = Double.parseDouble(a.getText().toString());
double B = Double.parseDouble(b.getText().toString());
double C = Double.parseDouble(c.getText().toString());
Formula(A,B,C);
}
My main graphical layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="SolveOnClick"
android:text="#string/solveButtonText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/quadFormText"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/A"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/aValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/textView1"
android:layout_marginLeft="15dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/bValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aValue"
android:layout_below="#+id/aValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/cValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bValue"
android:layout_below="#+id/bValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/answer1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/equals1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="88dp"
android:text="#string/equals2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer1Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer11"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="25dp"
android:text="#string/or1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer2Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer12"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer2Text"
android:layout_below="#+id/answer2Text"
android:layout_marginTop="24dp"
android:text="#string/or2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/equationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/cValue"
android:layout_marginTop="14dp"
android:text="#string/equation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cValue"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/C"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/bValue"
android:layout_alignLeft="#+id/textView3"
android:text="#string/B"
android:textAppearance="?android:attr/textAppearanceLarge" />
onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
One of the logs when the program fails:
04-26 20:39:33.174: E/AndroidRuntime(16092): Caused by: java.lang.ClassCastException: android.widget.TextView
04-26 20:39:33.174: E/AndroidRuntime(16092): at calc.ulater.QuadCalcActivity.SolveOnClick(QuadCalcActivity.java:31)
I believe this has something to do with the decimal text boxes. The program worked well before I made them only take decimal numbers, but it also worked a time or two after. I'd rather not show the rest of my code, unless you guys are sure the problem lies there.
If this is the line that's screwing up:
EditText b = (EditText)findViewById(R.id.bValue);
Then its saying you're trying to cast a TextView to an EditText.
What's line 31? That one?
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
all the initialization must be done in onCreate() and not in the constructor
Updated::
EditText a = (EditText)view.findViewById(R.id.aValue);
EditText b = (EditText)view.findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)view.findViewById(R.id.cValue);
equation = (TextView)view.findViewById(R.id.equationText);
answer11 = (TextView)view.findViewById(R.id.answer1);
answer12 = (TextView)view.findViewById(R.id.answer2);
or1 = (TextView)view.findViewById(R.id.or1);
or2 = (TextView)view.findViewById(R.id.or2);
if view is your parent view

Categories