Put two values in two separate fields Android - android

I need to be able to choose or pick an item from a list and after I choose it, the name of that item will go into one field and the corresponding weight to that item will go into another field.
I've been really stuck on this. I tired making a adapter but it wasn't working out and I'm just so lost, I don't know where to go with it.
I would really appreciate help.
If you need any code that I've done please say and I'll put it up straight away.
I don't have a database, I have a class of an array or numbers and Letter.
Thanks all!
Main.java
public void Chem() {
Chemical chemical_data[] = new Chemical[]
{
new Chemical("Acid", 125.33),
new Chemical("Blue", 145.3356),
};
ChemicalAdapter adapter = new ChemicalAdapter(this, R.layout.sollayout, chemical_data);
chemname = (AutoCompleteTextView) findViewById(R.id.editText4);
weightval = (EditText) findViewById(R.id.editText);
//ChemicalAdapter header = new ChemicalAdapter(this, R.layout.sollayout, null);
//weightval.addHeaderView(header);
chemname.setAdapter(adapter);
}
public void Chemname()
{
chemname = (AutoCompleteTextView) findViewById(R.id.editText4);
AutoCompleteTextView b = (AutoCompleteTextView) findViewById(R.id.editText4);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Chem();
}
});
}
Chemical.java
public class Chemical {
String chemicalValue;
double weight;
public Chemical(String chemicalValue, double weight) {
this.chemicalValue = chemicalValue;
this.weight = weight;
}
public String getChemicalValue() {
return chemicalValue;
}
public double getWeight() {
return weight;
}
}
ChemicalAdpater.java
public class ChemicalAdapter extends ArrayAdapter<Chemical> {
Context context;
int layoutResourceId;
Chemical data[] = null;
public ChemicalAdapter(Context context, int layoutResourceId, Chemical[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ChemicalHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ChemicalHolder();
holder.chemname = (AutoCompleteTextView)row.findViewById(R.id.editText4);
holder.weightval = (EditText)row.findViewById(R.id.editText);
row.setTag(holder);
}
else
{
holder = (ChemicalHolder)row.getTag();
}
Chemical chemical = data[position];
holder.weightval.setText(String.valueOf(chemical.weight));
holder.chemname.setText(chemical.chemicalValue);
return row;
}
static class ChemicalHolder
{
AutoCompleteTextView chemname;
EditText weightval;
}
}

Related

ArrayList<...> is duplicated after update

Please tell me what I'm doing wrong? I adapter receives data from the server and places them in the foliage. Then, when new data arrive, I try to update the adapter ... and as if all is well, but when it comes .. the new data, eg 2 messages, they are added to the adapter. But with the new data are added to all the other old dannye..Hotya logs on can see that the old data received I do not have a server. It turns out that it is not doing data.clear (). I carry ... data.clear (), my foliage becomes completely empty until the next request Handler.postDelayed () ;. Again request - full foliage and the new data ... 5 seconds passed and my clean sheet again ... and so constantly. How to overcome it?
My Adapter:
public class ChatMsgAdapter extends ArrayAdapter<ResponseMsgArray> {
private Context context;
private ArrayList<ResponseMsgArray> data;
protected String LV_KEY = Auth.key;
protected int LV_USID = Integer.parseInt(Auth.id);
protected int GET_ID = Integer.parseInt(FriendActivity.get_id);
String LOG_TAG = "FriendLOG";
public ChatMsgAdapter(Context context, ArrayList<ResponseMsgArray> values) {
super(context,R.layout.activity_friend_msg, values);
this.data = values;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v;
int type = getItemViewType(position);
if(type == LV_USID) {
v = inflater.inflate(R.layout.activity_friend_msg_adapter, null);
TextView user_id_msg = (TextView) v.findViewById(R.id.id_msg);
TextView user_text = (TextView) v.findViewById(R.id.msg);
TextView user_date = (TextView) v.findViewById(R.id.msg_time);
user_text.setText(data.get(position).getMsg());
user_date.setText(data.get(position).getMsg_time());
user_id_msg.setText(data.get(position).getMsg_id());
} else if (type == GET_ID) {
v = inflater.inflate(R.layout.talker, null);
TextView talker_text = (TextView) v.findViewById(R.id.msg);
TextView talker_date = (TextView) v.findViewById(R.id.msg_time);
talker_text.setText(data.get(position).getMsg());
talker_date.setText(data.get(position).getMsg_time());
} else {
//Если нет например сообщений
v = inflater.inflate(R.layout.msg_null, null);
}
return v;
}
public void setData(ArrayList<ResponseMsgArray> newData) {
addAll(newData);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
int newPosition = Integer.parseInt(data.get(position).getMsg_id_us());
return newPosition;
}
}
Text translated with the help of an interpreter. Thank you for understanding.
Here in this post https://ru.stackoverflow.com/questions/579311/%d0%9f%d0%b5%d1%80%d0%b5%d0%b4%d0%b0%d1%87%d0%b0-%d0%b4%d0%b0%d0%bd%d0%bd%d1%8b%d1%85-%d0%b2-%d0%b4%d1%80%d1%83%d0%b3%d0%be%d0%b9-%d0%ba%d0%bb%d0%b0%d1%81%d1%81/579403?noredirect=1#comment769066_579403 are all my current classes

