2013-10-17 66 views
0
I have this code: 


     public class MainActivity extends Activity { 
      // Declare Variables 
      JSONObject jsonobject; 
      JSONArray jsonarray; 
      ListView listview; 
      ListViewAdapter adapter; 
      ProgressDialog mProgressDialog; 
      ArrayList<HashMap<String, String>> arraylist; 
      static String NAME = "rank"; 
      static String TYPE = "country"; 
      static String FLAG = "flag"; 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       // Get the view from listview_main.xml 
       setContentView(R.layout.activity_main); 
       // Execute DownloadJSON AsyncTask 
       new DownloadJSON().execute(); 
      } 

      // DownloadJSON AsyncTask 
      private class DownloadJSON extends AsyncTask<Void, Void, Void> { 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        // Create a progressdialog 
        mProgressDialog = new ProgressDialog(MainActivity.this); 
        // Set progressdialog title 
        mProgressDialog.setTitle("Android JSON Parse Tutorial"); 
        // Set progressdialog message 
        mProgressDialog.setMessage("Loading..."); 
        mProgressDialog.setIndeterminate(false); 
        // Show progressdialog 
        mProgressDialog.show(); 
       } 
       @Override 
       protected Void doInBackground(Void... params) { 
        // Create an array 
        arraylist = new ArrayList<HashMap<String, String>>(); 
        // Retrieve JSON Objects from the given URL address 
        jsonobject = JSONParser 
          .getJSONfromURL("http://54.218.73.244:7002/"); 

        try { 
         // Locate the array name in JSON 
         jsonarray = jsonobject.getJSONArray("restaurants"); 

         for (int i = 0; i < jsonarray.length(); i++) { 
          HashMap<String, String> map = new HashMap<String, String>(); 
          jsonobject = jsonarray.getJSONObject(i); 
          // Retrive JSON Objects 
          map.put(MainActivity.NAME, jsonobject.getString("restaurantNAME")); 
     map.put(MainActivity.TYPE, jsonobject.getString("restaurantTYPE")); 
     //map.put(MainActivity.FLAG, jsonobject.getString("restaurantIMAGE")); 
          // Set the JSON Objects into the array 
          arraylist.add(map); 
         } 
        } catch (JSONException e) { 
         Log.e("Error", e.getMessage()); 
         e.printStackTrace(); 
        } 
        return null; 
       } 

       @Override 
       protected void onPostExecute(Void args) { 
        // Locate the listview in listview_main.xml 
        listview = (ListView) findViewById(R.id.listview); 
        // Pass the results into ListViewAdapter.java 
        adapter = new ListViewAdapter(MainActivity.this, arraylist); 
        // Set the adapter to the ListView 
        listview.setAdapter(adapter); 
        // Close the progressdialog 
        mProgressDialog.dismiss(); 
       } 
      } 
     } 

ListViewAdapter.java:ListViewAdapter沒有得到初始化

public abstract class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> data; 
    //ImageLoader imageLoader; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    public ListViewAdapter(Context context, 
      ArrayList<HashMap<String, String>> arraylist) { 
     this.context = context; 
     data = arraylist; 
     // imageLoader = new ImageLoader(context); 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // Declare Variables 
     TextView rank; 
     TextView country; 
     //ImageView flag; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View itemView = inflater.inflate(R.layout.listview_item, parent, false); 
     // Get the position 
     resultp = data.get(position); 

     // Locate the TextViews in listview_item.xml 
     rank = (TextView) itemView.findViewById(R.id.rank); 
     country = (TextView) itemView.findViewById(R.id.country); 

     // Locate the ImageView in listview_item.xml 
     //flag = (ImageView) itemView.findViewById(R.id.flag); 

     // Capture position and set results to the TextViews 
     rank.setText(resultp.get(MainActivity.NAME)); 
     country.setText(resultp.get(MainActivity.TYPE)); 
     // Capture position and set results to the ImageView 
     // Passes flag images URL into ImageLoader.class 
     // imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag); 
     // Capture ListView item click 
     itemView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // Get the position 
       resultp = data.get(position); 
       Intent intent = new Intent(context, SingleItemView.class); 
       // Pass all data rank 
       intent.putExtra("name", resultp.get(MainActivity.NAME)); 
       // Pass all data country 
       intent.putExtra("type", resultp.get(MainActivity.TYPE)); 
       // Pass all data flag 
       // intent.putExtra("flag", resultp.get(MainActivity.FLAG)); 
       // Start SingleItemView Class 
       context.startActivity(intent); 

      } 
     }); 
     return itemView; 
    } 
} 

And it gives an Error: 

    Cannot instantiate the type ListAdapter 

Why am i getting this error? A little help would be appreciated. 
+0

你能潛在地給出更詳細的堆棧跟蹤? –

+0

我想我已經給它正確 – NOOB

+0

得到在日食logcat和獲得這些行打印爲紅色。每行,然後粘貼在這裏 – Triode

回答

0

我認爲,你應該初始化您在活動的onCreate()列表視圖。也許你可以嘗試一下。

+0

它沒有工作.. – NOOB

0

您listadapter是一個抽象類,這就是爲什麼你[R收到錯誤對該類公共