2013-03-24 165 views
1

我想在android中使用數據庫創建gridview,但我得到「構造函數ArrayAdapter(啓動,int,ArrayList)未定義」錯誤「 Arrayadapter適配器=新Arrayadapter ..」行....我的代碼是下面給出......請幫我解決它.....在此先感謝錯誤:構造函數ArrayAdapter <String>(Startup,int,ArrayList <Contact>)未定義

Startup.java活動文件

public class Startup extends Activity { 
    private GridView gridView; 
    final static ArrayList<Contact> timetable = new ArrayList<Contact>(); 

    String response; 
    WebAPIRequest web = new WebAPIRequest(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.main); 
    setContentView(R.layout.main); 

    String url = "http://192.168.0.101/attendance/webservice/gettingTodaysLectures.php"; 
    new setd().execute(url); 
} 

public void displayTimeTable(String response) { 

    String getAllData[] = response.split("<br>"); 

    Contact displaylec; 
    for (int i = 0; i < getAllData.length - 1; i++) { 
     String tempdata[] = getAllData[i].split(":"); 
     // tmepdata[0]=start time // tempdata[1]=end time // 
     // tempdata[2]=semester 
     // tempdata[3]=department // tempdata[4]=division // 
     // tempdata[5]=subject 
     // tempdata[6]=type // tempdata[7]=batch // tempdata[8]=classno 
     displaylec = new Contact(tempdata[0], tempdata[1], tempdata[2], 
       tempdata[3], tempdata[4], tempdata[5], tempdata[6], 
       tempdata[7], tempdata[8]); 

     timetable.add(displaylec); 
     Log.i("All sem id", tempdata[0] + ":" + tempdata[1] + ":" 
       + tempdata[2] + ":" + tempdata[3] + ":" + tempdata[4] + ":" 
       + tempdata[5] + ":" + tempdata[6] + ":" + tempdata[7] + ":" 
       + tempdata[8]); 
     Toast.makeText(getApplicationContext(), tempdata[0], 1); 
    } 

    gridView = (GridView) findViewById(R.id.gridView1); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, timetable); 

    gridView.setAdapter(adapter); 

    gridView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
      Toast.makeText(getApplicationContext(), 
        ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 

public class setd extends AsyncTask<String, Void, Void> { 
    ProgressDialog pd; 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     // pd=new ProgressDialog(getApplicationContext()); 
     // pd.setTitle("Loading ....."); 
     // pd.setMessage("Inserting data"); 
     Log.i("ddd", "dd"); 
     pd = ProgressDialog.show(Startup.this, "Loading .....", 
       "getting data"); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     response = web.performGet(params[0]); 
     Log.i("response", response); 
     Log.i("response Display Extraactivity", response); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     pd.cancel(); 
     // Main Logic Write here 
     Log.i("RESPONSE : ", response.toString()); 
     displayTimeTable(response.toString()); 
    } 
} 
+0

嘗試在初始化 – 2013-03-24 10:05:47

+0

傳球,而不是這個情境變量有沒有人幫你嗎? – Korcholis 2013-03-25 09:29:33

回答

0

問題是,您正在嘗試將Contact對象的列表傳遞給期望String列表的構造函數。 參見Android文檔在這裏:generic ArrayList constructor

你可以解決這個醚:

  • 創建自己的自定義適配器,需要一個聯繫對象,並把它傳遞給意見,這裏是一個很好的教程爲:Custom ArrayAdapter
  • 您可以將您的Contact對象轉換爲String equivelent並將新生成的列表傳遞給ArrayAdapter,代碼應該如下所示。

    List<String> stringList = new ArrayList<String>(); 
    for(Contact object : timetable){ 
        stringList.put("new object " + object.toString()); 
    } 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, stringList); 
    
+0

謝謝......它幫助我解決了錯誤。 – user2203072 2015-06-04 12:26:52

0

您應該使用ArrayAdapter<Contact>(Startup, int, ArrayList<Contact>)來製作ArrayAdapter,因爲ArrayAdapter包含wh在內部ArrayList

相關問題