我用清单视图在android中做了一个简单的应用程序,在选择项目时我想举杯,我尝试了如下 *** 作,但是不起作用.
我的代码如下:
main.java
package com.example.ListvIEw;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import androID.os.Bundle;import androID.app.Activity;import androID.app.ListActivity;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;import androID.Widget.Toast;import androID.support.v4.app.NavUtils;public class ListVIEwActivity extends Activity { String items[]={"Car","Bird","Bike","Flower"}; String category[]={"Sports","Birds","Sports","Nature"}; int icons[]={R.drawable.car,R.drawable.bird,R.drawable.bike,R.drawable.flower}; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_List_vIEw); List <HashMap<String,String>> aList=new ArrayList<HashMap<String,String>>(); for(int i=0;i<4;i++) { HashMap<String,String> hm=new HashMap<String, String>(); hm.put("txt","Item : "+items[i]); hm.put("category","category : "+category[i]); hm.put("icon",Integer.toString(icons[i])); aList.add(hm); } String []from={"icon","txt","category"}; int []to={R.ID.image,R.ID.text,R.ID.category}; SimpleAdapter adapter=new SimpleAdapter(getBaseContext(),aList,R.layout.List,from,to); final ListVIEw lv=(ListVIEw)findVIEwByID(R.ID.ListVIEw); lv.setAdapter(adapter); lv.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> arg0, VIEw arg1, int position, long arg3) { // Todo auto-generated method stub String selectedValue =(String) (lv.getItemAtposition(position)); Toast.makeText(getApplicationContext(),selectedValue , Toast.LENGTH_LONG).show(); } }); }}
请提前帮助我..谢谢
解决方法:
HashMap<String, String> selectedValue = (HashMap<String, String>) (lv.getItemAtposition(position)); ArrayList<String> List = new ArrayList<String>(selectedValue.keySet()); Toast.makeText(getApplicationContext(), selectedValue.get("txt"), Toast.LENGTH_LONG).show();
该哈希图具有存在于该列表中的密钥.该列表实际上是您提供的from数组.只需提供相应的键即可显示相应的文本.
它的工作. 总结
以上是内存溢出为你收集整理的android-吐司不从列表中的选定项目生成文本全部内容,希望文章能够帮你解决android-吐司不从列表中的选定项目生成文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)