1

我是安卓新手。

我有一个 ListView ,其中每行包含多个值。我希望能够单击一行并将该行中的值传递给一个意图,甚至可能创建一个 Toast 消息。

到目前为止,我已经转换了一个 Object 类型的变量并且它可以工作,但是我收到“类型安全:从 Object 到 Hashmap 的未经检查的转换”的警告

有没有更好的方法来获得我想要的值?

public class AndroidListViewActivity extends ListActivity { 

@Override
protected void onCreate(Bundle savedInstanceState) {
    ListParse mytask;

    super.onCreate(savedInstanceState);

    mytask = new ListParse();
    mytask.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.layout.activity_main, menu);
    return true;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    HashMap<String, String> selected = new HashMap<String, String>();
    //String item = (String) getListAdapter().getItem(position);
    Object o = getListAdapter().getItem(position);
    selected = (HashMap<String, String>) o;

    // Launching new Activity on selecting single List Item
    Intent i = new Intent(getApplicationContext(), SingleListItem.class);
    // sending data to new activity
    i.putExtra("product", selected.get("name"));
    startActivity(i);
}

private class ListParse extends AsyncTask<URL, Integer, Long> {
    // url to make request
    private static final String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    // create the grid item mapping
    String[] from = new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE};
    int[] to = new int[] { R.id.list_name, R.id.list_email, R.id.list_mobile };

    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();      

    @Override
    protected void onPostExecute(Long result) {
        /*Iterator<HashMap<String, String>> entry = contactList.iterator();
        while (entry.hasNext()){
            Log.e("Debug Info", "We have " + entry.next());
        }*/

        SimpleAdapter adapter = new SimpleAdapter(AndroidListViewActivity.this, contactList,
                R.layout.list_item, from, to);
        setListAdapter(adapter);
    }

    @Override
    protected Long doInBackground(URL... arg0) {

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String email = c.getString(TAG_EMAIL);
                // Phone number is again JSON Object
                JSONObject phone = c.getJSONObject(TAG_PHONE);
                String mobile = phone.getString(TAG_PHONE_MOBILE);
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_EMAIL, email);
                map.put(TAG_PHONE_MOBILE, mobile);                  

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

}

}

4

1 回答 1

0

您可以使用自定义基础适配器...更多详情请点击这里

public class MyCustomBaseAdapter extends BaseAdapter {
    private static ArrayList<searchresults> searchArrayList;

    private LayoutInflater mInflater;

    public MyCustomBaseAdapter(Context context, ArrayList<searchresults> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return searchArrayList.size();
    }

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

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row_view, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.name);
            holder.txtCityState = (TextView) convertView
                    .findViewById(R.id.cityState);
            holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(searchArrayList.get(position).getName());
        holder.txtCityState.setText(searchArrayList.get(position)
                .getCityState());
        holder.txtPhone.setText(searchArrayList.get(position).getPhone());

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtCityState;
        TextView txtPhone;
    }
}
于 2013-06-26T00:52:11.597 回答