Android AdapterView cannot display database records in some device

I would like to ask some question about AdapterView.
In my application, there is an activity which retrieve data from database and display them in AdapterView.
However, when i install the application in different devices, I found that the part I have just mentioned could only function on some devices. The others cannot show the database results.
Here is my code:
private void showResults(String query) {
Cursor cursor = searchCustByInputText(query);
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[] {
"cust_code",
"chinese_name"};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] {
R.id.scust_code,
R.id.schinese_name};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter customers = new SimpleCursorAdapter(this,R.layout.cust_list_item, cursor, from, to);
mListView.setAdapter(customers);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) mListView.getItemAtPosition(position);
String cust_code = c.getString(c.getColumnIndex("cust_code"));
if (callFromAct.equals("Main")) {
String pay_term = c.getString(c.getColumnIndex("pay_term"));
String chinese_name = c.getString(c.getColumnIndex("chinese_name"));
String english_name = c.getString(c.getColumnIndex("english_name"));
String address_1 = c.getString(c.getColumnIndex("address_1"));
String address_2 = c.getString(c.getColumnIndex("address_2"));
String address_3 = c.getString(c.getColumnIndex("address_3"));
String address_4 = c.getString(c.getColumnIndex("address_4"));
String contact = c.getString(c.getColumnIndex("contact"));
String telephone = c.getString(c.getColumnIndex("telephone"));
String last_order_date = c.getString(c.getColumnIndex("last_order_date"));
//Pass data to another Activity
Intent it = new Intent(CustEnqActivity.this, CustEnqDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("cust_code", cust_code);
bundle.putString("pay_term", pay_term);
bundle.putString("chinese_name", chinese_name);
bundle.putString("english_name", english_name);
bundle.putString("address_1", address_1);
bundle.putString("address_2", address_2);
bundle.putString("address_3", address_3);
bundle.putString("address_4", address_4);
bundle.putString("contact", contact);
bundle.putString("telephone", telephone);
bundle.putString("last_order_date", last_order_date);
it.putExtras(bundle);
startActivity(it);
}
else {
returnToCallingAct(cust_code);
}
//searchView.setQuery("",true);
}
});
}
}
Besides, I discovered there were two warnings in my logcat.
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated
AdapterView is a raw type. References to generic type AdapterView should be parameterized
Are they related to the problem?
Try to create a class that extends BaseAdapter and use ViewHolders for performance
eg:
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());
return convertView;
}
// or you can try better way
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText(text);
return tv;
}
private ImageView detail(View v, int resId, int icon) {
ImageView iv = (ImageView) v.findViewById(resId);
iv.setImageResource(icon); //
return iv;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
}
}
More info/example:
http://www.pcsalt.com/android/listview-using-baseadapter-android/#sthash.lNGSCiyB.dpbs

How to set onClick for custom array adapter with extra data

