2014-04-23 53 views
-2

我想创建一个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); 
    } 
} 
+0

Java类不应该有他们的名字下划线和shoudl遵循驼峰 – JoxTraex

回答

2

失踪ViewHistorysetContentView(R.layout.yourlayout)。您的应用程序应该崩溃,因为你还没有设置布局的活动,初始化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;未初始化

+0

难道这一点,但仍不工作 – user3523322

+0

@ user3523322那是一个错误。你的应用崩溃了吗? – Raghunandan

+0

这里是logcat的一部分04-23 11:27:49.560:E/AndroidRuntime(3353):在writeCrashedAppName中,pkgName:com.example.eleave – user3523322

0

的第一件事是,你有没有在你的ViewHistory类设置你的布局,使第一所有设置你的布局如下使用setContentView(R.layout.yourlayoutfile);

第二件事是,你还没有实现你的ListView's onItemClick监听器允许其click事件让你有实现你ListView'sonItemClickListener

三要确保你有初始化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 

     } 
    });