Android - 从 ListView 上单击的项目传递值并将它们显示在新的 Activity 上

我是这家公司的实习生,我的团队领导给我分配了一项任务,我需要制作一个应用程序,显示我可以添加和编辑/删除列表中的项目的项目列表。我正在遵循给我的关于应用程序需要做什么的要求。


我遇到的问题是,当单击ListView使用自定义适配器的a 时,我需要从项目传递值,并将它们发送到新活动并显示在新活动的textviews和 上imageview。我尝试使用putExtras()列表单击方法中的方法并使用getExtras()方法获取值,但它们不起作用,我已经删除了这些代码,因此它们不再存在。如果您需要更多我正在使用的课程/活动,请告诉我。我正在使用Android Studio 3.1.4


ItemListView.java


public class ItemListView extends AppCompatActivity {


DatabaseHelper myDB;


ArrayList<Item> itemList;

ListView listView;


Item item;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_item_list_view);


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

    myDB = new DatabaseHelper(this);

    itemList = new ArrayList<>();

    Cursor data = myDB.getListContents();


    int numRows = data.getCount();

    if(numRows == 0){

        Toast.makeText(ItemListView.this, "There is nothing in the database.", Toast.LENGTH_LONG).show();

    } else {

        while(data.moveToNext()){

            item = new Item(data.getString(1), data.getString(2), data.getString(3));

            itemList.add(item);

        }

        Row_ListAdapter adapter = new Row_ListAdapter(this, R.layout.list_adapter_view, itemList);

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

        listView.setAdapter(adapter);

    }


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override

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


            Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);


            startActivity(intent);

        }

    });

}

ViewItemClicked.java 我希望在单击一行时将值显示在此活动的布局上。


public class ViewItemClicked extends AppCompatActivity {


    ImageView image;

    TextView name, desc;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_view_item_clicked);

    }

}

图形界面链接:https : //i.stack.imgur.com/wHfgv.png


莫回无
浏览 316回答 3
3回答

繁华开满天机

在传递数据之前,使用 Serializable 实现您的项目类,如下所示:public class Item implements Serializable{&nbsp; &nbsp; &nbsp;/*your class code here*/}然后像这样在 listView.setOnItemClicklistener 中传递数据listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Item passItem = itemList.get(position);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle itemBundle = new Bundle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; itemBundle.putSerializable("dataItem",passItem)// put the data and define the key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.putExtras(itemBundle)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });并在 ViewItemClicked.Class 中打开数据public class ViewItemClicked extends AppCompatActivity {&nbsp; &nbsp; ImageView image;&nbsp; &nbsp; TextView name, desc;&nbsp; &nbsp; Item item;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; item = (Item) getIntent().getSerializableExtra("dataItem"); // use the key&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_view_item_clicked);&nbsp; &nbsp; &nbsp; &nbsp; /*now u can use the item data*/&nbsp; &nbsp; }}

收到一只叮咚

您使用的是哪种格式的图像是 Base64 还是 Bitmap 如果是 Base64 试试这个代码..listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = list.get(position).getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String description = list.get(position).getDescription();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String image= list.get(position).getImage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }在您的第二个活动中使用此代码..Intent intent = getIntent();&nbsp; &nbsp; String name = intent.getStringExtra("name");&nbsp; &nbsp; String description = intent.getStringExtra("description");&nbsp; &nbsp; String image = intent.getStringExtra("image");在这里将 Base64 转换为 Bitmap ..&nbsp;byte[] decodedString = Base64.decode(image, Base64.DEFAULT);&nbsp;Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,&nbsp;&nbsp;decodedString.length);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java