0

我正在将 xml 解析为我的项目中的列表视图。我希望当有人单击 listView 中的任何位置时,我想将我在当前位置的所有信息发送到另一个活动。我是android新手,请帮我解决这个问题。我提供下面的代码。我在 ListView 中显示 KEY_NAME 、 KEY_DESCRIPTION 、KEY_ID 、 KEY_OFFLINE 和 KRY_THUMB_URL 。我想将当前位置的所有这些信息发送到另一个活动,我想在该活动中显示有关列表中当前选定项目的完整详细信息。

这是我的新修改代码.. 但我在此代码中收到错误.. 应用程序强制关闭。

public class MainActivity extends Activity {
// All static variables
static final String URL = "Some____ URL";
// XML node keys
static final String KEY_RESPONSE = "Response"; // parent node
static final String KEY_NAME = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_ID = "ConsultantID";
static final String KEY_OFFLINE = "OnlineStatus";
static final String KEY_THUMB_URL = "ProfilePicture";



ListView list;
LazyAdapter adapter;
//String name;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list=(ListView)findViewById(R.id.list);

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

    XMLfunctions parser = new XMLfunctions();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_RESPONSE);
    // 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_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_OFFLINE, parser.getValue(e, KEY_OFFLINE));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }




    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
   list.setAdapter(adapter);
   list.setTextFilterEnabled(true);  


    // Click event for single list row
   list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position);

        Intent i = new Intent(MainActivity.this,ShowLarge.class);
        i.putExtra("ID",o.get("KEY_ID"));
        i.putExtra("Name", o.get("KEY_NAME"));
        startActivity(i);

        Toast.makeText(MainActivity.this, "ID '" + o.get("KEY_ID") + "' was clicked.", Toast.LENGTH_LONG).show(); 

        }
    });
           }}

但是当我运行应用程序时,它给出了以下错误..

07-09 16:35:42.024: E/AndroidRuntime(618): java.lang.ClassCastException: java.lang.Integer

我不明白这一切为什么会发生..请提前帮助thanksx

我的第二个活动代码是。

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 title = (TextView)vi.findViewById(R.id.title); // title name
    TextView description = (TextView)vi.findViewById(R.id.artist); // descriptiom
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    TextView Status = (TextView)vi.findViewById(R.id.status); //status
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(MainActivity.KEY_NAME));
    description.setText(song.get(MainActivity.KEY_DESCRIPTION));
    Status.setText(song.get(MainActivity.KEY_OFFLINE));
    duration.setText(song.get(MainActivity.KEY_ID));
    imageLoader.DisplayImage(song.get(MainActivity.KEY_THUMB_URL), thumb_image);
    return vi;
}

}

4

2 回答 2

0

像这样更改 Lazy Adapter 类中的方法.. 将获得所需的结果

 public Object getItem(int position) {
    return data.get(position);
}

并像这样更改 ONItemClickListner ...

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> Parent, View view, int Position,
                long Id) {
            // TODO Auto-generated method stub
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(Position);
            Intent i = new Intent ("com.fedorvlasov.lazylist.SHOWLARGE");
            i.putExtra("ID",o.get(KEY_ID));
            i.putExtra("NAME", o.get(KEY_NAME)); 
            startActivity(i);

这会将与列表视图中的位置相关联的值传递给另一个活动... :)

于 2012-07-10T13:10:59.403 回答
0

通常您可以为此目的使用视图标签。

view.setTag('KEY_NAME', key_name_obj)

view.getTag('KEY_NAME')

于 2013-03-27T12:15:19.497 回答