0

In ListView row item where is two TextView which is textview(name) and textview(mobile no), when i click textView(mobile no) then same value come again and again, but in list view every list giving correct mobile no.

public class CustomerDetails extends AppCompatActivity {

ArrayList<com.venue.entities.CustomerDetails> arrayList;
Map<String, Boolean> isSelectedMap;
HashMap<String, Bookings> mapBookingsList;
ListView listView;
LinearLayout linearLayout;
com.venue.entities.CustomerDetails customerDetails;
Viewholder viewHolder;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(getResources().getColor(R.color.primary_color_dark));
    }
    setContentView(R.layout.customer_details_layout);

    listView = (ListView) findViewById(R.id.customerDetials_listview);

    HashMap<String, Bookings> mapBookingsList = new HashMap<>();
    ArrayList<String> dates = new ArrayList<String>();
    init();

    try {
        String bookingJson = (String) getIntent().getStringExtra("booking");
        String selectedJson = (String) getIntent().getStringExtra("selected");
        Type bookingType = new TypeToken<HashMap<String, Bookings>>() {
        }.getType();
        Type selectedType = new TypeToken<HashMap<String, Boolean>>() {
        }.getType();
        mapBookingsList = new Gson().fromJson(bookingJson, bookingType);
        isSelectedMap = new Gson().fromJson(selectedJson, selectedType);
        for (Map.Entry<String, Boolean> entry : isSelectedMap.entrySet()) {
            dates.add(entry.getKey());
        }
        arrayList = new ArrayList<>();
        for (String date : dates) {
            if (mapBookingsList.containsKey(date)) {
                Bookings obj = mapBookingsList.get(date);
                arrayList.add(new com.venue.entities.CustomerDetails(obj.getCustomer_name(),
                        obj.getCustomer_contact(), obj.getFor_date()));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


    CommonBaseAdapter adapter = new CommonBaseAdapter(CustomerDetails.this, arrayList) {


        @Override
        public View getViews(final int position, View convertView, ViewGroup parent, ArrayList<?> arrayList) {
            viewHolder = new Viewholder();
            customerDetails = (com.venue.entities.CustomerDetails) arrayList.get(position);

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.customer_details_item_layout, parent, false);
                viewHolder.dateTextView = (TextView) convertView.findViewById(R.id.cutomerDetails_textView_date);
                viewHolder.customerNameTextView = (TextView) convertView.findViewById(R.id.cutomerDetails_textView_name);
                viewHolder.customerMobileTextView = (TextView) convertView.findViewById(R.id.cutomerDetails_textView_number);
                viewHolder.imageButtonCall= (ImageButton) convertView.findViewById(R.id.callButtonImage);

            }
            else {
                 viewHolder = (Viewholder) convertView.getTag();
            }

            if(customerDetails.getName().equals(""))
            {
                viewHolder.customerNameTextView.setText(R.string.name_not_available);

            }
            else
            {
                viewHolder.customerNameTextView.setText(customerDetails.getName());
            }

            if(customerDetails.getMobileNumber().equals(""))
            {
                viewHolder.customerMobileTextView.setText(R.string.contact_not_available);
                viewHolder.imageButtonCall.setVisibility(View.GONE);
            }
            else
            {
                viewHolder.customerMobileTextView.setText(customerDetails.getMobileNumber());
            }

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = format.parse(customerDetails.getFor_date());
                SimpleDateFormat sdf = new SimpleDateFormat("dd MMM, yyyy");
                String dateString = sdf.format(date);

                viewHolder.dateTextView.setText(dateString);
            } catch(Exception e){
                e.printStackTrace();
            }

            //Direct Call
            viewHolder.imageButtonCall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    //TODO here when click same no. is shwoing...
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    String srt = customerDetails.get(position).getMobileNumber();
                    callIntent.setData(Uri.parse("tel:" + srt));
                    startActivity(callIntent);
                }
            });

            return convertView;
        }
    };
    listView.setAdapter(adapter);
}

public class Viewholder {
    TextView dateTextView;
    TextView customerNameTextView;
    TextView customerMobileTextView;
    ImageButton imageButtonCall;
}

