在为列表视图定制适配器的覆盖getview方法之后,在listview中看不到任何内容

时间:2013-11-25 09:43:15

标签: java android listview android-listview custom-adapter

我是android编程的新手。我正在编写一个android程序,它有两个选项卡:detailstab,用户填写表单并点击保存按钮。 listtab,其中显示用户在edittexts控件的详细信息选项卡中插入的内容。实现这个我做了这个类:

详细java类:

  public class detail {


public   String name;
public   String  adr;
public   String  note;
public int radioValue;

 }

详细说明了我制作一个arraylist并从细节中添加对象的活动类:

public class details extends Activity {

public  ArrayList<detail> detaillist=new ArrayList<detail>();
public EditText etName;
public EditText etAdr;
public  EditText etNote;
public  RadioGroup rg;
public  RadioButton rb_0;
public  RadioButton rb_1;
public  RadioButton rb_2;

 static details detailsActivity;

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

    detailsActivity=this;
    setContentView(R.layout.details_tab);



    Button btnSave=(Button) findViewById(R.id.button1);
    //define an arraylist to add detail object to it 

    etName=(EditText) findViewById(R.id.editText1);
    etAdr=(EditText) findViewById(R.id.editText2);
    etNote=(EditText) findViewById(R.id.editText3);
     rg=(RadioGroup) findViewById(R.id.radioGroup1);
     rb_0=(RadioButton) findViewById(R.id.radio0);
     rb_1=(RadioButton) findViewById(R.id.radio1);
     rb_2=(RadioButton) findViewById(R.id.radio2);



    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final detail d=new detail();
            makeDetailList(d);













        }
    });

}

public void clear(EditText et) {

et.setText("");

 } 
 public ArrayList<detail> makeDetailList(detail d){



     d.name=etName.getText().toString();
      d.adr=etAdr.getText().toString();




        if(rb_0.isSelected())
        {
            d.radioValue=0;

        }
        else if (rb_1.isSelected())
        {
            d.radioValue=1;

        }
        else if (rb_1.isSelected())
        {
            d.radioValue=2;

        }


        detaillist.add(d);

    return detaillist;

 }


 public static details getInstance(){
       return   detailsActivity;
     }

最后列出了活动类,该类包含一个列表视图,用于显示详细信息选项卡中的数据:

     public class list extends Activity {


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_tab);

    ListView lv=(ListView) findViewById(R.id.listView1);

    MyAdapter adapter=new MyAdapter(this,android.R.layout.simple_list_item_1 ,
            R.id.textView1,
            details.getInstance().detaillist);

    lv.setAdapter(adapter);

(this,R.layout.my_list,R.id.textView1,R.id.textView2,));
}
 private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource, int textViewResourceId,
                ArrayList<detail> detaillist) {
            super(context, resource, textViewResourceId);

        }
        @Override
        public View getView(int position, View convertView, ViewGroup  parent) {

            LayoutInflater inflater=(LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.my_list, parent,false);
            ArrayList<detail> items=details.getInstance().detaillist;

            ImageView ivR=(ImageView)      row.findViewById(R.id.imageView1);
            TextView tvN=(TextView) row.findViewById(R.id.textView1);
            TextView tvA=(TextView) row.findViewById(R.id.textView2);

            tvN.setText(items.get(position).name);
            tvA.setText(items.get(position).adr);

            if(items.get(position) != null)
            {
                if(items.get(position).radioValue==0)
                {
                    ivR.setImageResource(R.drawable.t);
                }
                else if(items.get(position).radioValue==1)
                {
                    ivR.setImageResource(R.drawable.t);
                }
                else if(items.get(position).radioValue==2)
                {
                    ivR.setImageResource(R.drawable.t);
                }
            }



            return row;
        }
    }
 }

这是my_list.xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="29dp"
    android:layout_marginTop="24dp"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="TextView"
    android:textColor="#687EFC"
    android:textSize="20sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_marginLeft="31dp"
    android:layout_toRightOf="@+id/imageView1"
    android:text="TextView"
    android:textColor="#18277D"
    android:textSize="30sp" />

</RelativeLayout>

我非常感谢你的帮助 问候

1 个答案:

答案 0 :(得分:0)

更改您的

private class MyAdapter extends ArrayAdapter<String>

进入

private class MyAdapter extends ArrayAdapter<detail>

同时更改

super(context, resource, textViewResourceId);

super(context, resource, textViewResourceId, detaillist);

另外,您应该阅读有关适配器的更多信息。检查一下 answer