2016-11-19 38 views
0

我有一个对象数组。在我的列表视图中,我仅传递这些对象的名称,但是当有人点击它们中的任何一个时,我想要一个新窗口弹出并查看我的项目中的额外信息。我能以某种方式做到吗?具有未知数量的项目的OnClickItem Android

这是我的名单活动看起来像:

public class ListItemsActivity extends ListActivity { 
    String[] mTestArray; 
    ListView listView; 
    private static final String TAG = "ListActivity"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     Log.d(TAG, "Trying to create the list activitye"); 
     super.onCreate(savedInstanceState); 
     ArrayAdapter<String> adapter; 

     mTestArray = getResources().getStringArray(R.array.sections); 
     ArrayList<Sweet> sweets = getSweets(mTestArray); 
     ArrayList<String> result = getSweetsNames(mTestArray); 
     Log.d(TAG, mTestArray.toString()); 

     adapter = new ArrayAdapter<String>(
       this, 
       R.layout.activity_list_items, 
       result; 
     setListAdapter(adapter); 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
     parent.setItemChecked(position, parent.isItemChecked(position)); 

     Toast.makeText(this, "You have selected " + mTestArray[position], 
       Toast.LENGTH_SHORT).show(); 
    } 

因此,这是确定的,它让我的名字的lsit。当我点击它们时,它会告诉我一个我选择它的小弹出物。我想要的实际上是打开一个新窗口并显示来自我的项目的所有信息。那可能吗?我将如何去做这件事?

我发现的唯一办法是做这样的事情:

public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    switch(position) { 
    case 0: Intent newActivity = new Intent(this, i1.class);  
       startActivity(newActivity); 
       break; 
    case 1: Intent newActivity = new Intent(this, i2.class);  
       startActivity(newActivity); 
       break; 
    case 2: Intent newActivity = new Intent(this, i3.class);  
       startActivity(newActivity); 
       break; 
    case 3: Intent newActivity = new Intent(this, i4.class);  
       startActivity(newActivity); 
       break; 
    case 4: Intent newActivity = new Intent(this, i5.class);  
       startActivity(newActivity); 
       break; 
    } 
} 

但它的这些原因个不错的办法: 1)我有一个未知数量的元素 2)我没有1000周的活动对于每个项目,我想要一个通用窗口,这取决于某个整数位置。

我可以这样做吗?

+0

你想要显示什么信息? – Swr7der

+0

我有一个名为Sweets的类,ti有名称,价格,说明......现在我只显示名称 – Mocktheduck

回答

2

如果你正在从listView中获取物品的位置,那么我认为你可以通过使用Adapter获得关于同一物品的信息。

代码,你可以尝试:

  1. 做一个XML,你的列表视图中的项目将有:

这可以包括任何类型的项目和项目将在可见列表视图,你想显示它。我正在制作一个名为的XML,其中list_items_view.xml为,并且在列表视图中只包含一个文本视图。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/nameInList" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="26dp" 
     android:padding="7dp"/> 

