2013-12-17 83 views
0

這裏是代碼,這就是列表視圖! 謝謝! **我想從列表中的行獲取信息,並在新的活動
打開它的代碼handele ListView,並且有持有單項視圖android:如何在新的活動中從列表視圖中打開項目?

public class HelpFriends extends Activity 
{ 
    ListView dilemasListView; 
    MyCustomListAdapter customListAdapter; 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.help_friends); 

     dilemasListView = (ListView) findViewById(R.id.dilemasListView); 

     HMCApplication myApp = (HMCApplication)getApplication(); 
     myApp.getAllDilemasFromServer(); 
     ArrayList<Dilema> myDilemas = myApp.getDilemas(); 

     //dilemasListView.setOnItemClickListener(this); 


     customListAdapter = new MyCustomListAdapter(this, 
           R.layout.dilema_list_layout,  // layout of a single item in the list 
           myDilemas); 
     dilemasListView.setAdapter(customListAdapter); 
    } 

    // the adapter handles the list 
    public class MyCustomListAdapter extends ArrayAdapter<Dilema> 
    { 
     // internal list of data 
     ArrayList<Dilema> mItems; 

     public MyCustomListAdapter(HelpFriends theActivity, 
       int viewResourceId, ArrayList<Dilema> objects) 
     { 
      super((Context) theActivity, viewResourceId, objects); 
      mItems = objects; 
     } 


     @Override   
     public int getCount() 
     {    
      return mItems.size(); 
     }  
     @Override   
     public Dilema getItem(int position) 
     {    
      return mItems.get(position); 
     }   
     @Override  
     public int getPosition(Dilema item) 
     {   
      return mItems.indexOf(item); 
     } 

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

     // getView returns/creates the view for a SINGLE item in the list 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
     { 

      // theUser will be the logic object with all data 
      Dilema theDilema = mItems.get(position); 

      // row will be the visual object 
      LayoutInflater inflater = getLayoutInflater(); 
      // single_fruit_item_layout is the layout of a single row 
      View row = inflater.inflate(R.layout.dilema_list_layout, parent, false); 


      // populate the row object from theUser object 
      TextView TextVieUsername = (TextView) row.findViewById(R.id.usernameText); 
      TextVieUsername.setText(theDilema.getUsername()); 

      TextView TextViewQuestion = (TextView) row.findViewById(R.id.questionText); 
      TextViewQuestion.setText(theDilema.getQuestion()); 

      TextView TextViewTime = (TextView) row.findViewById(R.id.timeText); 
      TextViewTime.setText(theDilema.getTime()); 

      TextView TextViewCategory = (TextView) row.findViewById(R.id.categoryText); 
      TextViewCategory.setText(theDilema.getCategory()); 

      return row; 
     } 
    } 



} 

回答

0

它添加到XML代碼的創建....

dilemasListView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 


       Intent myIntent = new Intent(activityname.this, nextactivity.class); 
       startActivityForResult(myIntent, 0); 
      } 
     } 
    }); 
+0

謝謝你,你幫我很多! – user3111912

+0

也許你可以告訴我我是如何從列表中的每一行獲取數據並將其顯示在新的活動中? – user3111912

0

使用setOnItemClickListener。在這個任務的內部,你可以編寫新的活動調用代碼。

dilemasListView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
Intent i = new Intent(); 
i.setclass(current_activity_name.this, next_activity.class); 
startActivity(i); 
} 
} 
0

您所選擇的項目的信息是在收聽觀看等級:

onItemClick(AdapterView<?> parent, View view,int position, long id) 

您可以使用視圖來獲取值。

相關問題