I am inflating the passenger_details_layout in main_passenger_details_layout.At run time i am setting the values in TextView . I am setting up pax_id that is invisible for every row that i am inflating in it.So on click of passenger_details_layout i want to call a new activity and pass the pax_id to the new activity.
Pax_id will be like 1,2,3 for every time for loop will run.So on click of 1 row i want to get the pax_id of first row that i have set.
Please help how could i do this Will be thank full to you.
Code to Infalate
ItinResponse data = obj.parseXMLData(fXmlFile);
List<SectorRecords> sectorList = data.getSectorRecordsDetails();
int items = 0;
List<PassengerDetails> listdata = data.getPaxDetails();
int i = 0;
for (PassengerDetails elements : listdata) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.passenger_details_layout, main, false);
firstName = (TextView) layout.findViewById(R.id.first_pax_name);
pax_id = (TextView) layout.findViewById(R.id.pax_id);
firstName.setText(elements.getFirstName() + " "
+ elements.getLastName());
pax_id.setText(elements.getPaxId());
main.addView(layout, i);
i++;
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+pax_id.getText(), Toast.LENGTH_LONG).show();
}
});
On every row is showing the last pax id
You have to do nothing what you are trying
Replace this
pax_id.setText(elements.getPaxId());
to
layout.setId(Integer.parseInt(elements.getPaxId()));
And in your Layout Click
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+layout.getId(), Toast.LENGTH_LONG).show();
}
});
Every time you will get the Unique ID that u want to set
All the best...!!!!
Use
TextView paxView = (TextView)v.findViewById(R.id.pax_id);
String pax_value = paxView.getText().toString();
On button click of every row do like this
Intent intent=new Intent(this,YOUR_NEXT_ACTIVIY.class);
intent.putExtra("paxid", pax_id.getText());
startActivity(intent);
ItinResponse data = obj.parseXMLData(fXmlFile);
List<SectorRecords> sectorList = data.getSectorRecordsDetails();
int items = 0;
List<PassengerDetails> listdata = data.getPaxDetails();
int i = 0;
for (PassengerDetails elements : listdata) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.passenger_details_layout, main, false);
firstName = (TextView) layout.findViewById(R.id.first_pax_name);
pax_id = (TextView) layout.findViewById(R.id.pax_id);
firstName.setText(elements.getFirstName() + " "
+ elements.getLastName());
pax_id.setText(elements.getPaxId());
layout.setTag(elements.getPaxId());
main.addView(layout, i);
i++;
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String paxId = v.getTag().toString();
// do some staff
}
});
}
// try this
ItinResponse data = obj.parseXMLData(fXmlFile);
List<SectorRecords> sectorList = data.getSectorRecordsDetails();
int items = 0;
List<PassengerDetails> listdata = data.getPaxDetails();
int i = 0;
for (PassengerDetails elements : listdata) {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(
R.layout.passenger_details_layout, main, false);
firstName = (TextView) layout.findViewById(R.id.first_pax_name);
pax_id = (TextView) layout.findViewById(R.id.pax_id);
firstName.setText(elements.getFirstName() + " "
+ elements.getLastName());
pax_id.setText(elements.getPaxId());
layout.setTag(elements);
main.addView(layout, i);
i++;
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PassengerDetails elements = (PassengerDetails) v.getTag();
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)"+elements.getPaxId(), Toast.LENGTH_LONG).show();
}
});
}
Related
I am working on a checkbox listview. What I have done so far is populated the list from local db, get all the checked item's databse id using the setOnCheckedChangeListener and store it in a separate table in my local db. This is how my code looks:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
dbHelper = new DBHelper(con);
database = dbHelper.getWritableDatabase();
switch (getItemViewType(position)){
case DYNAMIC_OBJECTIVE:
convertView = layoutInflater.inflate(R.layout.objective_list_view, null);
break;
case STATIC_OBJECTIVE:
convertView = layoutInflater.inflate(R.layout.other_objective_list_view, null);
break;
}
switch (getItemViewType(position)){
case DYNAMIC_OBJECTIVE:
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.objectiveCheckBox);
checkBox.setText(((Objectives)list.get(position)).getObjectiveTitle());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "YES");
}else{
selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "NO");
}
}
});
break;
case STATIC_OBJECTIVE:
TextView checkBoxOther = (TextView) convertView.findViewById(R.id.objectiveOtherCheckBox);
final TextView objectiveOtherET = (TextView) convertView.findViewById(R.id.objectiveOtherEditText);
checkBoxOther.setText((String)list.get(position));
//ll = (LinearLayout)convertView.findViewById(R.id.linearLayoutOther);
//viewNew = layoutInflater.inflate(R.layout.other_objective_row, null);
ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.addOtherObjectiveTextBox);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext(), "Clicked123", Toast.LENGTH_SHORT).show();
final Dialog dialog = new Dialog(v.getContext());
dialog.setContentView(R.layout.otherobjectivedialog);
final EditText enterObjET = (EditText) dialog.findViewById(R.id.enterObjEditText);
Button closeEnterObjBtn = (Button) dialog.findViewById(R.id.closeEnterObjDialog);
Button objConfirmBtn = (Button) dialog.findViewById(R.id.enterObjConfirmBtn);
closeEnterObjBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
objConfirmBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String enteredObj = enterObjET.getText().toString();
if(enteredObj.equals("")){
Toast.makeText(v.getContext(), "Please write a objective", Toast.LENGTH_LONG).show();
}else {
objectiveOtherET.setText(objectiveOtherET.getText().toString()+ " \n" + enteredObj);
otherObjList.add(enteredObj);
dialog.dismiss();
}
}
});
dialog.show();
/* LinearLayout linearLayoutNew = new LinearLayout(con);
linearLayoutNew.setOrientation(LinearLayout.VERTICAL);
et = new EditText(con);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setHint("Set some objective");
linearLayoutNew.addView(et);
ll.addView(linearLayoutNew);*/
}
});
break;
}
}
return convertView;
}
and this is how I'm populating my list:
ArrayList<Object> objectiveList = new ArrayList<>();
Cursor crs = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_OBJECTIVE +"", null);
while(crs.moveToNext())
{
String title = crs.getString(crs.getColumnIndex("title"));
int objectiveID = crs.getInt(crs.getColumnIndex("id"));
objectiveList.add(new Objectives(objectiveID, title));
}
crs.close();
objectiveList.add("Others");
listView.setAdapter(new ObjectivesAdapter(getActivity(), objectiveList));
viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
PROBLEM:
This all works perfect but now I want to edit the same information. All I want is the existing checkbox id's (that were checked and store in my db) should be checked when I come back to this view. I have queried the data and I have the IDs but I don't know how to check the checkboxes based on their IDs.
Cursor crsCheckObj = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PLAN_OBJECTIVE_MAP +" WHERE "
+ ItemsTable.COLUMN_PLAN_OBJECTIVE_MAP_PLANID +"='"+ Info.getInstance().getPlanVisitID() +"'", null);
if(crsCheckObj.getCount() > 0){
while (crsCheckObj.moveToNext()){
//Here IDs should match in order to check the checkboxes
}
}
crsCheckObj.close();
For that you can simply use checkbox.setChecked(true)
if it do not work then try following:
checkbox.postDelayed(new Runnable() {
#Override
public void run() {
checkbox.setChecked(true);
}
},100);
First setTag() for your checkbox like this:
checkBox.setTag(((Objectives)list.get(position)).getObjectiveID());
then all you need to is something like this:
Cursor crsCheckObj = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PLAN_OBJECTIVE_MAP +" WHERE "
+ ItemsTable.COLUMN_PLAN_OBJECTIVE_MAP_PLANID +"='"+ Info.getInstance().getPlanVisitID() +"'", null);
if(crsCheckObj.getCount() > 0){
while (crsCheckObj.moveToNext()){
if(((Integer) checkBox.getTag()) == crsCheckObj.getInt(crsCheckObj.getColumnIndex("objective_id"))){
checkBox.setChecked(true);
}
}
}
crsCheckObj.close();
I have an ArrayAdapter which shows some posts using a fragment. In this fragment I have some buttons. So all of the posts shown have these buttons.
The problem is, when I click a button of a post to setText to a field of this post; instead of changing text of post that I clicked, it changes text of newly scrolled item.
For example if I scroll down the ListView a new item comes. Then regardless of which post I clicked it changes text of this new post.
How can I prevent this to happen? I take current post using
getItem(position)
By scroolling I mean new element of the list are loaded and shown.
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.comment_small,parent,false);
TextView tvOwner = (TextView) v.findViewById(R.id.tvCommentSmallOwner);
TextView tvCreationDate = (TextView) v.findViewById(R.id.tvCommentSmallCreationDate);
TextView tvCreationDateValue = (TextView) v.findViewById(R.id.tvCommentSmallCreationDate);
TextView tvContent = (TextView) v.findViewById(R.id.tvCommentSmallContent);
tvVoteCount = (TextView) v.findViewById(R.id.tvCommentVoteCount);
c = getItem(position);
tvOwner.setText(context.getResources().getString(R.string.generic_by) + " " + c.getOwnerId());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String s =c.getPostDate();
System.out.println(s);
tvCreationDateValue.setText(s);
tvContent.setText(c.getContent());
btnDownVote = (ImageButton) v.findViewById(R.id.btnCommentDownVote);
btnUpVote = (ImageButton) v.findViewById(R.id.btnCommentUpVote);
tvVoteCount.setText("" + c.getNetCount());
btnUpVote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
c=getItem(position);
ServerRequests sr = new ServerRequests(getContext());
MemberLocalStore memberLocalStore = new MemberLocalStore(getContext());
Member m = memberLocalStore.getLoggedInMember();
sr.voteComment(c, true, m.getId(), new Consumer<String>() {
#Override
public void accept(String vote) {
tvVoteCount.setText("" + vote);
}
});
}
});
btnDownVote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
c=getItem(position);
ServerRequests sr = new ServerRequests(getContext());
MemberLocalStore memberLocalStore = new MemberLocalStore(getContext());
Member m = memberLocalStore.getLoggedInMember();
sr.voteComment(c, false, m.getId(), new Consumer<String>() {
#Override
public void accept(String vote) {
tvVoteCount.setText("" + vote);
}
});
}
});
return v;
}
I want to vote (like,dislike) some posts but when I click upvote button that marked as 1, overall vote of the post that marked as 2 changes.
I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.
I have tried parent.removeView(myView)
My Code for Layout adding :
LayoutInflater inflater = getLayoutInflater();
final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);
final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);
Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);
ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);
ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);
final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );
note_Title.setText(note_title);
linear_layout.addView(rel_layout_bottom, 0);
JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");
for (int i = 0; i < jsonArray.length(); i++) {
final View view_middle = inflater.inflate(R.layout.layout_middle, linear_layout, false);
final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);
final CheckBox note_check ;
final TextView note_content ;
note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
btn_remove.setOnClickListener(null);
try {
//Getting data correctly here
linear_layout.addView(view_middle,0);
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
linear_layout.addView(rel_layout_top , 0);
}
Any answer Appreciated...Thks
Something like below will solve your problem.
final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
view_middleArr = new View[jsonArray.length()];
view_middleArr[i] = inflater.inflate(R.layout.layout_middle, linear_layout, false);
// ...
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (View view_middle : view_middleArr) {
linear_layout.removeView(view_middle);
}
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
}
Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.
The code
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
is in a for loop.
It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.
Hello I am inflate xml file like..
List<EditText> allEds3 = new ArrayList<EditText>();
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int hh1 = 57; hh1 <= 76; hh1++) {
try {
View view = inflater.inflate(R.layout.form6, null);
form6_linear.addView(view);
lbl1 = (TextView) view.findViewById(R.id.lbl1);
lbl2 = (TextView) view.findViewById(R.id.lbl2);
txt1 = (TextView) view.findViewById(R.id.txt1);
txt1.setId(hh1);
txt1.setText(txt_array.get(hh1));
txt1.setOnClickListener(onclicklistener);
_txt2 = (EditText) view.findViewById(R.id.txt2);
lbl1.setText(lbl_array.get(hh1));
lbl2.setText(lbl_array.get(hh1 + 1));
_txt2.setText(txt_array.get(hh1 + 1));
allEds3.add(_txt2);
hh1++;
nn1++;
} catch (Exception e) {
// TODO: handle exception
}
}
Now I have one button in my main xml file and onclick of that button I have to get all values of this _txt2 of above.
Please help.
EDIT:
List<EditText> allEds3 = new ArrayList<EditText>();
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int hh1 = 57; hh1 <= 76; hh1++) {
try {
View view = inflater.inflate(R.layout.form6, null);
form6_linear.addView(view);
lbl1 = (TextView) view.findViewById(R.id.lbl1);
lbl2 = (TextView) view.findViewById(R.id.lbl2);
txt1 = (TextView) view.findViewById(R.id.txt1);
txt1.setId(hh1);
txt1.setText(txt_array.get(hh1));
txt1.setOnClickListener(onclicklistener);
_txt2 = (EditText) view.findViewById(R.id.txt2);
lbl1.setText(lbl_array.get(hh1));
lbl2.setText(lbl_array.get(hh1 + 1));
_txt2.setText(txt_array.get(hh1 + 1));
allEds3.add(_txt2);
hh1++;
nn1++;
} catch (Exception e) {
// TODO: handle exception
}
}
OnClick()
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_continue1:
final String[] strings2 = new String[allEds3.size()];
for (int i = 0; i < allEds3.size(); i++) {
strings2[i] = allEds3.get(i).getText().toString();
}
int ii = 0;
for (int db = 57; db <= 76; db++) {
try {
j.remove(txt_array.get(db));
j.put(txt_array.get(db), strings2[ii]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii++;
}
}
}
Error :-
ArrayIndexOutOfBounds at
j.put(txt_array.get(db), strings2[ii]);
If _txt2 is defined as a field in your activity class, then you can simply get it on the button's onClick:
// in onCreate block of your activity
Button button = (Button) findViewById(R.id.button_to_click);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// here you can do anything with _txt2 which is just executed when button is clicked
}
}
EDIT: I didn't figure out your question properly, so I should add this:
You can set an ID to any EditText you create. then find them by findViewById() when needed. or you can store all EditText(s) in an array of EditText and get them later.
I have a custom ListView with two button and I when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it. So far nothing has worked I keep getting the last item in my array.
Here is a screen shot to give you a better idea of what i mean
Here is my Adapter subclass for my custom ListView
static final String[] Names =
new String[] { "John", "Mike", "Maria", "Miguel"};
class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
int which;
public MyArrayAdapter(Context context, String[] pValues) {
super(context, R.layout.main, pValues);
this.context = context;
values = pValues;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
Button call = (Button) rowView.findViewById(R.id.button1);
Button chat = (Button) rowView.findViewById(R.id.button2);
textView.setText(values[position]);
// Change icon based on name
s = values[position];
which = position;
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = values[which];
Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
Edit:
String name = textView.getText().toString();
RelativeLayout ll = (RelativeLayout)v.getParent();
textView = (TextView)ll.findViewById(R.id.label);
Toast.makeText(CustomListView.this, name,
Toast.LENGTH_SHORT).show();
Easy to do:
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout rl = (RelativeLayout)v.getParent();
TextView tv = (TextView)rl.findViewById(R.id.label);
String text = tv.getText().toString();
Toast.makeText(CustomListView.this, text, Toast.LENGTH_SHORT).show();
}
});
use setTag attribute of the View..............
as
Button call = (Button) rowView.findViewById(R.id.button1);
call.setTag(position);
and
call.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int which = -1;
Obejct obj = v.getTag();
if(obj instaceof Integer){
which = ((Integer)obj).intValue();
}
if(which >-1){
String name = values[which];
Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
}
}
});
If you have a ListActivity, and you're not using your own adapter, you can still get the list item belonging to the tapped button, like so:
In your layout file of the list row:
<ImageButton
android:id="#+id/button_call"
android:layout_height="48dip"
android:layout_width="48dip"
android:contentDescription="Call"
android:onClick="callBuddy"
android:src="#drawable/call_button_image"
/>
In your ListActivity:
public void callBuddy(View view) {
int position = getListView().getPositionForView((View) view.getParent());
Buddy buddyToCall = (Buddy) getListView().getItemAtPosition(position);
Toast.makeText(MyListActivity.this, String.format("Calling your buddy %s.", buddyToCall.name), Toast.LENGTH_SHORT).show();
}
simply use getItem() and pass the position
Ex:getItem(position).getID()
here getID() method is getter method
Set onClick="click" to xml of button/image/etc...
and in your Activity, do:
public void click(View v) {
final int position = getListView().getPositionForView(v);
String text = getListView().getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext, text, Toast.LENGTH_SHORT).show();
}