onclick 时将数据从 ListView 传递到活动
Posted
技术标签:
【中文标题】onclick 时将数据从 ListView 传递到活动【英文标题】:Passing data from ListView to activity upon onclick 【发布时间】:2019-08-13 17:01:20 【问题描述】:AuctionList.java
public class AuctionList extends AppCompatActivity
ListView mListView;
ArrayList<Model> mList;
AuctionListAdapter mAdapter = null;
DatabaseHelperUpload mDBUpload;
TextView txtName,txtDescription,txtDuration,txtPrice;
ImageView imageViewIcon;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.auction_list);
mDBUpload = new DatabaseHelperUpload(this);
mListView = findViewById(R.id.listView);
mList = new ArrayList<>();
mAdapter = new AuctionListAdapter(this,R.layout.row,mList);
mListView.setAdapter(mAdapter);
txtName = findViewById(R.id.txtName);
txtDescription = findViewById(R.id.txtDescription);
txtDuration = findViewById(R.id.txtDescription);
txtPrice = findViewById(R.id.txtPrice);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
String name = txtName.getText().toString();
String description = txtDescription.getText().toString();
String price = txtPrice.getText().toString();
String duration = txtDuration.getText().toString();
Intent intent5 = new Intent(AuctionList.this,BuyerHome.class);
intent5.putExtra("name",name);
intent5.putExtra("description",description);
intent5.putExtra("price",price);
intent5.putExtra("duration",duration);
startActivity(intent5);
);
Activity.java
Bundle bundle = getIntent().getExtras();
String duration = bundle.getString("duration");
String price = bundle.getString("price");
TextView durationLog = (TextView)findViewById(R.id.text_view_countdown);
TextView priceLog = (TextView)findViewById(R.id.textPrice);
durationLog.setText(duration);
priceLog.setText(price);
模型.java
public class Model
private int id;
private String name;
private String duration;
private String description;
private String price;
private byte[] image;
public Model(int id, String name,String description,String price,String duration, byte[] image)
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.duration = duration;
this.image = image;
public String getDescription()
return description;
public void setDescription(String description)
this.description = description;
public String getPrice()
return price;
public void setPrice(String price)
this.price = price;
public int getId()
return id;
public void setId(int id)
this.id = id;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getDuration()
return duration;
public void setDuration(String duration)
this.duration = duration;
public byte[] getImage()
return image;
public void setImage(byte[] image)
this.image = image;
activity.java 代码是部分的,因为我仍在尝试。所以我只是想获取两个数据(持续时间和价格)。
但是,我遇到了错误,我的应用程序不断崩溃。请问我的代码有什么问题。我实际上有一个模型代码,如图所示。
错误是:
2019-03-23 00:30:58.046 11048-11048/com.example.jianminmong.aucon E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.jianminmong.aucon, PID: 11048
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.jianminmong.aucon.AuctionList$1.onItemClick(AuctionList.java:63)
at android.widget.AdapterView.performItemClick(AdapterView.java:318)
at android.widget.AbsListView.performItemClick(AbsListView.java:1159)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136)
at android.widget.AbsListView$3.run(AbsListView.java:4052)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
非常感谢这里的一些帮助,因为这是我最后一年项目的最后一部分。顺便说一句,我是这里的业余爱好者,所以我越需要帮助。
我基本上只是想相应地从列表视图中检索数据并初始化值,&&& 它不应该在我按下它时不断重置数据,因为活动需要它在后台运行(例如计时器)
【问题讨论】:
【参考方案1】:当您从 Textview 获取文本时,您的 Textview 出现问题,此时任何一个 Textview 都没有与 xml 绑定,这就是您的应用程序崩溃的原因。
mListView.setOnItemClickListener(new.
AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id)
String name = txtName.getText().toString();
String description =
txtDescription.getText().toString();
String price = txtPrice.getText().toString();
String duration = txtDuration.getText().toString();
Intent intent5 = new Intent(AuctionList.this,BuyerHome.class);
intent5.putExtra("name",name);
intent5.putExtra("description",description);
intent5.putExtra("price",price);
intent5.putExtra("duration",duration);
startActivity(intent5);
);
【讨论】:
我已经初始化了 alr ,只是我没有在这里展示。还是我误解了你的解释?你能告诉我我应该怎么做吗?我刚刚编辑了显示我的初始化的代码。 我已经知道是什么问题,但我不知道如何解决。我正在从另一个 xml 文件中检索数据。但我无法更改此 java 文件的 xml 文件。有什么办法解决吗?以上是关于onclick 时将数据从 ListView 传递到活动的主要内容,如果未能解决你的问题,请参考以下文章
从按钮 OnClick 方法中访问 listview/arrayadapter