2015-02-09 19 views
-1

我試圖 我有這個問題我試圖獲得在ListView選定位置的價值,我有錯誤,每次我把下面這行代碼 選擇onitemclicklisterner「String name= (String) lv.getItemAtPosition(position);」 的方法是(); 我也使用
試圖 「如何SherlockListFragment

HashMap<String, String> name = 
      (HashMap<String, String>) adapter.getItem(position); 
String jok=name.get("joke"); 

,但仍然沒有工作

這是我的代碼

package com.mall.our; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 

import com.actionbarsherlock.app.SherlockListFragment; 
import com.mall.first.CategoryListAdapter; 
import com.mall.first.JSONParser; 
import com.mall.first.Login; 
import com.mall.first.R; 





public class Friends extends SherlockListFragment { 
    JSONParser jsonParser = new JSONParser(); 
    private static final String TAG_POSTS = "posts"; 
    public static final String TAG_ID = "id"; 
    public static final String TAG_NAME = "category"; 
    public static final String TAG_CATEGORIES_COUNT = "categories_count"; 
    public static final String TAG_CATEGORIES_LOGO = "categories_logo"; 

    private static final String URL_CATEGORY = "http://10.0.2.2/ochat/selectfriends.php"; 

     private BaseAdapter mAdapter; 
     private ListView lv; 
     SharedPreferences sp ; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.friends, container, false); 
     return rootView; 
    } 
    @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      lv = getListView(); 
      lv.setDivider(null); 
      new LoadComments().execute(); 



    } 

     class LoadComments extends AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> { 
      private ProgressDialog pDialog; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(getActivity()); 

       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(true); 

      } 

      @Override 
      protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) { 

        ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>(); 

       SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
       String username = sp.getString("username", "anon"); 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("username", username)); 
        JSONObject json = jsonParser.makeHttpRequest(URL_CATEGORY, "POST", 
          params); 

        try { 

         JSONArray categories = json.getJSONArray(TAG_POSTS); 
         for (int i = 0; i < categories.length(); i++) { 
          String id = categories.getJSONObject(i).getString("TAG_ID"); 
          String name = categories.getJSONObject(i).getString("TAG_NAME"); 
          String songs_count = categories.getJSONObject(i).getString("TAG_CATEGORIES_COUNT"); 
          String category_logo = categories.getJSONObject(i).getString("TAG_CATEGORIES_LOGO"); 

          HashMap<String, String> map = new HashMap<String, String>(); 
          map.put(TAG_ID, id); 
          map.put(TAG_NAME, name); 
          map.put(TAG_CATEGORIES_COUNT, songs_count); 
          map.put(TAG_CATEGORIES_LOGO, category_logo); 

          categoryList.add(map); 
         } 
        }catch (Throwable e){ 
         e.printStackTrace(); 
        } 
        return categoryList; 
       } 

      @Override 
      protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
       super.onPostExecute(result); 

       mAdapter = new CategoryListAdapter(getActivity(),result); 
       lv.setAdapter(mAdapter); 
       yes(); 
      } 

      private void yes() { 
       lv.setOnItemClickListener(new OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View viewClicked, 
          int position, long id) { 
        String yy= (String) lv.getItemAtPosition(position) ;     
           Intent i = new Intent(getActivity(),Chatting.class);  

           startActivity(i); 

        } 

       }); 
      } 
     } 
    } 

這是我的錯誤

02-09 19:12:31.280: E/AndroidRuntime(5915): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String 
    02-09 19:12:31.280: E/AndroidRuntime(5915):  at com.mall.our.Friends$LoadComments$1.onItemClick(Friends.java:128) 
    02-09 19:12:31.280: E/AndroidRuntime(5915):  at android.widget.AdapterView.performItemClick(AdapterView.java:299) 
    02-09 19:12:31.280: E/AndroidRuntime(5915):  at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 
    02-09 19:12:31.280: E/AndroidRuntime(5915):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904) 

的CategoryListAdapter

package com.mall.first; 

import java.util.ArrayList; 

import java.util.HashMap; 
import com.mall.our.Friends; 
import com.squareup.picasso.Picasso; 

import android.annotation.SuppressLint; 
import android.content.Context; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class CategoryListAdapter extends BaseAdapter { 
    private Context mContext; 
    LayoutInflater inflater; 
    private final ArrayList<HashMap<String, String>> urls; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    public static final String CATEGORY_LOGO_URL = "http://10.0.2.2/demi.jpg"; 

    public CategoryListAdapter(Context context, 
      ArrayList<HashMap<String, String>> items) { 
     mContext = context; 
     urls = items; 
    } 

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

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

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

    @SuppressLint("ViewHolder") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView category_name; 
     ImageView category_logo; 
     TextView item_count; 
     TextView item_id; 

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

     View view = inflater.inflate(R.layout.custom_list_category, parent, 
       false); 

     resultp = urls.get(position); 

     category_name = (TextView) view.findViewById(R.id.timee); 
     category_logo = (ImageView) view.findViewById(R.id.img_category_logo); 
     item_count = (TextView) view.findViewById(R.id.songs_count); 
     item_id = (TextView) view.findViewById(R.id.category_id); 

     category_name.setText(resultp.get(Friends.TAG_NAME)); 
     item_count.setText(resultp.get(Friends.TAG_CATEGORIES_COUNT)); 
     item_id.setText(resultp.get(Friends.TAG_ID)); 

     // Picasso image loader library starts here 
     Picasso.with(mContext) 
       .load(resultp.get(Friends.TAG_CATEGORIES_LOGO)) // Photo URL 
       .placeholder(R.drawable.placeholder) // This image will be displayed while photo URL is loading 
       .error(R.drawable.error) // if error shows up during downloading 
       .fit().centerCrop() // settings 
       .into(category_logo); // we put it into our layout component (imageview) 

     return view; 
    } 

} 

回答

0

的問題是在CategoryListAdaptergetItem方法:

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

這是返回位置INT。你應該

@Override 
public Object getItem(int position) { 
    return urls.get(position); 
} 

取代它。一旦你做出這種改變,代碼

HashMap<String, String> name = 
     (HashMap<String, String>) adapter.getItem(position); 
String jok=name.get("joke"); 

應該只是罰款。