项目数量未知的 OnClickItem Android

OnClickItem with unknown number of items Android

我有一组对象。在我的列表视图中,我只传递了这些对象的名称,但是当有人单击其中任何一个时,我希望弹出一个新的 window 并查看我的项目的额外信息。我能以某种方式做到吗?

这是我的列表 activity 的样子:

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();
    }

所以这没关系,它向我显示了一堆名字。当我点击它们时,它只是在一个小弹出窗口中告诉我我已经选择了它。我想要的实际上是打开一个新的 window 并显示我的项目的所有信息。那可能吗?我将如何去做?

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

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 个活动,我想要 1 个一般 window 这将取决于某个整数位置。

我可以这样做吗?

如果您是从 listView 获取项目的位置,那么我认为您可以通过使用 Adapter 获取有关同一项目的信息。

您可以尝试的代码:

  1. 创建一个 xml 您的列表视图项目将具有:

这可以包括任何类型的项目,并且项目会在列表视图中显示为您希望显示的样子。我正在制作一个名为 list_items_view.xml 的 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>
  1. 创建一个 class,其中包含您要与每个列表项绑定的项目:

在这里,我将每个列表项与其描述、价格和热量(您可以根据需要更改)绑定在一起,并为每个 getter-setter 创建构造函数和方法 [ class 的 =100=] 是 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;
    }
}
  1. 制作一个适配器,可以在一个项目中适应 xml 和 class 的属性:

我在这里制作了一个适配器 class,它扩展了 BaseAdapter 并根据 purpose.Name 的 class 的使用实现了它的方法是 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;
    }
}
  1. 最后在您的主 activity 中实现适配器,以便包含具有绑定数据的列表项:([=102= 的名称] 是 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);
    
        }
        });
    
    }
    
  2. 在新的activity中使用activity:

    [=73=获取数据显示在表格中]

这里你必须为新的activity定义一个新的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. 列表视图

  1. 新 activity
  2. 中的项目详细信息

希望对您有所帮助!

我不是很了解,但是你可以使用 Intent 来创建新的 Window 例如:

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

您可以启动一个公共 activity 并将所选项目与意图一起传递:

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
}

在打开的activity中,可以从intent中获取item并显示其内容:

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

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

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 方法接受一个键和一个值 可以在目的地 Activity.

中检索到
Bundle extras = getIntent().getExtras();
String id,key;
    if(extras == null) {
        id = null;
        key = null; 
    } else {
        id= extras.getString("id");
        key= extras.getString("key");
    }