Android从listView onClick获取正确信息
因此,我有一个从web服务的xml文件填充的ListView。列表填充得很好。嗯,它使用了一个不好的方法来填充,我知道它应该使用AsyncTask,但是它在UI线程上填充。我试了又试,但我输了!我知道我知道!!如果你有解决这个问题的办法,我很想听!! 无论如何,当单击列表项时,我会启动一个新活动,并使用intent.putExtra传递信息 这是可行的,但它总是显示列表中第一项的信息。 例如: A B C D E 是列表中的项目。按下其中任何一个按钮,我将获得以下活动中与A相关的所有信息 我该如何解决这个问题 ListView活动:Android从listView onClick获取正确信息,android,listview,onitemclick,Android,Listview,Onitemclick,因此,我有一个从web服务的xml文件填充的ListView。列表填充得很好。嗯,它使用了一个不好的方法来填充,我知道它应该使用AsyncTask,但是它在UI线程上填充。我试了又试,但我输了!我知道我知道!!如果你有解决这个问题的办法,我很想听!! 无论如何,当单击列表项时,我会启动一个新活动,并使用intent.putExtra传递信息 这是可行的,但它总是显示列表中第一项的信息。 例如: A B C D E 是列表中的项目。按下其中任何一个按钮,我将获得以下活动中与A相关的所有信息 我该如
public class ColorPacksMain extends Activity {
// All static variables
static final String URL = "https://dl.dropbox.com/u/43058382/BeanPickerColorToolUpdates/ColorPacksList.xml";
// XML node keys
static final String KEY_COLORPACK = "ColorPack"; // parent node
static final String KEY_ID = "id";
static final String KEY_USER = "user";
static final String KEY_THEME = "theme";
static final String KEY_THEMECOLOR = "themeColor";
static final String KEY_TEXTCOLORPRIMARY = "primaryTextColor";
static final String KEY_TEXTCOLORSECONDARY = "secondaryTextColor";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_DL_URL_GSM = "dl_url_gsm";
static final String KEY_DL_URL_LTE = "dl_url_lte";
static final String KEY_SCREEN1 = "screen1";
static final String KEY_SCREEN2 = "screen2";
static final String KEY_SCREEN3 = "screen3";
static final String KEY_SCREEN4 = "screen4";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color_packs_activity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ArrayList<HashMap<String, String>> colorPacksList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_COLORPACK);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_USER, parser.getValue(e, KEY_USER));
map.put(KEY_THEME, parser.getValue(e, KEY_THEME));
map.put(KEY_THEMECOLOR, parser.getValue(e, KEY_THEMECOLOR));
map.put(KEY_TEXTCOLORPRIMARY, parser.getValue(e, KEY_TEXTCOLORPRIMARY));
map.put(KEY_TEXTCOLORSECONDARY, parser.getValue(e, KEY_TEXTCOLORSECONDARY));
map.put(KEY_DL_URL_GSM, parser.getValue(e, KEY_DL_URL_GSM));
map.put(KEY_DL_URL_LTE, parser.getValue(e, KEY_DL_URL_LTE));
map.put(KEY_SCREEN1, parser.getValue(e, KEY_SCREEN1));
map.put(KEY_SCREEN2, parser.getValue(e, KEY_SCREEN2));
map.put(KEY_SCREEN3, parser.getValue(e, KEY_SCREEN3));
map.put(KEY_SCREEN4, parser.getValue(e, KEY_SCREEN4));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
colorPacksList.add(map);
}
list = (ListView) findViewById(R.id.colorPacksList);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, colorPacksList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ListClickedItem.class);
// sending data to new activity
String user = ((TextView) findViewById(R.id.user)).getText().toString();
String theme = ((TextView) findViewById(R.id.theme)).getText().toString();
String themeColor = ((TextView) findViewById(R.id.themeColor)).getText().toString();
String tcP = ((TextView) findViewById(R.id.primaryTextColor)).getText().toString();
String tcS = ((TextView) findViewById(R.id.secondaryTextColor)).getText().toString();
String dlGSM = ((TextView) findViewById(R.id.GSMurl)).getText().toString();
String dlLTE = ((TextView) findViewById(R.id.LTEurl)).getText().toString();
String screen1 = ((TextView) findViewById(R.id.screen1url)).getText().toString();
String screen2 = ((TextView) findViewById(R.id.screen2url)).getText().toString();
String screen3 = ((TextView) findViewById(R.id.screen3url)).getText().toString();
String screen4 = ((TextView) findViewById(R.id.screen4url)).getText().toString();
i.putExtra("UserName", user);
i.putExtra("ThemeName", theme);
i.putExtra("ThemeColor", themeColor);
i.putExtra("TextColorPrimary", tcP);
i.putExtra("TextColorSecondary", tcS);
i.putExtra("GSM", dlGSM);
i.putExtra("LTE", dlLTE);
i.putExtra("Screenshot 1", screen1);
i.putExtra("Screenshot 2", screen2);
i.putExtra("Screenshot 3", screen3);
i.putExtra("Screenshot 4", screen4);
startActivity(i);
}
});
}
还有,我的适配器类,因为我知道它将被要求:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView user = (TextView)vi.findViewById(R.id.user); // username
TextView theme = (TextView)vi.findViewById(R.id.theme); // theme name
TextView themeColor = (TextView)vi.findViewById(R.id.themeColor); // theme color
TextView primaryTextColor = (TextView)vi.findViewById(R.id.primaryTextColor); // main text color
TextView secondaryTextColor = (TextView)vi.findViewById(R.id.secondaryTextColor); // secondary text color
TextView gsmURL = (TextView)vi.findViewById(R.id.GSMurl); // Maguro URL
TextView lteURL = (TextView)vi.findViewById(R.id.LTEurl); // Toro/ToroPlus URL
TextView screenShot1 = (TextView)vi.findViewById(R.id.screen1url); // Screenshot url
TextView screenShot2 = (TextView)vi.findViewById(R.id.screen2url); // Screenshot url
TextView screenShot3 = (TextView)vi.findViewById(R.id.screen3url); // Screenshot url
TextView screenShot4 = (TextView)vi.findViewById(R.id.screen4url); // Screenshot url
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> colorPack = new HashMap<String, String>();
colorPack = data.get(position);
// Setting all values in listview
user.setText(colorPack.get(ColorPacksMain.KEY_USER));
theme.setText(colorPack.get(ColorPacksMain.KEY_THEME));
themeColor.setText(colorPack.get(ColorPacksMain.KEY_THEMECOLOR));
primaryTextColor.setText(colorPack.get(ColorPacksMain.KEY_TEXTCOLORPRIMARY));
secondaryTextColor.setText(colorPack.get(ColorPacksMain.KEY_TEXTCOLORSECONDARY));
gsmURL.setText(colorPack.get(ColorPacksMain.KEY_DL_URL_GSM));
lteURL.setText(colorPack.get(ColorPacksMain.KEY_DL_URL_LTE));
screenShot1.setText(colorPack.get(ColorPacksMain.KEY_SCREEN1));
screenShot2.setText(colorPack.get(ColorPacksMain.KEY_SCREEN2));
screenShot3.setText(colorPack.get(ColorPacksMain.KEY_SCREEN3));
screenShot4.setText(colorPack.get(ColorPacksMain.KEY_SCREEN4));
imageLoader.DisplayImage(colorPack.get(ColorPacksMain.KEY_THUMB_URL), thumb_image);
return vi;
}
公共类LazyAdapter扩展了BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共图像加载器;
公共LazyAdapter(活动a,ArrayList d){
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
imageLoader=新的imageLoader(activity.getApplicationContext());
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.list_行,空);
TextView用户=(TextView)vi.findviewbyd(R.id.user);//用户名
TextView主题=(TextView)vi.findviewbyd(R.id.theme);//主题名称
TextView主题颜色=(TextView)vi.findViewById(R.id.themeColor);//主题颜色
TextView primaryTextColor=(TextView)vi.findViewById(R.id.primaryTextColor);//主文本颜色
TextView secondaryTextColor=(TextView)vi.findviewbyd(R.id.secondaryTextColor);//辅助文本颜色
TextView gsmURL=(TextView)vi.findviewbyd(R.id.gsmURL);//Maguro URL
TextView lteURL=(TextView)vi.findViewById(R.id.lteURL);//Toro/ToroPlus URL
TextView屏幕快照1=(TextView)vi.findViewById(R.id.screen1url);//屏幕快照url
TextView屏幕快照2=(TextView)vi.findViewById(R.id.screen2url);//屏幕快照url
TextView屏幕快照3=(TextView)vi.findViewById(R.id.screen3url);//屏幕快照url
TextView屏幕快照4=(TextView)vi.findViewById(R.id.screen4url);//屏幕快照url
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);//thumb image
HashMap colorPack=新的HashMap();
colorPack=data.get(位置);
//在listview中设置所有值
user.setText(colorPack.get(ColorPacksMain.KEY_user));
setText(colorPack.get(ColorPacksMain.KEY_-theme));
themeColor.setText(colorPack.get(ColorPacksMain.KEY_themeColor));
primaryTextColor.setText(colorPack.get(ColorPacksMain.KEY_TEXTCOLORPRIMARY));
secondaryTextColor.setText(colorPack.get(ColorPacksMain.KEY_TEXTCOLORSECONDARY));
gsmURL.setText(colorPack.get(ColorPacksMain.KEY_DL_URL_GSM));
setText(colorPack.get(ColorPacksMain.KEY_DL_URL_LTE));
screenShot1.setText(colorPack.get(ColorPacksMain.KEY_SCREEN1));
screenShot2.setText(colorPack.get(ColorPacksMain.KEY_SCREEN2));
screenShot3.setText(colorPack.get(ColorPacksMain.KEY_SCREEN3));
screenShot4.setText(colorPack.get(ColorPacksMain.KEY_SCREEN4));
imageLoader.DisplayImage(colorPack.get(ColorPacksMain.KEY\u THUMB\u URL)、THUMB\u image);
返回vi;
}
非常感谢您的帮助!您正在通过
i.putExtra(“用户名”,user);
向intent填充数据,但您是通过extras.getString(“用户名”);
相反,您可以在列表ClickEdItem
活动中获得如下值
String user = getIntent().getStringExtra("UserName");
getExtras()最有可能在您使用Bundle填充数据并在其他活动中获取数据时使用。希望这对您有所帮助
注意:除了上面的更正之外,正如您所说,您知道应该使用asynctask,我可以向您建议另一件事——当您使用asynctask时,在
doInBackground()
方法中从web服务获取所需数据后,您应该在onPostExecute()中将数据填充到listview中
方法。您可以使用runOnUIThread()
来填充onPostExecute()方法中的数据,甚至可以将onClickListener
设置为此方法本身中的listview。您正在通过i.putExtra(“用户名”,用户)来填充数据
但是你是通过像extras.getString(“用户名”);
相反,您可以在列表ClickEdItem
活动中获得如下值
String user = getIntent().getStringExtra("UserName");
getExtras()最有可能在您使用Bundle填充数据并在其他活动中获取数据时使用。希望这对您有所帮助
注意:除了上面的更正之外,正如您所说,您知道应该使用asynctask,我可以向您建议另一件事——当您使用asynctask时,在
doInBackground()
方法中从web服务获取所需数据后,您应该在onPostExecute()中将数据填充到listview中
方法。您可以使用runOnUIThread()
来填充onPostExecute()方法中的数据,甚至可以将onClickListener
设置为此方法本身中的listview。在everyline中写入view.findViewById。如下所示
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), ListClickedItem.class);
// sending data to new activity
String user = ((TextView) view.findViewById(R.id.user)).getText().toString();
String theme = ((TextView) view.findViewById(R.id.theme)).getText().toString();
startActivity(i);
}
public void onItemClick(AdapterView arg0,视图,int arg2,
长arg3){
//在选择单个列表项时启动新活动
Intent i=新Intent(getApplicationContext(),ListClickedItem.class);
//将数据发送到n
use an AsyncTask but instead it populates on the UI thread. I tried and tried with an AsyncTask, but I lost the fight!
public class Home extends Activity {
/** Called when the activity is first created. */
static final String URL = "https://dl.dropbox.com/u/43058382/BeanPickerColorToolUpdates/ColorPacksList.xml";
static final String KEY_ITEM = "Book"; // parent node
static final String KEY_BOOKAUTHOR = "book_author";
static final String KEY_BOOKRATING = "BookRating";
static final String KEY_BOOKID = "BookID";
static final String KEY_BOOKDESC = "BookDescription";
static final String KEY_BOOKDATEPUBLISHED = "DatePublished";
static final String KEY_BOOKTITLE = "BookTitle";
static final String KEY_BOOKCODE = "BookCode";
static ArrayList<String> BookTitle = null;
static ArrayList<Integer> BookRating = null;
static ArrayList<String> BookDescription = null;
static ArrayList<String> BookCoverPhotos = null;
static ArrayList<String> BookAuther = null;
ConnectivityManager cm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// First Check the Internet Connectivity
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
// Avoid to reload the page again and again
if (BookTitle == null) {
BookTitle = new ArrayList<String>();
BookRating = new ArrayList<Integer>();
BookDescription = new ArrayList<String>();
BookIDs = new ArrayList<String>();
BookCode = new ArrayList<String>();
BookCoverPhotos = new ArrayList<String>();
BookAuther = new ArrayList<String>();
// Execute the AsyncTask
new myBackGroundTask().execute(URL);
} else {
ImageAdapter adapter2 = new ImageAdapter(getBaseContext(),
act);
adapter2.notifyDataSetChanged();
gridView.setAdapter(adapter2);
}
private class myAsyncTask extends AsyncTask<String, Integer, String> {
ProgressDialog progressDialog;
ImageAdapter adapter = new ImageAdapter(getBaseContext(), act);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(getParent(),
"Your Title ...",
"This may Take a few seconds.\nPlease Wait...");
}
@Override
protected String doInBackground(String... params) {
String URL = params[0];
XMLParser parser = new XMLParser();
String XMLString = null;
XMLString = parser.getXmlFromUrl_FeaturedBooks(URL, lIndex);
if (XMLString != null) {
Document doc = parser.getDomElement(XMLString);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
try {
BookRating.add(Integer.valueOf(parser.getValue(e,
KEY_BOOKRATING)));
} catch (Exception e2) {
BookRating.add(0);
}
BookDescription.add(parser.getValue(e, KEY_BOOKDESC));
BookTitle.add(parser.getValue(e, KEY_BOOKTITLE));
BookCoverPhotos
.add("http://shiaislamicbooks.com/books_Snaps/"
+ parser.getValue(e, KEY_BOOKCODE)
+ "/1_thumb.jpg");
int tempCount = BookCoverPhotos.size() - 1;
BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR));
publishProgress(i + 1);
}
} else {
// Request Time
publishProgress(5000);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setMessage(values[0]
+ " Book(s) found \nPlease wait...");
adapter.notifyDataSetChanged();
if (values[0] == 5000) {
Toast.makeText(context,
"Rrequest Time out!\nNo or Slow Internet Connection!",
Toast.LENGTH_LONG).show();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
adapter.notifyDataSetChanged();
gridView.setAdapter(adapter);
}
}
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
context = c;
}
// ---returns the number of images---
public int getCount() {
return BookTitle.size();
}
public ImageAdapter(Context ctx, Activity act) {
inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
// ImageView bmImage;
final ViewHolder holder;
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.grid_style, parent, false);
holder = new ViewHolder();
holder.txt_BooksTitle = (TextView) vi
.findViewById(R.id.txt_BookTitle);
holder.img_BookCoverPhoto = (ImageView) vi
.findViewById(R.id.imgBookCover);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.txt_BooksTitle.setText(BookTitle.get(position) + "");
holder.img_BookCoverPhoto.setImageBitmap(bitmapArray.get(position));
return vi;
}
}
class ViewHolder {
TextView txt_BooksTitle;
ImageView img_BookCoverPhoto;
}