</RelativeLayout> 
  • 做一个类,将包括要与每个列表项绑定物品:
  • 在这里,我结合各列表项与它的描述,价格和callories(你可以根据你的需要改变),使构造函数和getter,setter方法的类的每个one.Name是ListDetailsClass

    public class ListDetailsClass { 
    
        String price,name, description,calories; 
    
        public String getPrice() { 
         return price; 
        } 
    
        public void setPrice(String price) { 
         this.price = price; 
        } 
    
        public ListDetailsClass(String price, String name, String description, String calories) { 
         this.price = price; 
         this.name = name; 
         this.description = description; 
         this.calories = calories; 
        } 
    
        public String getName() { 
         return name; 
        } 
    
        public void setName(String name) { 
         this.name = name; 
        } 
    
        public String getDescription() { 
         return description; 
        } 
    
        public void setDescription(String description) { 
         this.description = description; 
        } 
    
        public String getCalories() { 
         return calories; 
        } 
    
        public void setCalories(String calories) { 
         this.calories = calories; 
        } 
    } 
    
  • 做出可能适应的XML的特性,并且在一个单一的项目类的适配器:
  • 这里我已延伸BaseAdapter适配器类并根据我的目的使用它的方法。类的名称是adapterForLV

    public class adapterForLV extends BaseAdapter { 
    
        ArrayList<ListDetailsClass> itemsInList; 
        Context mContext; 
    
        LayoutInflater inflater; 
    
        public Context getmContext() { 
         return mContext; 
        } 
    
        public void setmContext(Context mContext) { 
         this.mContext = mContext; 
        } 
    
        public ArrayList<ListDetailsClass> getItemsInList() { 
         return itemsInList; 
        } 
    
        public void setItemsInList(ArrayList<ListDetailsClass> itemsInList) { 
         this.itemsInList = itemsInList; 
        } 
    
        public adapterForLV(ArrayList<ListDetailsClass> itemsInList, Context mContext) { 
         this.itemsInList = itemsInList; 
         this.mContext = mContext; 
        } 
    
        @Override 
        public int getCount() { 
         return itemsInList.size(); 
        } 
    
        @Override 
        public Object getItem(int position) { 
         return itemsInList.get(position); 
        } 
    
        @Override 
        public long getItemId(int position) { 
         return position; 
        } 
    
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
    
         if(inflater == null){ 
          inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         } 
         if(convertView == null){ 
          convertView = inflater.inflate(R.layout.list_items_view,null); 
         } 
    
         TextView nameOfItem = (TextView) convertView.findViewById(R.id.nameInList); 
    
         ListDetailsClass items = itemsInList.get(position); 
    
         String name = items.getName(); 
    
         nameOfItem.setText(items.getName()); 
    
         return convertView; 
        } 
    } 
    
  • 最后在主活动实现适配器,以便包括与绑定数据的列表项:(的名称活动MainActivity

    ListView listView; 
    
    ArrayList<ListDetailsClass> list = new ArrayList<>(); 
    
    adapterForLV customAdapter; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        listView = (ListView) findViewById(R.id.lv) ; 
    
    //Adapted the list form with customAdapter 
    
        customAdapter = new adapterForLV(list,this); 
    
    //Set the listview to the customAdapter 
    
        listView.setAdapter(customAdapter); 
    
    //Made two new objects of the ListDetaisClass to add data in the listview 
    
        ListDetailsClass newData = new ListDetailsClass("3$","abc","description","543 cal"); 
        ListDetailsClass newData2 = new ListDetailsClass("35.3$","item name","description about item","callories about it"); 
    
    //Added data to the list 
    
        list.add(newData); 
        list.add(newData2); 
    
    
    //Listview item click listener implementation 
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    
         String name = customAdapter.getItemsInList().get(position).getName(); 
         String description = customAdapter.getItemsInList().get(position).getDescription(); 
         String price = customAdapter.getItemsInList().get(position).getPrice(); 
         String calories = customAdapter.getItemsInList().get(position).getCalories(); 
    
    
    //Intent to pass the data of the list item to next activity 
    
         Intent i = new Intent(getApplicationContext(),Main2Activity.class); 
         i.putExtra("Item_Name",name); 
         i.putExtra("Item_Desc",description); 
         i.putExtra("Item_Price",price); 
         i.putExtra("Item_cal",calories); 
         startActivity(i); 
    
        } 
        }); 
    
    } 
    
  • 获取数据根据我们在新的活动中使用的形式展现:

  • 在这里,您必须为新活动定义一个新的xml,以便可以以我们想要的形式显示数据。

    Main2Activity

    //defined textViews to show my data 
    TextView itemName,itemDescription,itemPrice,itemCal; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main2); 
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
        setSupportActionBar(toolbar); 
    
        itemName = (TextView) findViewById(R.id.ItemName); 
        itemDescription = (TextView) findViewById(R.id.ItemDescr); 
        itemCal = (TextView) findViewById(R.id.ItemCal); 
        itemPrice = (TextView) findViewById(R.id.ItemPrice); 
    
    //Getting data from oldActivity i.e. MainActivity 
        Intent i = getIntent(); 
    
    //Setting data to textViews 
        itemName.setText("Name: "+i.getStringExtra("Item_Name")); 
        itemDescription.setText("Description: "+i.getStringExtra("Item_Desc")); 
        itemPrice.setText("Price: "+i.getStringExtra("Item_Price")); 
        itemCal.setText("Calories: "+i.getStringExtra("Item_cal")); 
    
    
    } 
    

    截图执行后:

    1. 列表视图

    Listview

    在新的活动
  • 项目详细
  • item details

    希望这有助于你!

    +0

    我得到的位置,但我可以推动下一个活动的位置?让我们来说一下Intent intent = new Intent(ListItemActivity.this,newDetailActivity.class,position); startActivity(intent); ?? – Mocktheduck

    +0

    是的,当你想开始新的活动,只需发送的intent.put – Swr7der

    +0

    的细节和/或位置我不知道的代码,只是尝试,如果它的工作@Mocktheduck – Swr7der

    1

    我没有很好地理解,但你可以使用意向的新窗口例如:

    public void onListItemClick(ListView parent, View v, int position, long id) { 
        parent.setItemChecked(position, parent.isItemChecked(position)); 
    
        Intent intent=new Intent(ListItemActivity.this, newDetailActivity.class); //newDetailActivity is a Activity you need to create or can say redirect window 
    startActivity(intent); // This opens a window 
    } 
    

    下面是详细信息,官方文档Follow Documentation

    +0

    但我有我们说1000个项目。我如何显示信息? newDetailActivity中有什么?它如何知道我选择了哪个项目? – Mocktheduck

    +0

    为了您的最佳实践,您可以尝试创建一个新项目并选择主/细节流程,而不是空白活动,其中有完整的代码和工作模型。这就是你的意思。学习并实施你的代码介绍! –

    1

    可以启动一个共同的活动,并通过所选择的项目与意图一起:

    public void onListItemClick(ListView parent, View v, int position, long id) { 
        parent.setItemChecked(position, parent.isItemChecked(position)); 
    
        //DetailsActivity is the activity which shows the extra details 
        Intent intent=new Intent(ListItemActivity.this, DetailsActivity.class); 
    
        //Add the item that the user clicked on, the class has to implement Parcelable or Serializable 
        intent.putExtra("data", sweets.getItem(position)); 
        startActivity(intent); // This opens a window 
    } 
    

    在打开的活动中,您可以从意图中获取该项目并显示它的con帐篷:

    //in newDetailActivity : 
    Sweet s = getIntent().getExtras.getParcelable("data"); 
    
    +0

    好吧,我会试试这个。 – Mocktheduck

    1

    在活动之间传递数据最简单的方法是使用意图。

    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
    
        Intent newActivity = new Intent(this, i1.class); 
        newActivity.putExtra("id", postion); 
        newActivity.putExtra("key", value);   
        startActivity(newActivity); 
    
        } 
    

    总之,putExtra方法需要一个键和值 可以在目标的活动来检索。

    Bundle extras = getIntent().getExtras(); 
    String id,key; 
        if(extras == null) { 
         id = null; 
         key = null; 
        } else { 
         id= extras.getString("id"); 
         key= extras.getString("key"); 
        } 
    
    +1

    以及如何将onItemCLick方法添加到我的适配器? – Mocktheduck

    +0

    适配器是您数​​据和视图之间的桥梁。你想在你的listview实例上设置监听器 –

    相关问题