I have a ListView in my app and each item in the ListView contains a button and item count in it.While clicking on the button in each item, I want to show a dialogue with an EditText to enter new count of the corresponding item and update the item with the value which i get from the dialogue EditText field.
I created dialogue to enter new count on button click, but can't update the value.
My adapter
public class MyAdapter extends BaseAdapter {
#Override
public int getCount() {
return planList.size();
}
#Override
public Object getItem(int position) {
return planList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final SalesModel db_data = planList.get(position);
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.return_confirm, null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
final TextView stock = (TextView) convertView.findViewById(R.id.stock);
TextView amount = (TextView) convertView.findViewById(R.id.amount);
ImageView minus = (ImageView) convertView.findViewById(R.id.minus);
Double count = Double.parseDouble(db_data.getStock());
Double price = Double.parseDouble(db_data.getSprice());
Double s_price = count*price;
String set_amount = s_price.toString();
name.setText(db_data.getName());
stock.setText(db_data.getStock());
amount.setText(set_amount);
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stock_return = ShowDialogue();
Double n_stock = Double.parseDouble(db_data.getStock())-Double.parseDouble(stock_return);
stock.setText(n_stock.toString());
}
});
return convertView;
}
}
Function to show dialogue
public String ShowDialogue(){
String stk_val;
stock_return = "0.0";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
dialog.setCancelable(false);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
Button ok = (Button)dialogLayout.findViewById(R.id.later );
Button close = (Button)dialogLayout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(stk.getText().toString().trim().isEmpty()){
stk.setError("Enter quantity");
}
else {
stock_return = stk.getText().toString().trim();
}
}
});
dialog.setView(dialogLayout,0,0,0,0);
dialog.show();
return stock_return;
}
you doing something wrong.
Dialog cannot return before you enter ok button.
Try like this :
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stock_return = ShowDialogue(this, position);
}
});
Change dialog to accordance with it like this..
public void ShowDialogue(MyAdapter myAdapter, int position){
String stk_val;
stock_return = "0.0";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final AlertDialog dialog = builder.create();
dialog.setCancelable(false);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.popup_reminder, null);
final EditText stk = (EditText)dialogLayout.findViewById(R.id.stock);
Button ok = (Button)dialogLayout.findViewById(R.id.later );
Button close = (Button)dialogLayout.findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(stk.getText().toString().trim().isEmpty()){
stk.setError("Enter quantity");
}
else {
planList.get(position).setStock(stk.getText().toString().trim());
dialog.dismiss();
myAdapter.notifyDataSetChanged();
}
}
});
dialog.setView(dialogLayout,0,0,0,0);
dialog.show();
}
Related
I am working on an app in android studio.
My app has a listview, whiche has Edittext , textview and checkbox in its row.
my proplem is when I have more than five items in my listvew, the listview lost focus, for example: when I press on checkbox on the 6th item , the textview shows me the name from the first item.
I wish I could explain my proplem very well.
This is my adapter:
public AdapterListView(Context context, int resource, ArrayList<ObjectPeople> arraypeople) {
super(context, resource);
this.mContext = context;
this.arrayPeople = arraypeople;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, null);
holder = new ViewHolder();
holder.mission_name = (TextView) convertView.findViewById(R.id.mission_name);
holder.mission_time = (TextView) convertView.findViewById(R.id.mission_time);
holder.edit_time = (EditText) convertView.findViewById(R.id.edit_time);
holder.mission_image= (ImageView) convertView.findViewById(R.id.mission_image);
holder.cbm = (CheckBox) convertView.findViewById(R.id.checkbo);
convertView.setTag(holder);
holder.cbm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v ) {
CheckBox cb = (CheckBox) v ;
final ObjectPeople person = arrayPeople.get(position);
if(cb.isChecked()) {
arrayPeople.get(position).checkbox= true;
int mis_tm=0;
try{ mis_tm= Integer.parseInt( holder.edit_time.getText().toString());}
catch (Exception e){
cb.setChecked(false);
Toast.makeText(mContext, "يرجى إدخال قيمة معينة", Toast.LENGTH_SHORT).show();}
person.time= mis_tm;
if(mis_tm>0&&mis_tm<1200){
holder.mission_name.setText("اسم المهمة: "+person.name);
holder.mission_time.setText( "مدة المهمة: "+ person.time );
holder.edit_time.setVisibility(View.GONE);}
else{
cb.setChecked(false);
Toast.makeText(mContext, "يرجى اختيار رقم حقيقي", Toast.LENGTH_SHORT).show();
}
}
else {
arrayPeople.get(position).checkbox= false;
holder.edit_time.setVisibility(View.VISIBLE);
}
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
final ObjectPeople person = arrayPeople.get(position);
holder.mission_name.setText("اسم المهمة: "+person.name);
holder.mission_time.setText( "مدة المهمة: ");
holder.mission_image.setImageResource(arrayPeople.get(position).image);
holder.edit_time.setId(position);
holder.edit_time.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
try {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
arrayPeople.get(position).time = Integer.parseInt(Caption.getText().toString());
}
}catch (Exception e){}
}
});
holder.cbm.setTag(arrayPeople);
return convertView;
}
#Override
public int getCount() {
return arrayPeople.size();
}
static class ViewHolder {
TextView mission_name;
TextView mission_time;
EditText edit_time;
ImageView mission_image;
CheckBox cbm;
}
}
and this select.java which contains the arraylist and list view:
public class select extends AppCompatActivity {
ArrayList<ObjectPeople> arrPeople;
ListView lvPeople;
AdapterListView adapter;
ObjectPeople person;
SharedPreferences settings;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
settings = getApplicationContext().getSharedPreferences("shared", 0);
lvPeople= (ListView) findViewById(R.id.lvPeopl);
arrPeople=new ArrayList<>();
person = new ObjectPeople("حفظ قرآن", 0,false, R.drawable.quran_hifz_r);
arrPeople.add(person);
person = new ObjectPeople("تلاوة قرآن", 0 ,false,R.drawable.img);
arrPeople.add(person);
person = new ObjectPeople("قراءة كتاب", 0 , false,R.drawable.books_r);
arrPeople.add(person);
person = new ObjectPeople("دورات تطوير مهارات", 0 , false,R.drawable.courses_r);
arrPeople.add(person);
person = new ObjectPeople("رياضة", 15 , false,R.drawable.sport_r);
arrPeople.add(person);
final String[] misson_name = new String[1];
Button adf= (Button) findViewById(R.id.add_mission_out_btn);
adf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View view = LayoutInflater.from(select.this).inflate(R.layout.add_mission_lyt,null);
final EditText add_name = (EditText) view.findViewById(R.id.add_mission_name);
AlertDialog.Builder builder = new AlertDialog.Builder(select.this);
builder.setMessage("add your mission")
.setView(view)
.setPositiveButton("إضافة", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
misson_name[0] = add_name.getText().toString();
arrPeople.add(new ObjectPeople(misson_name[0], 0, true,R.drawable.smile_small_r));
}
})
.setNegativeButton("إلغاء",null)
.setCancelable(false);
AlertDialog alert =builder.create();
alert.show();
}
});
adapter=new AdapterListView(this,R.layout.item_listview,arrPeople);
lvPeople.setAdapter(adapter);
checkButtonClick();
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.btn_save);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences settings = getApplicationContext().getSharedPreferences("shared", 0);
int hours2=settings.getInt("hours",0)*60;
ArrayList<ObjectPeople> selected_missions = new ArrayList<>();
int missoins_time_calc=0;
for (int i = 0; i <arrPeople.size(); i++) {
if(arrPeople.get(i).checkbox==true){
selected_missions.add(arrPeople.get(i));
missoins_time_calc= missoins_time_calc + arrPeople.get(i).time;}
}
if (hours2 == missoins_time_calc){
Gson gson = new Gson();
String jsonText = gson.toJson(selected_missions);
SharedPreferences.Editor editor = settings.edit();
editor.putString("selected_array", jsonText);
editor.commit();
Intent intent = new Intent(select.this, yourprogram.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
else if (hours2 > missoins_time_calc){
Toast.makeText(select.this, "بقي"+(-missoins_time_calc+ hours2)+" دقيقة ", Toast.LENGTH_SHORT).show();}
else if (hours2< missoins_time_calc){
Toast.makeText(select.this,"يوجد "+ (-hours2+missoins_time_calc)+" دقيقة زيادة على الوقت المخصص", Toast.LENGTH_SHORT).show();
}
/* ArrayList<missions> missionsList = dataAdapter.missionsList;
for(int i = 0; i< missionsList.size(); i++){
missions missions = missionsList.get(i);
if(missions.isSelected()){}}*/
}
});
}
}
I think the error is the way your arrayPeople List is being populated, if you say the sixth item gets you the first you can try reversing the order with
Collections.reverse(arrayPeople);
you can place this line of code below your holder.cbm.setOnClickListener on your adapter
Hope it helps.
Adapter Class:
public List<TSPDataModel> employeeData;
private Context mContext;
private LayoutInflater mInflater;
RadioGroup radiogroupbutton;
String[] data = {"Document not clear","Adress is not Visibile","Photo is not pasted","Signature is not Avilable"};
String value;
public TSPListDocumentadapter(Context context, int textViewResourceId,
List<TSPDataModel> objects)
{
this.employeeData = objects;
this.mContext = context;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.tspdocumentlistitem, null);
holder.relatvie1=(RelativeLayout)convertView.findViewById(R.id.relatvie1);
holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
holder.accecpt = (ImageView) convertView.findViewById(R.id.imageButton);
holder.reject = (ImageView) convertView.findViewById(R.id.imageButton2);
holder.statustextview = (TextView) convertView.findViewById(R.id.statustextview);
holder.poaedittext=(TextView) convertView.findViewById(R.id.poieditext);
holder.poaedittext=(TextView)convertView.findViewById(R.id.poaedittext);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(employeeData.get(position).getName());
holder.poaedittext.setText(employeeData.get(position).getPoa());
holder.accecpt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.statustextview.setText("Accepted");
employeeData.get(position).setSelected(true);
employeeData.get(position).getOrderId();
holder.relatvie1.setBackgroundResource(R.color.acceptedcolor);
}
});
holder.reject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showdilog();
holder.statustextview.setText("Rejected");
employeeData.get(position).setSelected(false);
employeeData.get(position).setReasone(value);
holder.relatvie1.setBackgroundResource(R.color.rejectcolor);
}
});
return convertView;
}
static class ViewHolder {
TextView txtName;
ImageView reject;
ImageView accecpt;
TextView statustextview;
TextView poiedittext;
TextView poaedittext;
RelativeLayout relatvie1;
}
public int getCount() {
return employeeData.size();
}
public TSPDataModel getItem(int position) {
return employeeData.get(position);
}
public long getItemId(int position) {
return 0;
}
public void showdilog() {
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.layoutpopup);
radiogroupbutton = (RadioGroup) dialog.findViewById(R.id.radio_gp_day);
ListView listview = (ListView) dialog.findViewById(R.id.radio_slot_list);
Button setbutton = (Button) dialog.findViewById(R.id.setbutton);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> myadpter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_single_choice, data);
for (int i = 0; i < data.length; i++) {
list.add(data[i]);
}
listview.setAdapter(myadpter);
listview.setItemsCanFocus(false);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
value = data[position];
Toast.makeText(mContext, value, Toast.LENGTH_LONG).show();
}
});
setbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, value, Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
This Button click in my actvity class :
#Override
public void onClick(View view) {
if (view.getId() == R.id.button1) {
try {
List<TSPDataModel> empData = adapter.employeeData;
System.out.println("Total Size :" + empData.size());
for (TSPDataModel employeeModel : empData) {
if (employeeModel.isSelected()) {
Toast.makeText(TSPDocumentListActvity.this, employeeModel.getName(), Toast.LENGTH_LONG).show();
} else {
String Reasonse= employeeModel.getresonse() ;
Toast.makeText(TSPDocumentListActvity.this, "false" + employeeModel.getName(), Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
First i Print data in Listview then each list view item there is accept and reject Button is there when we click on accept then no alert will asked only reject button reason will ask which is come on listitem on popup i want to get that selected reason on Button click in actvity but i always get null value please help me where i am doing wrong
change your reject button and showDialog code to
holder.reject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showdilog(position);
holder.statustextview.setText("Rejected");
holder.relatvie1.setBackgroundResource(R.color.rejectcolor);
}
});
and showDualog to
public void showdilog(int list_position) {
final Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.layoutpopup);
radiogroupbutton = (RadioGroup) dialog.findViewById(R.id.radio_gp_day);
ListView listview = (ListView) dialog.findViewById(R.id.radio_slot_list);
Button setbutton = (Button) dialog.findViewById(R.id.setbutton);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> myadpter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_single_choice, data);
for (int i = 0; i < data.length; i++) {
list.add(data[i]);
}
listview.setAdapter(myadpter);
listview.setItemsCanFocus(false);
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
value = data[position];
employeeData.get(list_position).setSelected(false);
employeeData.get(list_position).setReasone(value);
Toast.makeText(mContext, value, Toast.LENGTH_LONG).show();
}
});
setbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, value, Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
public class SoloPlayActivity extends BaseActivity implements View.OnClickListener{
private ListView case_list;
private RelativeLayout add_case;
private TextView num_case_textview;
private Button start_button;
ArrayList<ItemSoloplayCase> caseArrayList;
AdapterSoloplay adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_solo_play);
initView();
//처음에는 경우의 수가 아무것도 없음
num_case_textview.setText("0");
caseArrayList = new ArrayList<ItemSoloplayCase>();
adapter = new AdapterSoloplay(this, caseArrayList, num_case_textview);
case_list.setAdapter(adapter);
}
private void initView(){
add_case = (RelativeLayout)findViewById(R.id.add_case);
add_case.setOnClickListener(this);
num_case_textview = (TextView)findViewById(R.id.num_case);
start_button = (Button)findViewById(R.id.game_start);
start_button.setOnClickListener(this);
case_list = (ListView)findViewById(R.id.case_list);
//리스트뷰에서 포커스를 잃지 않도록 한다.
case_list.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
}
#Override
protected String getActionbarTitle() {
return getString(R.string.solo_play_name);
}
//뒤로가기가 눌렸을때 추가한 경우의 수가 있을경우에만 다이얼로그 띄우고 경우의수가 비었다면 그냥 종료
#Override
public void onBackPressed() {
if (!caseArrayList.isEmpty()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("입력한 경우의 수는 모두 사라집니다. 정말로 닫겠습니까?")
.setCancelable(false)
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
SoloPlayActivity.this.finish();
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
else
SoloPlayActivity.this.finish();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.game_start:
Toast.makeText(getApplicationContext(), "게임시작 버튼 누름", Toast.LENGTH_SHORT);
break;
case R.id.add_case:
//경우의수 추가
ItemSoloplayCase additem = new ItemSoloplayCase();
caseArrayList.add(additem);
num_case_textview.setText(Integer.toString(caseArrayList.size()));
adapter.notifyDataSetChanged();
break;
}
}}
activity file.
Adapter file.
public class AdapterSoloplay extends BaseAdapter {
private Context context;
private ArrayList<ItemSoloplayCase> caselist;
private TextView num_case;
//순서 갱신 오류, 해당 지운 에디트가 지워지지 않음
public AdapterSoloplay(Context context, ArrayList<ItemSoloplayCase> caselist, TextView num_case) {
this.context = context;
this.caselist = caselist;
this.num_case = num_case;
}
#Override
public int getCount() {
return caselist.size();
}
#Override
public Object getItem(int position) {
return caselist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Viewholder holder;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_case_input, parent, false);
holder = new Viewholder();
holder.order = (TextView) convertView.findViewById(R.id.order_num);
holder.editText = (EditText)convertView.findViewById(R.id.input_case);
holder.button_clear = (ImageButton)convertView.findViewById(R.id.remove_case);
//순서는 현재 크기만큼
holder.order.setText(Integer.toString(getCount()));
convertView.setTag(holder);
}
else
holder = (Viewholder)convertView.getTag();
//클리어를 눌럿을때 제거하고 순서를 재정렬하고 갱신시킨다
holder.button_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ItemSoloplayCase item = (ItemSoloplayCase)getItem(position);
item = null;
caselist.remove(position);
notifyDataSetChanged();
num_case.setText(Integer.toString(caselist.size()));
}
});
return convertView;
}
class Viewholder {
private EditText editText;
private TextView order;
private ImageButton button_clear;
}
}
i want to make dynamically add or remove edit text in list view. '
like image.
enter image description here
if i want remove second list(in this image, b) but it removed last data(in this image, d). and i click add button it makes last data..(in this image, makes edit text value d).
above is my code. help.
please help me.
Add a linearlayout to listview rows xml file :
For add dynamically EditText to this layout try this :
EditText editText = new EditText(context);
editText.setHint(hint);
editText.requestFocus();
editText.setSelection(editText.getText().length());
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
linearlayout.addview(editText);
This line will not set, holder.t1.setText(NewItem);
If I move it to the parent onClick, with a hardcoded string (for testing) it does.
Keep in mind, this is inside a getView method of an ArrayAdapter. I am trying to setText to ListView rows.
Edit:
EXPANDED, COMPLETE getView() -- AS REQUESTED
(Did not have time to edit, will later, sorry!)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.commentlayout, parent,
false);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.labelComment);
holder.t2 = (TextView) convertView.findViewById(R.id.labelDate);
holder.t3 = (TextView) convertView.findViewById(R.id.labelUser);
holder.t3.setTypeface(tf);
holder.t4 = (TextView) convertView
.findViewById(R.id.labelHelpfulCount);
holder.t5 = (TextView) convertView
.findViewById(R.id.labelCommentCount);
holder.ib1 = (ImageView) convertView
.findViewById(R.id.labelChatIcon);
holder.ib2 = (ImageView) convertView
.findViewById(R.id.labelCommentFlag);
holder.rb1 = (RatingBar) convertView
.findViewById(R.id.myCommentsRatingBarSmall);
holder.b1 = (Button) convertView.findViewById(R.id.bReview1);
holder.b2 = (Button) convertView.findViewById(R.id.bReview2);
holder.b3 = (Button) convertView.findViewById(R.id.bReview3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
convertView.setOnCreateContextMenuListener(null);
}
ReviewObject ro = getItem(position);
final String item = ro.item;
final String review = ro.review;
final String username = ro.username;
Long date = Long.valueOf(ro.date);
String rating = ro.ratings;
String voteCount = ro.voteCount;
String chatcount = ro.chatCount;
String cat = ro.cat;
final ArrayList<String> passing = new ArrayList<String>();
passing.add(item);
passing.add(review);
passing.add(cat);
passing.add(username);
String time = "";
time = DateConvert.dateConvert(Long.valueOf(date));
holder.t1.setText(review);
holder.t2.setText(time);
holder.t3.setText(username);
holder.t4.setText(voteCount);
holder.t5.setText(chatcount);
holder.ib1.setImageResource(R.drawable.updown);
holder.ib2.setImageResource(R.drawable.comment);
holder.rb1.setRating(Float.valueOf(rating));
if (rating.equals("0")) {
holder.rb1.setEnabled(false);
}
holder.b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ReviewUser = holder.t3.getText().toString();
String ReviewWords = holder.t1.getText().toString();
Intent intent = new Intent(getContext(), Comments.class);
intent.putExtra("comment", ReviewWords);
intent.putExtra("user", ReviewUser);
intent.putExtra("item", item);
getContext().startActivity(intent);
}
});
if (!Rateit.username.equals(username)) {
holder.b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(getContext());
alertbox.setMessage("Did you like this?");
alertbox.setNegativeButton("Vote Up",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface arg0,
int arg1) {
String VoteTally = holder.t4.getText()
.toString();
int ReviewCountInt = Integer
.valueOf(VoteTally) + 1;
VoteTally = String.valueOf(ReviewCountInt);
holder.t4.setText(VoteTally);
new HelpfulTask().execute(passing);
}
});
alertbox.setPositiveButton("Vote Down",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface dialog,
int id) {
String VoteTally = holder.t4.getText()
.toString();
int ReviewCountInt = Integer
.valueOf(VoteTally) - 1;
VoteTally = String.valueOf(ReviewCountInt);
holder.t4.setText(VoteTally);
new UnHelpfulTask().execute(passing);
}
});
alertbox.setNeutralButton("Report Spam",
new DialogInterface.OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(DialogInterface dialog,
int id) {
new SpamTask().execute(passing);
}
});
alertbox.show();
}
});
} else {
holder.b2.setText("Edit");
holder.b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(getContext());
dialog.setContentView(R.layout.editreview);
dialog.setTitle("Edit Review");
dialog.show();
final EditText etEdit = (EditText) dialog
.findViewById(R.id.etEditReview);
etEdit.setText(review);
Button bInsert = (Button) dialog.findViewById(R.id.bInsert);
bInsert.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
NewItem = etEdit.getText().toString();
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
} else {
holder.t1.setText(NewItem);
passing.add(NewItem);
dialog.dismiss();
new EditCommentTask().execute(passing);
}
}
});
}
});
}
if (!Rateit.username.equals(username)) {
holder.b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getContext(), OtherProfile.class);
i.putExtra("userprofile", username);
getContext().startActivity(i);
}
});
} else {
holder.b3.setText("Delete");
holder.b3.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(
getContext());
alertbox.setMessage("Are you sure you want to delete your review?");
alertbox.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
});
alertbox.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
new DeleteReviewTask().execute(passing);
}
});
alertbox.show();
}
});
}
return convertView;
}
From what you've said in the above comments, this is the intended function. Since,
Except, the dialog does dismiss, I get the toast and the task runs correctly.
This is coded into your onClick() here:
bInsert.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
public void onClick(View v) {
NewItem = etEdit.getText().toString();
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
} else {
holder.t1.setText(NewItem);
passing.add(NewItem);
dialog.dismiss();
new EditCommentTask().execute(passing);
}
}
});
Specifically, this is executing:
if (NewItem.equals("")) {
Toast.makeText(getContext(),
"Please add something first.",
Toast.LENGTH_SHORT).show();
}
so I am assuming your problem is with NewItem, I never see where you actually initialize it, but I am assuming it .equals("") since this is executing. Try throwing a Log.d or println() just below the line NewItem = etEdit.getText().toString(); to see what the value of NewItem is at this point.
I have a listview with an ArrayAdapter. The ListView at a position fills itselfs with the same position in some ArrayList. In each listitem
there are two buttons. When the onClickListener start, it gets
the position of the overrided View getView(position, View etc.), takes some variables out of the ArrayList and sends it to a database. The problem is, the position is not really accurate. Sometimes I get the position of a Listitem above the Listitem I want. Here is the code:
public class MemoAdapter extends ArrayAdapter<String> {
private final Activity context;
ArrayList<String> commentarray;
ArrayList<String> fromarray;
ArrayList<String> datearray;
ArrayList<String> ids;
ArrayList<String> feedback;
Dialog dialog;
EditText feedbacktxt;
String token;
String website;
public MemoAdapter(Activity context, ArrayList<String> commentArray,
ArrayList<String> fromArray, ArrayList<String> dateArray,
ArrayList<String> ids, ArrayList<String> feedback, String token,
String website) {
super(context, R.layout.memo_listitem, commentArray);
this.context = context;
this.commentarray = commentArray;
this.fromarray = fromArray;
this.datearray = dateArray;
this.ids = ids;
this.feedback = feedback;
this.token = token;
this.website = website;
}
#Override
public long getItemId(int position) {
return 0;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public TextView textView;
public TextView textView2;
public TextView textView3;
public TextView textView4;
// public ImageView reply;
// public ImageView accept;
public Button reply;
public Button accept;
public RelativeLayout r;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.memo_listitem, null);
holder = new ViewHolder();
holder.textView = (TextView) rowView
.findViewById(R.id.memodescription);
holder.textView2 = (TextView) rowView.findViewById(R.id.memofrom);
holder.textView3 = (TextView) rowView.findViewById(R.id.memodate);
holder.textView4 = (TextView) rowView
.findViewById(R.id.memofeedback);
holder.reply = (Button) rowView.findViewById(R.id.buttonFeedback);
holder.accept = (Button) rowView.findViewById(R.id.buttonAccepteer);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.textView.setText(commentarray.get(position));
holder.textView2.setText(datearray.get(position));
holder.textView3.setText(fromarray.get(position));
holder.textView4.setText(feedback.get(position));
if (holder.textView4.getText().toString().equals("")
|| holder.textView4.getText().toString().equals(" ")) {
holder.textView4.setText(" - ");
}
// ONCLICKLISTENER FOR REPLYIMAGE
holder.reply.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog = new Dialog(NostradamusActivity2.parentcontext);
dialog.setContentView(R.layout.memo_dialog);
dialog.setTitle("Feedback");
feedbacktxt = (EditText) dialog.findViewById(R.id.memoeddittxt);
Button cancel = (Button) dialog
.findViewById(R.id.memobtncancel);
// CANCEL BUTTON ONCLICKLISTENER REPLYIMAGE
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
Button okay = (Button) dialog.findViewById(R.id.memobtnokay);
// OKAY BUTTON ONCLICKLISTENER REPLYIMAGE
okay.setOnClickListener(new OnClickListener() {
private String errormessage;
private AlertDialog alertDialog;
public void onClick(View v) {
Database2 db = new Database2(
"https://"
+ website
+ "/index2.php?option=com_webservices&controller=json&method=core.memos.memo_feedback&token=",
"token", token, "id", ids.get(position),
"memo_feedback", feedbacktxt.getText()
.toString());
}
// //ACCEPTEER MEMO BUTTON ONCLICKLISTENER
holder.accept.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
NostradamusActivity2.parentcontext);
builder.setMessage("Heeft u de opdracht uitgevoerd?")
.setCancelable(false)
.setPositiveButton("Ja",
new DialogInterface.OnClickListener() {
private AlertDialog alertDialog;
public void onClick(DialogInterface dialog,
int id) {
Database2 dbaccept = new Database2(
"http://intern.koeckers.nl/index2.php?option=com_webservices&controller=json&method=core.memos.memo_state&token=",
"token", token, "id", ids
.get(position),
"memo_completed", "1");
dialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
})
.setNegativeButton("Nee",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
return rowView;
}
}
You are getting the item by ids.get(position).
You are using the arrayList, instead use the getTag() method.
rowView.getTag() will give you the ViewHolder from which, you can get the correct ids and Feedback Text.
Database2 dbaccept = new Database2(
"http://intern.koeckers.nl/index2.php? option=com_webservices&controller=json&method=core.memos.memo_state&token=",
"token", token, "id", ids.get(position),
"memo_completed", "1");