2013-10-30 41 views
3

我一直在做的是關於customadapter。現在我想從我的列表視圖中獲取選定的項目,但它的適配器是自定義的。在我的列表視圖中,我有一個圖像,標題和一個標題,我想獲取選定的項目,在我的情況下只是標題。我怎麼做?從customadapter listview中獲取所選項Android

這裏是我試過到目前爲止:

@Override 
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) { 
    String strHouseName = "house_name"; 
    String strHousePrice = "house_price"; 
    //String selectedFromList =(String) (lv.getItemAtPosition(pos)); 
    Intent i; 

    switch(pos){ 
    case 0: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "5 Dina Retreat, Carrum Downs"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 1: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 941 Cob Terrace, Clyde North"); 
     i.putExtra(strHousePrice, "$800,000.00"); 
     startActivity(i); 
     break; 
    case 2: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 932 Ventasso Street, Clyde"); 
     i.putExtra(strHousePrice, "$1,000,000.00"); 
     startActivity(i); 
     break; 
    case 3: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 515 Summerhill Blvd, Drouin"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 4: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 17 Todman Street, Drouin (Option 2)"); 
     i.putExtra(strHousePrice, "$550,000.00"); 
     startActivity(i); 
     break; 
    case 5: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 32 Aqueduct Road, Langwarrin"); 
     i.putExtra(strHousePrice, "$600,000.00"); 
     startActivity(i); 
     break; 
    case 6: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Units 2-7, 269 North Road, Langwarin"); 
     i.putExtra(strHousePrice, "$450,000.00"); 
     startActivity(i); 
     break; 
    case 7: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "U 39, 40-60 Potts Road, Langwarrin"); 
     i.putExtra(strHousePrice, "$1,300,000.00"); 
     startActivity(i); 
     break; 
    case 8: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 1 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$2,000,000.00"); 
     startActivity(i); 
     break; 
    case 9: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 2 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 10: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 3 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$900,000.00"); 
     startActivity(i); 
     break; 
    case 11: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 4 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$600,000.00"); 
     startActivity(i); 
     break; 
    case 12: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 5 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$1,200,000.00"); 
     startActivity(i); 
     break; 
    case 13: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 7 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$700,000.00"); 
     startActivity(i); 
     break; 
    case 14: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 11 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$500,000.00"); 
     startActivity(i); 
     break; 
    case 15: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 12 McClenaghan Place, Pakenham"); 
     i.putExtra(strHousePrice, "$3,000,000.00"); 
     startActivity(i); 
     break; 
    case 16: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 532 Summerhill Blvd, Drouin"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 17: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 17 Ajax Street, Drouin"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 18: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 133 Mountainview Blvd, Cranbourne North"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    case 19: 
     i = new Intent(this, ViewHouse.class); 
     i.putExtra(strHouseName, "Lot 531 Summerhill Blvd, Drouin"); 
     i.putExtra(strHousePrice, "$300,000.00"); 
     startActivity(i); 
     break; 
    default: Log.d(strHouseName, "No such house name available"); 
    } 

} 


    class MyCustomAdapter extends BaseAdapter 
{ 
    String[] data_text1; 
    String[] data_text2; 
    int[] data_image; 

MyCustomAdapter() { 
    data_text1 = null; 
    data_text2 = null; 
    data_image = null; 
} 

MyCustomAdapter(int[] image, String[] house, String[] price) { 
    data_text1 = house; 
    data_text2 = price; 
    data_image = image; 
} 

public int getCount() { 
    return data_text1.length; 
} 

public String getItem(int position) { 
    return null; 
} 

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

public View getView(int position, View convertView, ViewGroup parent) { 

    LayoutInflater inflater = getLayoutInflater(); 
    View row; 

    row = inflater.inflate(R.layout.listrow, null); 

    TextView textview1 = (TextView) row.findViewById(R.id.text1); 
    TextView textview2 = (TextView) row.findViewById(R.id.text2); 
    ImageView imageview = (ImageView) row.findViewById(R.id.image); 

    imageview.setScaleType(ImageView.ScaleType.FIT_XY); 
    textview1.setText(data_text1[position]); 
    textview2.setText("$" + (new DecimalFormat("#,###.00")).format(Double.parseDouble(data_text2[position]))); 
    imageview.setImageResource(data_image[position]); 

    return (row); 

    } 
} 

對於平均時間,我只是通過它的位置得到,但問題是,如果列表進行排序呢?怎樣才能得到正確的選擇項目?有任何想法嗎?我很樂意欣賞它。謝謝。

+0

你應該使用DataSet類和viewHolder類的列表視圖 之後,你就可以選擇行數據發送到下一個活動。 –

+0

@praveenSharma你可以給我一些示例代碼來做到這一點? ty – Dunkey

+0