private void init() {
    new LogoutUtils(this).registerReceiver(logoutReceiver);

    ActionBar actionBar = getSupportActionBar();
    ActionBarUtils actionBarUtils = new ActionBarUtils(CustomerDetails.this);
    actionBarUtils.setActionBar(actionBar);
    actionBarUtils.setTitleTextView(getString(R.string.customer_detail));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action buttons
    switch (item.getItemId()) {
    case R.id.action_settings:
        new SettingsDialog(CustomerDetails.this).showDialog();
        return true;
    case android.R.id.home:
        onBackPressed();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onDestroy() {
    try {
        new LogoutUtils(CustomerDetails.this).commonFunctionality(logoutReceiver);
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onDestroy();
}

private final BroadcastReceiver logoutReceiver = new    BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
}

CustomerDetails.java

public class CustomerDetails implements Serializable{

String name;
String mobileNumber;
String for_date;

public CustomerDetails(String name, String mobileNumber, String for_date){
    this.name = name;
    this.mobileNumber = mobileNumber;
    this.for_date = for_date;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getMobileNumber() {
    return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}
public String getFor_date() {
    return for_date;
}
public void setFor_date(String for_date) {
    this.for_date = for_date;
}

}

customer_details_item_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout            xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <RelativeLayout
        android:id="@+id/ll_customer_details_row"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/gray_drawer">

        <com.venue.components.TextViewCircular
            android:id="@+id/cutomerDetails_textView_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:textSize="10sp" />

        <com.venue.components.TextViewCircular
            android:id="@+id/cutomerDetails_textView_name"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@+id/cutomerDetails_textView_date"
            android:paddingLeft="5dp" />

        <com.venue.components.TextViewCircular
            android:id="@+id/cutomerDetails_textView_number"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@+id/cutomerDetails_textView_name"
            android:layout_marginLeft="5dp" />

        <ImageButton
            android:id="@+id/callButtonImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/cutomerDetails_textView_name"
            android:layout_marginRight="5dp"
            android:background="@color/transparent"
            android:src="@drawable/phone" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

</LinearLayout>
Sikander Bhutto
  • 226
  • 1
  • 11
  • On a side note, use the .equal() operator instead of == for strings. See [this](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) on why. – agomes May 25 '16 at 11:28

3 Answers3

1

Well you have missed some important things in your code.

@Override
public View getViews(int position, View convertView, ViewGroup parent, ArrayList<?> arrayList) {
    Viewholder viewHolder ;
    customerDetails = (com.venue.entities.CustomerDetails) arrayList.get(position);

    if (convertView == null) {
        viewHolder = new Viewholder();//initilize viewholder here
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.customer_details_item_layout, parent, false);
        //your code
        viewHolder.imageButtonCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos= (int) v.getTag();//getting clicked item position
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                String mobileNumber=arrayList.get(pos).getMobileNumber();//get the mobile number from clicked position 
                callIntent.setData(Uri.parse("tel:" + mobileNumber));
                startActivity(callIntent);
            }
        });
        convertView.setTag(viewHolder);//set viewholder to converview
    } else {
        viewHolder = (Viewholder) convertView.getTag(); //getting the viewholder
    }

    //your code
    if (customerDetails.getMobileNumber().equals("")) {
       //no change
    } else {
        viewHolder.customerMobileTextView.setText(customerDetails.getMobileNumber());
        viewHolder.imageButtonCall.setTag(position);//set position so that whenever image clicks you get the exact clicked position.
    }
    //your code
    return convertView;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • i used this line and it is working String srt = ((com.venue.entities.CustomerDetails)arrayList.get(position)).getMobileNumber(); – Sikander Bhutto May 26 '16 at 05:12
0

Change in this Line. You need to take position of each item.

Use customerDetails.get(position).getMobileNumber().

viewHolder.customerMobileTextView.setText(customerDetails.get(position).getMobileNumber());
Community
  • 1
  • 1
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • where have to change, i think here viewHolder.imageButtonCall.setOnClickListener – Sikander Bhutto May 25 '16 at 11:51
  • @SikanderBhutto Yes change it their and make `int position` as final so it will allow you to set inside `onClickListener`. – Jay Rathod May 25 '16 at 11:52
  • @SikanderBhutto What error it is showing ? make int position as final if it is not allowing you to use. – Jay Rathod May 25 '16 at 11:57
  • @Override public View getViews(final int position, View convertView, ViewGroup parent, ArrayList> arrayList) { – Sikander Bhutto May 25 '16 at 12:00
  • //Direct Call viewHolder.imageButtonCall.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //TODO here when click same no. is shwoing... Intent callIntent = new Intent(Intent.ACTION_CALL); //String srt = viewHolder.customerMobileTextView.getText(customerDetails.get(position).getMobileNumber()); callIntent.setData(Uri.parse("tel:" + customerDetails.getMobileNumber().get(position))); startActivity(callIntent); } }); – Sikander Bhutto May 25 '16 at 12:01
  • @SikanderBhutto No not like this `get(position)` will come before `getMobileNumber()` like this `callIntent.setData(Uri.parse("tel:" + customerDetails.get(position).getMobileNumber()));`. – Jay Rathod May 25 '16 at 12:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112906/discussion-between-jaydroider-and-sikander-bhutto). – Jay Rathod May 25 '16 at 12:03
  • callIntent.setData(Uri.parse("tel:" + customerDetails.get(position).getMobileNumber())); – Sikander Bhutto May 25 '16 at 12:05
  • first time position is showing red underline and solved with with final but now get is showing red – Sikander Bhutto May 25 '16 at 12:06
  • error like this ------ Cannot resolve method 'get (int)' – Sikander Bhutto May 25 '16 at 12:08
  • 1
    @SikanderBhutto Please continue in chat. – Jay Rathod May 25 '16 at 12:08
  • where i can chat?? – Sikander Bhutto May 25 '16 at 12:13
  • @SikanderBhutto You can see above comment i have moved `Let us continue this discussion in chat` click on that. – Jay Rathod May 25 '16 at 12:14
0

Change this line only:

String srt = ((com.venue.entities.CustomerDetails)arrayList.get(position)).getMobileNumber();

You can get position from arrayList like this:

//Direct Call
        viewHolder.imageButtonCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                //TODO here when click same no. is showing...

String srt = ((com.venue.entities.CustomerDetails)arrayList.get(position)).getMobileNumber();
Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + srt));
                startActivity(callIntent);
            }
        });
halfer
  • 19,824
  • 17
  • 99
  • 186
Sikander Bhutto
  • 226
  • 1
  • 11