I have a custom array adapter for a ListView. The goal is to have each list item have an individual picture, and that when the user clicks on the item they are taken to a web page. I have the image part working, and there are some stackoverflow answers for where to put the onItemClickListener. It seems it would go in the custom array adapter class But I can't figure out how to access the url from the list item.
Here is the code:
public class Animal
{
int image_resource_id;
String animal_type;
String wikipedia_url;
Animal(int image_resource_id, String animal_type, String wikipedia_url)
{
this.image_resource_id = image_resource_id;
this.animal_type = animal_type;
this.wikipedia_url = wikipedia_url;
}
}
class AnimalViewHolder
{
ImageView animal_image = null;
TextView animal_name = null;
String animal_url = "";
}
class CustomAnimalAdapter extends ArrayAdapter<Animal>
{
Context context;
int layoutResourceId;
Animal data[] = null;
public CustomAnimalAdapter(Context context, int layoutResourceId, Animal[] data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
AnimalViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new AnimalViewHolder();
holder.animal_image = (ImageView)row.findViewById(R.id.animal_image);
holder.animal_name = (TextView)row.findViewById(R.id.animal_name);
row.setTag(holder);
}
else
{
holder = (AnimalViewHolder)row.getTag();
}
Animal animal = data[position];
holder.animal_name.setText(animal.animal_type);
holder.animal_image.setImageResource(animal.image_resource_id);
return row;
}
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Animal[] animal_list = {
new Animal(R.drawable.lion, "Lion", "http://en.wikipedia.org/wiki/Lion"),
new Animal(R.drawable.tiger, "Tiger", "http://en.wikipedia.org/wiki/Tiger"),
new Animal(R.drawable.bear, "Bear", "http://en.wikipedia.org/wiki/Bear"),
new Animal(R.drawable.monkey, "Monkey", "http://en.wikipedia.org/wiki/Monkey"),
new Animal(R.drawable.moose, "Moose", "http://en.wikipedia.org/wiki/Moose"),
new Animal(R.drawable.shark, "Shark", "http://en.wikipedia.org/wiki/Shark")
};
CustomAnimalAdapter adapter = new CustomAnimalAdapter(this,
R.layout.row_item, animal_list);
list_view_1 = (ListView)findViewById(R.id.listView1);
list_view_1.setAdapter(adapter);
}
update your adapter getview with
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(animal.wikipedia_url));
context.startActivity(browserIntent);
}
});

ListView showing only one item or last item

