1

中的自定義列表視圖中獲取所選項目我已經使用基礎適配器創建了自定義列表視圖。我想從列表視圖中獲取所選項目。我嘗試了不同的解決方案,但他們都沒有工作。我的ListView控件的代碼如下所示:無法從android

class SingleRow { 
     String mFetchedJobTitle; 
     String mFetchedCompanyName; 
     String mFetchedExperience; 
     String mFetchedLocation; 


     SingleRow(String mFetchedJobTitle, String mFetchedCompanyName, String mFetchedExperience, String mFetchedLocation) { 
      this.mFetchedJobTitle = mFetchedJobTitle; 
      this.mFetchedCompanyName = mFetchedCompanyName; 
      this.mFetchedExperience = mFetchedExperience; 
      this.mFetchedLocation = mFetchedLocation; 

     } 
    } 

    class MyAdapter extends BaseAdapter { 

     ArrayList<SingleRow> list; 
     Context context; 

     MyAdapter(Context c) { 
      context = c; 

      list = new ArrayList<SingleRow>(); 
      String[] mFetchedJobTitle1 = searchJobTitle; 
      String[] mFetchedCompanyName1 = searchCompanyName; 
      String[] mFetchedExperience1 = searchExperience; 
      String[] mFetchedLocation1 = searchLocation; 

      for (int i = 0; i < mArrayLength; i++) { 

       list.add(new SingleRow(mFetchedJobTitle1[i], mFetchedCompanyName1[i], mFetchedExperience1[i], mFetchedLocation1[i])); 

      } 

     } 

     @Override 
     public int getCount() { 
      return list.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return list.get(position); 
     } 

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

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

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View row = inflater.inflate(R.layout.custom_search_row, parent, false); 

      TextView title = (TextView) row.findViewById(R.id.textViewJobTitle); 
      TextView companyName = (TextView) row.findViewById(R.id.textViewCompanyName); 
      TextView experience = (TextView) row.findViewById(R.id.textViewExperience); 
      TextView location = (TextView) row.findViewById(R.id.textViewLocation); 


      SingleRow temp = list.get(position); 
      title.setText(temp.mFetchedJobTitle); 
      companyName.setText(temp.mFetchedCompanyName); 
      experience.setText(temp.mFetchedExperience); 
      location.setText(temp.mFetchedLocation); 


      return row; 
     } 
    } 

的ListView OnItemClickListener代碼:

 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 



    mSelectedItem = mListView.getItemAtPosition(i).toString(); 
          String mselected = (String) adapterView.getSelectedItem(); 

          Log.d("selecteditem", ""+mselected); 
          Log.d("selecteditem", ""+mSelectedItem); 

         } 
        }); 

       } 

      } 

我得到mSelectedItem的價值:

[email protected] 

如何得到確切的字符串值。任何人都可以給我一個想法。所有建議都歡迎。

+0

什麼是mSelectedItem類型' – Tauqir

回答

1

嘗試此

SingleRow mSelectedItem = mListView.getItemAtPosition(i);  
    String mFetchedJobTitle = mSelectedItem.mFetchedJobTitle; 
    String mFetchedCompanyName = mSelectedItem.mFetchedCompanyName; 
    String mFetchedExperience = mSelectedItem.mFetchedExperience; 
    String mFetchedLocation = mSelectedItem.mFetchedLocation; 
2

明白了,在你的每一個清單項中你都得到了一個SingleRow類的實例。 做到像

mListView.getItemAtPosition(i).mFetchedJobTitle; 
0

adapterView.getSelectedItem()返回對象的基準和在字符串澆鑄它返回一個包含類名和存儲位置,其中對象的字符串被儲存了。爲了得到選定的項目,你可以使用這種方法

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