列表视图中的内容数据

时间:2012-08-30 07:02:06

标签: android android-listview

我想获取列表视图内容的所有记录。我使用了row.setonclicklistner,但前3-4项工作正常,下一项显示空指针异常。我该怎么办? 我已使用(*)。

指出该特定行
package com.example.tex.list;
public class tex extends Activity {

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

    setContentView(R.layout.activity_xutex);
    ListView lt=(ListView)findViewById(R.id.listView1);

    Databasequery db =  new Databasequery(this);
    db.open();
    List<Propertiess>test= db.getAllcards();
    lt.setAdapter(new myadatper(getApplicationContext(),R.layout.listlayoutdesign, test));


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_xutex, menu);
    return true;
}

 public class myadatper extends ArrayAdapter<Propertiess>
{
List<Propertiess> items;

LayoutInflater mInflater ; 

Context context;

int layoutResourceId; 
Propertiess p;

public myadatper(Context context, int layoutResourceId,
          List<Propertiess>items)
        {
       super(context, layoutResourceId, items);

        this.layoutResourceId=layoutResourceId;

        this.items = items;

        this.context=context;
         // TODO Auto-generated constructor stub
        }


        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
         // TODO Auto-generated method stub
         //return super.getView(position, convertView, parent);

         View row = convertView;
         ViewHolder holder = new ViewHolder();

         if(row==null)
         {


          LayoutInflater inflater=getLayoutInflater();
          row=inflater.inflate(R.layout.listlayoutdesign, parent, false);        
          holder.name = (TextView) row.findViewById(R.id.TextView01);
          holder.address = (TextView) row.findViewById(R.id.TextView02);
          holder.designation=(TextView)row.findViewById(R.id.TextView03);
          holder.contactno=(TextView)row.findViewById(R.id.textView1);
          holder.Emailaddress=(TextView)row.findViewById(R.id.TextView04);
          holder.TapToadd=(ImageView)row.findViewById(R.id.imageView1);

          row.setTag(holder);

    row.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {       
                // TODO Auto-generated method stub

                ListView l3t=(ListView)findViewById(R.id.listView1);
                View view = l3t.getChildAt(position);
        Exception Point      ******************TextView t = (TextView)view.findViewById(R.id.TextView01);******************
                  TextView t2 = (TextView)view.findViewById(R.id.TextView02);
                 TextView t3=(TextView)view.findViewById(R.id.TextView03);
                 String data=t.getText().toString();
                Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();

            }
        });



         }


         else
         {

                holder=(ViewHolder)row.getTag();


         }

                String name=items.get(position).getCompanyname();
                String contactno=items.get(position).getCompanycontactno();
                String address=items.get(position).getCompanyaddress();
                String designation=items.get(position).getCompanyempdesignation();
                String Emailaddress=items.get(position).getCompanyemailaddress();
                holder.name.setText(name);
                holder.contactno.setText(contactno);
                holder.designation.setText(designation);
                holder.address.setText(address);
                holder.Emailaddress.setText(Emailaddress);

                int Datacount=items.size();
                if(position==Datacount-1)
                {
                Toast.makeText(getApplicationContext(), Datacount+"", Toast.LENGTH_SHORT).show();
                holder.TapToadd.setBackgroundResource(R.drawable.taptoadd);
                }

                Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT).show();
                return row;

}

                  public class ViewHolder
                  {

                      TextView name;
                      TextView address;
                      TextView designation;
                      TextView contactno;
                      TextView Emailaddress;
                      ImageView TapToadd;
                      Object obj;


                  }
      }
 }

1 个答案:

答案 0 :(得分:1)

如果要从ListView中提取数据,则不得使用行的视图。这是因为在构建(和显示)ListView的行时,视图会被回收。

您在适配器类中,应该处理ListView背后的数据,所以请使用它!

你应该替换你的

ListView l3t=(ListView)findViewById(R.id.listView1);
View view = l3t.getChildAt(position);
TextView t = (TextView)view.findViewById(R.id.TextView01);
TextView t2 = (TextView)view.findViewById(R.id.TextView02);
TextView t3=(TextView)view.findViewById(R.id.TextView03);
String data=t.getText().toString();

更简单:

String data = items.get(position).getCompanyname();