String name= (String) lv.getItemAtPosition(position);
”
在方法yes();
我也试过用
“
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 :(得分:0)
问题出在getItem
的{{1}}方法:
CategoryListAdapter
这是返回int的位置。你应该用
替换它@Override
public Object getItem(int position) {
return position;
}
进行此更改后,代码
@Override
public Object getItem(int position) {
return urls.get(position);
}
应该可以正常工作。