简体   繁体   中英

this custom array adapter is not working on integers

i have written a custom array adapter for practice so.. this below code is working when i am using strings as variable in data.java class but if i use integers variables instead it will get installed in the AVD but will not open...

main activity.java

enter code here

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<data> arr= new ArrayList<>();
    arr.add(new data("2","6","india","4","22222"));
    ListView listView=findViewById(R.id.list);
    adpater a=new adpater(this,arr);
    listView.setAdapter(a);
 }
}

adapter.java

public class adpater  extends ArrayAdapter<data> {
public adpater(Activity context, List<data> arrayList) {
    super(context,0, arrayList);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if(convertView==null){
        convertView= LayoutInflater.from(getContext()).inflate(R.layout.list,parent,false);
    }
    data currentitem = getItem(position);
    TextView textView=convertView.findViewById(R.id.location);
    TextView textView1=convertView.findViewById(R.id.recovered);
    TextView textView2=convertView.findViewById(R.id.dead);
    TextView textView3=convertView.findViewById(R.id.confirmed);
    TextView textView4=convertView.findViewById(R.id.updated);
    textView.setText(currentitem.getCountry());
    textView1.setText(currentitem.getRecovered());
    textView2.setText(currentitem.getDead());
    textView3.setText(currentitem.getConfirmed());
    textView4.setText(currentitem.getUpdated());
    return  convertView;
 }
}

data.java

public class data {
private  String  confirmed;
 private String dead;
private String country;
private String recovered;
private String updated;





public String  getConfirmed() {
    return confirmed;
}

public String getDead() {
    return dead;
}

public String getRecovered() {
    return recovered;
}

public String getCountry() {
    return country;
}

public String getUpdated() {
    return updated;
}
public data(String Confirmed,String Dead,String Country,String Recovered,String Updated){
    confirmed=Confirmed;
    dead=Dead;
    country=Country;
    recovered=Recovered;
    updated=Updated;
 }
}

my problem is that if i will use any of the integer variables here..it will not work. i dont know if its good question but this really messed me up...

Root cause

When you use a String variable, the TextView will use setText(CharSequence) method and it works well. But when you use an int variable, the TextView will use setText(int) method, the param is a special int type, it refers to the int resource type in R class that created by the compiler in build time, it won't understand if you pass a normal int to it.

Solution

Cast the int to String type, so the TextView will use setText(CharSequence) method instead.

Assume in data class, the confirmed variable is an int type, then change your code from

textView3.setText(currentitem.getConfirmed());

to

textView3.setText(currentitem.getConfirmed() + "");

or

textView3.setText(String.valueOf(currentitem.getConfirmed()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM