在ListView行项目中,其中有两个TextView,即textview(名称)和textview(移动号码),当我单击textView(移动号码)时,相同的值会一次又一次地出现,但在列表视图中每个列表都给出正确的移动号码
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>
答案 0 :(得分:1)
您错过了代码中的一些重要内容。
@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;
}
答案 1 :(得分:0)
此行更改。您需要获取每个项目的
position
。使用
customerDetails.get(position).getMobileNumber()
。
viewHolder.customerMobileTextView.setText(customerDetails.get(position).getMobileNumber());
答案 2 :(得分:0)
仅更改此行:
String srt = ((com.venue.entities.CustomerDetails)arrayList.get(position)).getMobileNumber();
你可以从arrayList获得这样的位置:
//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);
}
});