2013-05-26 4 views
1

나는 안드로이드 초보자입니다.여러 줄로부터 텍스트 값 가져 오기 ListView

각 행에 여러 값이 들어있는 ListView가 있습니다. 행을 클릭하고 해당 행의 값을 의도에 전달하거나 토스트 메시지를 만들 수 있기를 원합니다.

는 지금까지, 나는 Object 형식의 변수를 캐스팅했고 그것은 작동하지만, 난의 경고 얻을 "유형의 안전 : 체크되지는 해시 맵에 Object에서 캐스팅"

이 값을 얻을 수있는 더 좋은 방법이 있나요 내가 원하는?

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; 
    } 
} 
에 대한 사용자 정의 기본 어댑터 ... 을 사용할 수 있습니다
관련 문제