The title of this question is same but technical issue are different.
Hi i am trying to get data from SQLite but i am able to show only last item in listview. I tried different- different solution but not getting success.
Problem is not getting item from SQLite(I am able to fetch all item) but showing item using adapter in listview.
Here is my code.
ListActivity.java
db=new DBHelper(getBaseContext());
db.getWritableDatabase();
try {
final DBHelper m = new DBHelper(getBaseContext());
final List<GetSet> NotesWiseProfile = m.getBabyDetails();
for (final GetSet cn : NotesWiseProfile) {
counter++;
String babyName = cn.getBabyName();
String babyImage = cn.getBabyImage();
int babyId = cn.getBabyId();
BabyData baby_data[] = new BabyData[]
{
new BabyData(R.drawable.ic_launcher, babyName,babyId),
};
adapter = new MobileArrayAdapter(this,
R.layout.list_row, baby_data);
listView1.invalidateViews();
listView1.setAdapter(adapter);
}
}
catch (Exception e) {
}
BabyData.java
public class BabyData {
public int icon;
public String title;
public int babyid;
public BabyData(){
super();
}
public BabyData(int icon, String title,int babyId) {
super();
this.icon = icon;
this.title = title;
babyid = babyId;
}
}
MobileArrayAdapter.java
public class MobileArrayAdapter extends ArrayAdapter<BabyData>{
Context context;
int layoutResourceId;
BabyData data[] = null;
public MobileArrayAdapter(Context context, int layoutResourceId, BabyData[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DataHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DataHolder ();
holder.imgIcon = (ImageView)row.findViewById(R.id.imvBabyFace);
holder.txtTitle = (TextView)row.findViewById(R.id.tvbabyNameList);
holder.txtBabyId = (TextView)row.findViewById(R.id.tvBabyId);
row.setTag(holder);
}
else
{
holder = (DataHolder )row.getTag();
}
BabyData weather = data[position];
holder.txtTitle.setText(weather.title);
holder.txtBabyId.setText(String.valueOf(weather.babyid));
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class DataHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtBabyId;
}
}
I don't understand what's wrong in my code. Please give me any hint or reference.
Thanks in Advance.
Put the listview declarations out of the for loop, something like:
BabyData baby_data[] = new BabyData[NotesWiseProfile.size()];
for (final GetSet cn : NotesWiseProfile) {
String babyName = cn.getBabyName();
String babyImage = cn.getBabyImage();
int babyId = cn.getBabyId();
baby_data[counter] = new BabyData(R.drawable.ic_launcher, babyName,babyId);
counter++;
}
adapter = new MobileArrayAdapter(this,
R.layout.list_row, baby_data);
listView1.invalidateViews();
listView1.setAdapter(adapter);
I think you should use a field for storing you babies. Currrently, you are using a local Baby array for that. As far as I know, the ListView always gets its data from the array you passed to it (invalidating it causes the ListView to look up that data again.
To recap: Store your array as a field - if data changes, update the array and call notifyDatasetChanged() on your adapter, which will cause your ListView to reload the data.

Finding the Checked state of checkbox in a custom listview

Hai i'm trying to develop an app where by i can send sms and email to a particular group of people..
I have a listview showing the contacts which are in my group.Each row is of the form
TextView(Name)TextView(phone) Checkbox(sms)
TextView(email id) Checkbox(mail)
I have used custom adapter to display the contact details to the listview.ihave set the onitemclick listener to find the position of the row..
I have to send sms and email to those contacts for which checkboxes have been set as true.how can i find the state of each of the checkboxes.
Please help me..lotz of thanx in advance..
I hav added below the custom adapetr i hv created..
public class ContactInfoAdapter extends ArrayAdapter{
private ArrayList<Boolean> mChecked_sms,mChecked_email;
Context context;
int layoutResourceId;
ContactInfo data[] = null;
public ContactInfoAdapter(Context context, int layoutResourceId, ContactInfo[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
mChecked_sms = new ArrayList<Boolean>();
mChecked_email = new ArrayList<Boolean>();
for (int i = 0; i < this.getCount(); i++) {
mChecked_sms.add(i, false);
mChecked_email.add(i,false);
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ContactHolder holder;
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactHolder();
holder.txtName = (TextView)row.findViewById(R.id.textViewName);
holder.txtPhone = (TextView) row.findViewById(R.id.textViewPhone);
holder.txtEmail = (TextView) row.findViewById(R.id.textViewEmail);
holder.cb_sms_state = (CheckBox) row.findViewById(R.id.checkBox1);
holder.cb_email_state = (CheckBox) row.findViewById(R.id.checkBox2);
row.setTag(holder);
}
else
{
holder = (ContactHolder)row.getTag();
}
ContactInfo contact = data[position];
holder.txtName.setText(contact.name);
holder.cb_sms_state.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.cb_sms_state.isChecked()) {
mChecked_sms.set(position, true);
Toast.makeText(getContext(), "checked", 2).show();
} else {
mChecked_sms.set(position, false);
}
}
});
holder.cb_sms_state.setChecked(mChecked_sms.get(position));
holder.cb_email_state.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (holder.cb_email_state.isChecked()) {
mChecked_email.set(position, true);
Toast.makeText(getContext(), "checked", 2).show();
} else {
mChecked_email.set(position, false);
}
}
});
holder.cb_email_state.setChecked(mChecked_email.get(position));
holder.txtPhone.setText(contact.number);
holder.txtEmail.setText(contact.email);
return row;
}
static class ContactHolder
{
TextView txtName;
TextView txtPhone;
TextView txtEmail;
CheckBox cb_sms_state;
CheckBox cb_email_state;
}
}
The ContactInfo class is :
public class ContactInfo {
public String name;
public String number;
public String email;
public boolean sms_state;
public boolean email_state;
public ContactInfo(){
super();
}
public ContactInfo(String name,String number,String email,boolean sms_state,boolean email_state) {
super();
this.name = name;
this.number = number;
this.email = email;
this.sms_state = sms_state;
this.email_state = email_state;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setNUmber(String number) {
this.number = number;
}
public String getNumber() {
return number;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
public void setSms_state(Boolean sms_state)
{
this.sms_state = sms_state;
}
public Boolean getSms_state(){
return sms_state;
}
public void setEmail_state(Boolean email_state)
{
this.email_state = email_state;
}
public Boolean getEmail_state(){
return email_state;
}
Inside the getView() method, you have to implement a OnCheckedChangeListener for the CheckBox.
Here is a listener code, say for example:
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});
See the doc.
I think you need :
checkbox.isChecked()
Here's a simple sample:
mContactListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
CheckBox chkContact = (CheckBox) view.findViewById(R.id.listrow_contact_chkContact);
if (chkContact.isChecked()) {
...
}
});
I've had this problem with my app Hasta La Vista I've create a with custom listview with checked items and I needed to get checked items this was the solution:
ListView lv = getListView(); //ver listview
if(lv != null){
final SparseBooleanArray checkedItems = lv.getCheckedItemPositions(); //get checked items
if (checkedItems == null) {
return;
}
final int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
// This tells us the item position we are looking at
final int position = checkedItems.keyAt(i);
// This tells us the item status at the above position
final boolean isChecked = checkedItems.valueAt(i);
if(isChecked){
//get item from list and do something
lv.getAdapter().getItem(position);
}
}
}
As ListView views are recycled, you can not rely on listening on particular instance, you will have to store "checked" state in your data model. You will also need custom list adapter, where you create and populate individual entries. Following shall be done:
- in overriden getView(), either create new view ( in case no convertView was supplied ) or inflate new one
- populate viewv fields from you data model
- remove old onclick listener, and set new one ( can be anonymous inner class ) modifying your data model
PS: recycling views is important if your list is big

Categories

Resources