如何从arraylist中获取字符串

时间:2017-04-03 07:03:33

标签: java android arraylist

我从ArrayList获取值时遇到问题。我能做的就是获得价值:[Ljava.lang.string @....

你可以帮帮我吗?我不知道mProductList.get(???)应该是什么。我想从点击的项目中分离出值。

public class Activity1 extends Activity {
private ListView lvProduct;
private ProductListAdapter adapter;
private List<Product> mProductList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);

    lvProduct = (ListView)findViewById(R.id.listView1);

    mProductList = new ArrayList<>();
    mProductList.add(new Product(1, "Product_1", "Price", "Description"));
    mProductList.add(new Product(2, "Product_2", "Price", "Description"));
    mProductList.add(new Product(3, "Product_3", "Price", "Description"));

    adapter = new ProductListAdapter(getApplicationContext(), mProductList);
    lvProduct.setAdapter(adapter);

    lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent newActivity = new Intent(getApplicationContext(), BrowseProduct.class);
            newActivity.putExtra("1st column value", mProductList.get(???));
            newActivity.putExtra("2nd column value", mProductList.get(???));
            newActivity.putExtra("3rd column value", mProductList.get(???));
            newActivity.putExtra("4th column value", mProductList.get(???));
            startActivity(newActivity);
        }
    });
}

}

8 个答案:

答案 0 :(得分:1)

您应在 Product

中声明获取并设置方法
 nowyEkran.putExtra("1st column value", mProductList.get(position).getProduct());

示例

        /**
         * Created by Intellij Amiyo  on 03-04-2017.
         */

        public class Product implements Serializable {

        public String product,price,description;
        int id;

        // Empty constructor
        public Product()
        {

        }

        // constructor
        public Product( int id,String product,String price,String description) {

            this.price = price;
            this.product = product;
            this.description = description;
            this.id=id;

        }

          public String getprice() {
            return this.price;
        }

        public void setprice(String price) {
            this.price = price;
        }
         public String getProduct() {
            return this.product;
        }

        public void setProduct(String product) {
            this.product = product;
        }
         public String getDescription() {
            return this.description;
        }

        public void setDescription(String product) {
            this.description = description;
        }

        public int getID() {
            return this.id;
        }

        public void setID(int id) {
            this.id = id;
        }

}

答案 1 :(得分:0)

试试这个

mProductList.get(position).getMethod()

答案 2 :(得分:0)

这将获得数组列表的索引0 1 2,它将返回对象

mProductList.get(0);
mProductList.get(1);
mProductList.get(2);

答案 3 :(得分:0)

您需要先获取位置参考,然后从ArrayList

中获取该位置的值
mProductList.get(position).getProductName();

快乐的编码!

答案 4 :(得分:0)

将位置放在大括号()中。如下......

lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent newActivity = new Intent(getApplicationContext(), BrowseProduct.class);
            newActivity.putExtra("1st column value", mProductList.get(position));
            newActivity.putExtra("2nd column value", mProductList.get(position));
            newActivity.putExtra("3rd column value", mProductList.get(position));
            newActivity.putExtra("4th column value", mProductList.get(position));
            startActivity(newActivity);
        }
    });

希望这会对你有所帮助......(:

答案 5 :(得分:0)

mProductList.get(position).price

答案 6 :(得分:0)

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

        }

您可以从回调方法的int position参数中获取点击的项目位置

答案 7 :(得分:0)

您已经有一个名为position的参数。这将是get的参数。但是,return Product对象将需要从那里读取给定的成员。由于您未与我们分享Product class的代码,因此我们只能猜测:

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent newActivity = new Intent(getApplicationContext(), BrowseProduct.class);
        newActivity.putExtra("1st column value", mProductList.get(position).ID);
        newActivity.putExtra("2nd column value", mProductList.get(position).Name);
        newActivity.putExtra("3rd column value", mProductList.get(position).Price);
        newActivity.putExtra("4th column value", mProductList.get(position).Description);
        startActivity(newActivity);
    }