将数据放入listview的问题

时间:2016-05-27 20:23:59

标签: android listview android-studio

您好我无法将数据放入listview 我为它创建了资源文件,我为它制作了自定义数组适配器但是当我运行应用程序时它会关闭

这是自定义适配器类

public class MembreAdapter  extends BaseAdapter{
private Context context;
private List<Membre> memb;

public MembreAdapter(Context context, List<Membre> memb) {
    this.context = context;
    this.memb = memb;
}

@Override
public int getCount() {
    return memb.size();
}

@Override
public Object getItem(int position) {
    return memb.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v= View.inflate(context,R.layout.display_member_row,null);
    TextView tnom = (TextView)v.findViewById(R.id.nom_id);
    TextView tprenom = (TextView)v.findViewById(R.id.prenom_id);
    TextView temail = (TextView)v.findViewById(R.id.email_id);
    TextView tnumero= (TextView)v.findViewById(R.id.numero_tele_id);

    tnom.setText(memb.get(position).getNom());
    tprenom.setText(memb.get(position).getPrenom());
    temail.setText(memb.get(position).getEmail());
    tnumero.setText(memb.get(position).getNumero());// thats the line that sstack trace points at 
    return v;
    }
}

以及我想在

中显示数据的类
 BaseDeDonee bdd = new BaseDeDonee(this);//database
    mem=new ArrayList<>();
    Cursor c =bdd.GETallMem();
    //function returns all the data from table person
    c.moveToFirst();
    while(c.moveToNext()){
        nom= c.getString(c.getColumnIndex("nom"));
        prenom=c.getString(c.getColumnIndex("prenom"));
        email=c.getString(c.getColumnIndex("profile"));
        numero=c.getInt(c.getColumnIndex("numero_tel"));

        mem.add(new Membre(nom,prenom,email,numero));
    }

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

   mema= new MembreAdapter(getApplicationContext(),mem);
    listView.setAdapter(mema);
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),"it works " ,Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Stack trace

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

我认为你给setText方法一个整数,这使得它试图找到一个id等于那个整数的字符串资源,这会导致资源找不到异常。

尝试将其更改为

tnumero.setText(""+memb.get(position).getNumero());

答案 1 :(得分:1)

您需要将String传递给setText方法。试试这个 -

tnumero.setText(Integer.toString(memb.get(position).getNumero()));