简体   繁体   中英

Passing data from ListView to activity upon onclick

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

Model.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;
}
}

The activity.java codes are partial as I am still trying out. So I am just trying to get two data(duration and price).

However, I am having error and my app kept crashing. May I kindly ask what is wrong with my codes. I actually have a Model code as shown.

The error is:

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)

Really appreciate some help here as this is the last part for my final year project. I am a amateur here by the way,so the more I am in need of help.

I basically just want to retrieve the data from the listview accordingly and just initialise the values, &&& it should not keep resetting the data back whenever i press it,because the activity requires it to run in the background(such as the timer)

Problem with your Textview when you getText from Textview that time any one Textview is not bind with xml that's why your app was crashed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM