2016-06-28 7 views
1

ich eine App erschaffe, wo Sie eine Liste von Hotels angezeigt wird, werden alle Daten von kommenden MySQLJSON und PHP Ich habe die benutzerdefinierte list view erstellt, indem ich den Basisadapter auf einen benutzerdefinierten erweitert habe, aber ich bin nicht in der Lage, OnItemClickListener für listview zu implementieren, da ich den Hotelnamen dieser Zeile in Toast anzeigen möchte, wenn der Benutzer auf eine Zeile klickt Listenansicht. Ich habe verschiedene Beispiele im Internet getestet, aber ich arbeite einfach nicht.Wie eine OnItemClickListener für benutzerdefinierte Listenansicht erstellen, wenn Daten von PHP vorbei json mit

Adapter

public class CustomListAdapterHotel extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<WorldsBillionaires> billionairesItems; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 


    public CustomListAdapterHotel(Activity activity, List<WorldsBillionaires> billionairesItems) { 
     this.activity = activity; 
     this.billionairesItems = billionairesItems; 
    } 


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

    @Override 
    public Object getItem(int location) { 
     return billionairesItems.get(location); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_hotel, null); 

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 

     //NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail); 
     TextView hotel_name = (TextView) convertView.findViewById(R.id.hotel_name); 
     TextView zone = (TextView) convertView.findViewById(R.id.zone); 
     TextView contact_person = (TextView) convertView.findViewById(R.id.contact_person); 
     TextView contact_number = (TextView) convertView.findViewById(R.id.contact_number); 
     TextView btc_direct = (TextView) convertView.findViewById(R.id.btcdirect); 

     // getting billionaires data for the row 
     WorldsBillionaires m = billionairesItems.get(position); 


     // name 
     hotel_name.setText(String.valueOf(m.getHotel_Name())); 

     zone.setText(String.valueOf(m.getHotel_Zone())); 

     contact_person.setText(String.valueOf(m.getContact_Person())); 

     contact_number.setText(String.valueOf(m.getContact_Number())); 

     btc_direct.setText(String.valueOf(m.getBtc_Direct())); 

     return convertView; 
    } 

} 

Modell

public class WorldsBillionaires { 

    private String hotel_name,hotel_zone,contact_person,contact_number,btc_direct; 

    public WorldsBillionaires(String hotel_name, String hotel_zone, String contact_person, String contact_number, String btc_direct) { 
     this.hotel_name=hotel_name; 
     this.hotel_zone=hotel_zone; 
     this.contact_person=contact_person; 
     this.contact_number=contact_number; 
     this.btc_direct=btc_direct; 
    } 
    public WorldsBillionaires() { 
    } 
    public String getZone() { 
     return zone; 
    } 

    public void setZone(String zone) { 
     this.zone = zone; 
    } 

    public String getThumbnailUrl() { 
     return thumbnailUrl; 
    } 

    public void setThumbnailUrl(String thumbnailUrl) { 
     this.thumbnailUrl = thumbnailUrl; 
    } 
    public String getHotel_Name() { 
     return hotel_name; 
    } 

    public void setHotel_Name(String hotel_name) { 
     this.hotel_name = hotel_name; 
    } 

    public String getHotel_Zone() { 
     return hotel_zone; 
    } 

    public void setHotel_Zone(String hotel_zone) { 
     this.hotel_zone = hotel_zone; 
    } 

    public String getContact_Person() { 
     return contact_person; 
    } 

    public void setContact_Person(String contact_person) { 
     this.contact_person = contact_person; 
    } 

    public String getContact_Number() { 
     return contact_number; 
    } 

    public void setContact_Number(String contact_number) { 
     this.contact_number = contact_number; 
    } 

    public String getBtc_Direct() { 
     return btc_direct; 
    } 

    public void setBtc_Direct(String btc_direct) { 
     this.btc_direct = btc_direct; 
    } 
} 

Hauptaktivität

public class ShowHotel extends AppCompatActivity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    // Billionaires json url 
    private ProgressDialog pDialog; 
    private List<WorldsBillionaires> worldsBillionairesList = new ArrayList<WorldsBillionaires>(); 
    private ListView listView; 
    private CustomListAdapterHotel adapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_hotel); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setLogo(R.mipmap.ic_launcher); 
     getSupportActionBar().setDisplayUseLogoEnabled(true); 
     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapterHotel(this, worldsBillionairesList); 
     listView.setAdapter(adapter); 
     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 


     // Creating volley request obj 
     JsonArrayRequest billionaireReq = new JsonArrayRequest("http://192.168.247.1/AdminBihar/getHotel.php?zone="+methods.zone, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           WorldsBillionaires worldsBillionaires = new WorldsBillionaires(); 
           worldsBillionaires.setHotel_Name(obj.getString("hotel_name")); 
           worldsBillionaires.setThumbnailUrl(obj.getString("image")); 
           worldsBillionaires.setHotel_Zone(obj.getString("zone")); 
           worldsBillionaires.setContact_Person(obj.getString("contact_person")); 
           worldsBillionaires.setContact_Number(obj.getString("contact_number")); 
           worldsBillionaires.setBtc_Direct(obj.getString("btc_direct")); 

           // adding Billionaire to worldsBillionaires array 
           worldsBillionairesList.add(worldsBillionaires); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 
      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(billionaireReq); 


    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     hidePDialog(); 
    } 

    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 
} 

Antwort

0

so, nachdem Sie json Daten, in Tätigkeit erhalten, die zeigt Ihre Liste so etwas tun:

public class DisplayListView extends AppCompatActivity { 
    ListView listView; 

protected void onCreate(){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_display_list_view); 

    listView = (ListView) findViewById(R.id.listview); 
    hotelAdapter = new CustomListAdapterHotel (this, R.layout.row_layout); 
    listView.setAdapter(hotelAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 

      Model stuff = (Model) hotelAdapter.getItem(position); 
      String hotelName = stuff.getHotel_Name(); 

      Toast.makeText(getApplicationContext(), hotelName, Toast.LENGTH_SHORT).show(); 


     } 
    }); 

da funktionierte das für mich :)

Verwandte Themen