2013-06-08 3 views
0

데이터를 저장 한 사용자 지정 목록 구현 -> 클래스 -> 해시 맵 -> 목록 -> Adapter.It 데이터를 성공적으로 배치하고 list.But를 클릭하면 표시됩니다. list.I의 항목 내가 코드의이 부분에서 도움이 필요 클릭 value.my 코드를 검색 할 수 없습니다사용자 지정 목록보기에서 클릭 한 항목에서 값을 얻으려면

public class asasa extends Activity { 
    //ListView listView; 
    Intent intent; 
    private ProgressDialog pDialog; 

    //Class Declartion DataHolder 
    DataHolder obj; 

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

    static final String URL = "http://10.0.2.2/android_connect/get_all_products.php"; 

    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_PRODUCTS = "products"; 

    private static final String TAG_NAME = "name";   
    private static final String TAG_Description = "description"; 
    private static final String TAG_URL = "url"; 
    private static final String TAG_Price = "price"; 
    private static final String TAG_class = "DataHolider"; 


    LazyAdapter adapter; 
    // flag for Internet connection status 
    Boolean isInternetPresent = false; 

    // products JSONArray 

    JSONArray products = null; 

    // Connection detector class 
    ConnectionDetector cd; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     /*ListView l1 = (ListView) findViewById(R.id.sportsList); 
     l1.setAdapter(new MyCustomAdapter(text, image)); 
     */ 

     // creating connection detector class instance 
     cd = new ConnectionDetector(getApplicationContext()); 

     // get Internet status 
     isInternetPresent = cd.isConnectingToInternet(); 
     if (isInternetPresent) { 
      LoadAllProducts il = new LoadAllProducts(); 
      il.execute(URL); 
     } 
     else 
     { 
      // Internet connection is not present 
      // Ask user to connect to Internet 
      Toast.makeText(asasa.this, "No Internet Connection You don't have internet connection.", Toast.LENGTH_LONG).show(); 
     } 



    } 
    public void longToast(CharSequence message) { 
     Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
    } 

    public class LazyAdapter extends BaseAdapter { 

     private Activity activity; 
     private LayoutInflater inflater=null; 
     private ArrayList<HashMap<Integer, Object>> data; 
     //public ImageLoader imageLoader; 
     int i=0; 
     public LazyAdapter(Activity a, ArrayList<HashMap<Integer, Object>> 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 
      TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
      TextView duration = (TextView)vi.findViewById(R.id.duration); // duration 
      ImageView imageview=(ImageView) vi.findViewById(R.id.list_image); 
      //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image 

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

      DataHolder objj=new DataHolder(); 
      objj=(DataHolder) song.get(position); 

      Log.i("iiiiii "," " +i++); 

      Log.i("objj.GetName() ",objj.GetName()); 
      Log.i("objj.GetDescription() ",objj.GetDescription()); 
      Log.i("objj.GetPrice() ",objj.GetPrice()); 


      title.setText(objj.GetName()); 
      artist.setText(objj.GetDescription()); 
      duration.setText(objj.GetPrice()); 
      imageview.setImageBitmap(objj.Getimage()); 

      //imageLoader. 

      return vi; 
     } 
    } 






    class LoadAllProducts extends AsyncTask<String, String, String> { 
     // creating new HashMap 
      ArrayList<HashMap<Integer, Object>> productsList=new ArrayList<HashMap<Integer,Object>>(); 
      Bitmap decodedByte; 



     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(asasa.this); 
      pDialog.setMessage("Loading products. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 

      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(URL, "GET", params); 

      // Check your log cat for JSON reponse 
      //Log.d("All Products: ", json.toString()); 

      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // products found 
        // Getting Array of Products 
        products = json.getJSONArray(TAG_PRODUCTS); 
        Log.i("products ",products.toString()); 
        // looping through All Products 
        Log.i("LENGTHHHH "," "+products.length()); 
        for (int i = 0; i < products.length(); i++) { 
         JSONObject c = products.getJSONObject(i); 
         Log.i("ccccccccc ",c.toString()); 
         // Storing each json item in variable 
         // String id = c.getString(TAG_PID); 
         String name = c.getString(TAG_NAME); 
         Log.i("name::",name); 

         String description = c.getString(TAG_Description); 
         Log.i("description::",description); 

         /*String URl = c.getString(TAG_URL); 
         Log.i("URl",URl); 
*/ 
         String price = c.getString(TAG_Price); 
         Log.i("price",price); 

         byte[] decodedString = Base64.decode(c.getString(TAG_URL), Base64.DEFAULT); 
         decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

         obj=new DataHolder(); 
         obj.setData(name, description, price, decodedByte); 


          HashMap<Integer, Object> map = new HashMap<Integer, Object>(); 



         // adding each child node to HashMap key => value 
         map.put(i, obj); 
         /*map.put(TAG_Description, description); 
         map.put(TAG_URL, decodedByte); 
         map.put(TAG_Price, price);*/ 
         //Log.i("MAP",map.toString()); 
         // adding HashList to ArrayList 

         productsList.add(map); 

        } 
       } else { 
        // no products found 
        // Launch Add New product Activity 
        /*Intent i = new Intent(getApplicationContext(), 
          NewProductActivity.class); 
        // Closing all previous activities 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i);*/ 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 

      ListView list; 

      // dismiss the dialog after getting all products 
      list=(ListView)findViewById(R.id.list); 

      // Getting adapter by passing xml data ArrayList 
      //adapter=new LazyAdapter(a, d) 
      adapter=new LazyAdapter(asasa.this, productsList);   
      list.setAdapter(adapter); 


      // Click event for single list row 
      list.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
       Log.i("Name---------------"," "+ productsList.get(view.getId()).get(position).getClass().getName()); 
       } 
      });  

      if (pDialog.isShowing()) { 
       pDialog.dismiss(); 
       // progressDialog.setCancelable(true); 
      } 


    } 

    } 
    public void onPause() 
    { 
     super.onPause(); 


    } 


public class DataHolder 

{ 
    String Name; 
    String Description; 
    String Price; 
    Bitmap image; 

    public void setData(String Name,String Descipton,String Price,Bitmap iamage) 
    { 
     this.Name=Name; 
     this.Description=Descipton; 
     this.Price=Price; 
     this.image=iamage; 
    } 

    public String GetName() 
    {return Name;} 
    public String GetDescription() 
    {return Description;} 
    public String GetPrice() 
    {return Price;} 
    public Bitmap Getimage() 
    {return image;} 

} 





} 

됩니다 오전

// Click event for single list row 
    list.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
     Log.i("Name---------------"," "+ productsList.get(view.getId()).get(position).getClass().getName()); 
     } 
    });  

Q2 :의 getView가에서 백엔드하지만 궁극적으로 불리는 이유 목록이 성공적으로 표시됩니다.

답변

0

나는이 문제를 해결했습니다.

HashMap<String, Object> item = productsList.get(position); 
DataHolder Data= new DataHolder(); 
Data= (DataHolder) item.get(ITEMTITLE); 
Log.i("productsListID ", Data.Get_Id()) 

; 
관련 문제