我發佈一個代碼作爲答案如果您有任何疑問,那麼你可以問。 –

回答

6

試試這個..

@Override 
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) { 

    TextView textview1 = (TextView) v.findViewById(R.id.text1); 
    TextView textview2 = (TextView) v.findViewById(R.id.text2); 

    Log.v("textview1",textview1.getText().toString().trim()); 
    Log.v("textview2",textview2.getText().toString().trim()); 

} 
+0

thanx爲你的幫助 – droidev

0

試着去查看。 通過id解析它到你的textview。獲取文本,並繼續

0

試試這個..

public void onItemClick(AdapterView<?> parent, View v, int pos, long id) { 
    // Type cast to what the layout you used in the xml i.e, for the list item 
    // For example i took the RelativeLayout 
    if(v instanceOf RelativeLayout){ 
     RelativeLayout layout = (RelativeLayout) v; 
     TextView title = (TextView) layout.findViewById(R.id.conFormat_Name); 
     System.out.println(title.getText()); 
    } 

} 
0

您可以創建一個成員在您的自定義適配器等會兒選擇指數...

private int selectedIndex; 

然後在它初始化爲selectedIndex = -1定製適配器的構造函數及其吸氣和定位器...

public void setSelectedIndex(int index) { 
    this.selectedIndex = index; 
} 

public int getSelectedIndex() { 
    return selectedIndex; 
} 

And fina lly,你只需要在設置器中傳遞position參數的適配器onItemClick,並使用getter在你想要的任何地方獲取選定的項目。

而且一旦你維護的selectedIndex,你可以很容易地選擇項目如..

public BookInfo getSelectedItem() { 
    if (selectedIndex <= -1 || selectedIndex >= items.size()) { 
     return null; 
    } else { 
     return items.get(selectedIndex); 
    } 
} 

希望這可以幫助... :)

+0

嗯,我不認爲這會有所幫助,但是謝謝,我不是在尋找索引,而是項目本身。 – Dunkey

+0

這裏是項目親愛的,來源更新...:) –

1

在項目點擊獲取當前視圖,得到它的用戶界面的TextView標題的UI元素,並將它們發送到意圖

@Override 
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) 
{ 
    if (v== null) 
     v= inflater.inflate(R.layout.list_row_layout, null); // reached in case of listview bug in android which is a little chance but to make sure not to crash on NPE  
    String title = ((TextView) v.findViewById(R.id.list_row_title)).getText(); 
    String price = ((TextView) v.findViewById(R.id.list_row_price)).getText(); 

    i = new Intent(this, ViewHouse.class); 
    i.putExtra(strHouseName, title); 
    i.putExtra(strHousePrice, price); 
    startActivity(i); 
} 
+0

這是很好的謝謝 – Dunkey

+0

不客氣,如果你覺得有用,請親切接受 –

1

不確定爲一個例子

// DatasetClass

public class HouseData{ 

    //private String HouseName= ""; 
    private String HousePrice = ""; 

    /*public String getHouseName() { 
     return HouseName; 
    } 
    public void setHouseName(String HouseName) { 
     this.HouseName= HouseName; 
    }*/ 
    public String getHousePrice() { 
     return HousePrice ; 
    } 
    public void setHousePrice (String HousePrice) { 
     this.HousePrice = HousePrice ; 
    } 

// viewholder類是: -

public class ViewHolder 
     { 
      public TextView tv_HousePrice,tv_HouseName 

     } 

//這裏是這裏的getview方法。

public View getView(final int position, View convertView, 
       ViewGroup parent) { 

      final ViewHolder holder; 
      //holder=new ViewHolder(); 

      View listrow = convertView; 

      activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

      if (listrow == null) { 

       LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       listrow = vi.inflate(R.layout.your_listview_item_layout, null); 

       holder=new ViewHolder(); 
       holder.tv_HouseName=(TextView)listrow.findViewById(R.id.tv_name); 


       holder.tv_HousePrice=(EditText)listrow.findViewById(R.id.tv_price); 
       listrow.setTag(holder); 

      } 
      else 
       holder=(ViewHolder)listrow.getTag(); 
      listrow.setBackgroundColor(((position % 2)!=0) ? Color.rgb(229,229,229) : Color.WHITE); 

      try { 
       houseData= getItem(position); 



       if (houseData!= null) { 

        holder.tv_HouseName.setText(""+houseData.getHouseName()); 
        holder.tv_HousePrice.setText(""+houseData.getHousePrice()); 

       } 
      } catch (Exception e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 

      listrow.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
//here you can pass complete data object to next activity 
} 
      }); 


      return listrow; 
     } 
+2

看到這裏我只是從我的工作中提出一些代碼。 here workorderAssetCharactorDataset = getItem(position);您是否回答 –

+0

在這裏我把代碼按照您的要求。希望它對你有幫助。 –

相關問題