1

I want to create gridview in android which uses database but I am getting " The constructor ArrayAdapter(Startup, int, ArrayList) is undefined" error on "Arrayadapter adapter=new Arrayadapter.." line ....my code is as given below...please help me to solve it out.....thanks in advance

Startup.java activity file

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());
    }
}
4

2 回答 2

0

你应该ArrayAdapterArrayAdapter<Contact>(Startup, int, ArrayList<Contact>), 因为ArrayAdapter包含里面的模板ArrayList

于 2013-03-24T10:11:21.047 回答
0

问题是您试图将Contact对象列表传递给期望String列表的构造函数。在此处查看 android 文档:通用 ArrayList 构造函数

你可以通过以下方式解决这个以太:

  • 创建您自己的自定义适配器,该适配器接受一个联系人对象并将其传递给视图,这是一个很好的教程:自定义 ArrayAdapter
  • 您可以将您的Contact对象转换为String等效对象并将新生成的列表传递给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);
    
于 2013-03-24T10:17:26.300 回答