2013-05-26 51 views
1

我是Android新手。从多行ListView获取文本值

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

到目前为止,我已经投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; 
    } 

} 

}

回答

0

您可以使用自定义的基础转接器... 更多细节click here

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; 
    } 
}