无法创建列表视图

时间:2014-04-23 05:49:47

标签: android listview android-listview

我想在点击按钮上创建列表视图。当从活动中单击按钮时,它应该打开列表视图。无法单击列表视图。它只是用于显示数据。目前我无法这样做 代码如下:

public class Approve_Stud extends Activity {
    Button b1,b2,b3;
    int l;
        String name,sem,reason,from,to,branch,shift,phone_no;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.approve_stud);
            Bundle bun=getIntent().getExtras();
            phone_no=bun.getString("phone_no");
            name=bun.getString("name");
            sem=bun.getString("sem");
            reason=bun.getString("reason");
            from=bun.getString("from");
            to=bun.getString("to");
            branch=bun.getString("branch");
            shift=bun.getString("shift");
            TextView tv2,tv4,tv8,tv12,tv14,tv6,tv10;
            tv2=(TextView) findViewById(R.id.textView2);
            tv4=(TextView) findViewById(R.id.textView4);
            tv8=(TextView) findViewById(R.id.textView8);
            tv12=(TextView) findViewById(R.id.textView12);
            tv14=(TextView) findViewById(R.id.textView14);
            tv6=(TextView) findViewById(R.id.textView6);
            tv10=(TextView) findViewById(R.id.textView10);
            b1=(Button) findViewById(R.id.button1);
            b2=(Button) findViewById(R.id.button2);
            b3=(Button) findViewById(R.id.button3);
            tv2.setText(name);
            tv4.setText(sem);
            tv8.setText(reason);
            tv12.setText(from);
            tv14.setText(to);
            tv6.setText(branch);
            tv10.setText(shift);
            b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                     AsyncTask<String,Void,String>ap=new Approve(Approve_Stud.this).execute(phone_no,sem,from,to,"1");
                       try{
                           String result=ap.get();
                             Toast.makeText(getBaseContext(),result, Toast.LENGTH_LONG).show();                
                                       }catch(Exception e)
                       {

                       }
                }
            });
    b2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                     AsyncTask<String,Void,String>ap=new Approve(Approve_Stud.this).execute(phone_no,sem,from,to,"0");
                       try{
                           String result=ap.get();
                             Toast.makeText(getBaseContext(),result, Toast.LENGTH_LONG).show();                
                                       }catch(Exception e)
                       {

                       }
                }
            });

    b3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String status[]=new String[20];
            String sem[]=new String[20];
            String from_date[]=new String[20];
            String to_date[]=new String[20];
             AsyncTask<String,Void,String>id1=new CountRows1(Approve_Stud.this).execute(phone_no);
               try{
                String   result2= id1.get();
                    l=Integer.parseInt(result2);
               }catch(Exception e)
               {

               }
            AsyncTask<String,Void,String>id=new History(Approve_Stud.this).execute(phone_no);
             try{ String result1= id.get();
                int j=0;
                for(int i=0;i<l;i++)
                {
            String div[]=result1.split("-");

             sem[i]=div[j++];
             from_date[i]=div[j++];
             to_date[i]=div[j++];
             status[i]=div[j++];
                }
             }catch(Exception e)
             {

             }
              Intent i=new Intent(Approve_Stud.this,ViewHistory.class);

                 Bundle bundle =new Bundle();

                   bundle.putStringArray("sem",sem);
                   bundle.putStringArray("from_date",from_date);
                   bundle.putStringArray("to_date",to_date);
                   bundle.putStringArray("status", status);
                    bundle.putInt("length", l);
                    i.putExtras(bundle);
                    startActivity(i);
        }
    });
   }
}

// ViewHistory.java

public class ViewHistory extends Activity {
    String[] status,sem,fdate,tdate;
    int len;
    ArrayList<String> history;
    ListView lv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.history);
        lv2=(ListView) findViewById(R.id.listView2);
        Bundle bundle = getIntent().getExtras();

     sem=bundle.getStringArray("sem");
     fdate=bundle.getStringArray("from_date");
    tdate= bundle.getStringArray("to_date");

        status= bundle.getStringArray("status");
         len=bundle.getInt("length");
        for(int i=0;i<len;i++)
        {
            history.add(sem[i]+" "+fdate[i]+" "+tdate[i]+" "+status[i]);
        }

        ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,history);
        lv2.setAdapter(ad);
    }
}

2 个答案:

答案 0 :(得分:2)

setContentView(R.layout.yourlayout)中缺少ViewHistory。您的应用应该崩溃,因为您没有将布局设置为活动并初始化ListView

ListView lv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout); // missing
lv2=(ListView) findViewById(R.id.listView2);

ArrayList<String> history;未初始化

答案 1 :(得分:0)

首先,您尚未在ViewHistory课程中设置布局,因此首先使用setContentView(R.layout.yourlayoutfile);

将布局设置如下

第二件事是您尚未实施ListView's onItemClick侦听器以允许其点击事件,因此您已实施ListView's onItemClickListener

第三,确保您已在ArrayList<String> history;

中初始化onCreate()

更改您的代码如下:

ListView lv2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
      setContentView(R.layout.yourlayout); // set your layout first
     lv2=(ListView) findViewById(R.id.listView2);

      history=new ArrayList<String>(); //initiliaze arraylist

     //implement your listview's item click listener.
     lv2.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

        }
    });