You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android基于PHP/JSON/Volley的MySQL CRUD问题:ListView点击项无法赋值到EditText

Fixing ListView Item Click to Populate EditTexts in Your Android CRUD App

Hey there! Let's work through this issue you're having. Since your Custom ListView is already displaying all your inserted data correctly, we just need to tweak the item click handling to get those values into your EditTexts. Here's a step-by-step breakdown of how to fix this:

1. Make Sure You've Set Up the ListView OnItemClickListener

First, double-check that you've attached a click listener to your ListView. This is the foundation for capturing when a user taps an item. Add this code to your Activity/Fragment where you initialize the ListView:

ListView customListView = findViewById(R.id.your_listview_id);
customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // We'll add the logic to populate EditTexts here
    }
});

2. Retrieve the Clicked Item's Data Object

Since you're using a custom adapter, you need to fetch the data model corresponding to the clicked position. Assume your adapter uses an ArrayList<YourDataModel> to hold data (replace YourDataModel with your actual model class name):

// Get the clicked item from the adapter
YourDataModel clickedData = (YourDataModel) parent.getItemAtPosition(position);
// Alternatively, you can get it directly from your adapter instance:
// YourDataModel clickedData = yourCustomAdapter.getItem(position);

Pro tip: Ensure your custom adapter's getItem() method returns the correct data object at the given position—this is a common spot for mistakes!

3. Populate the EditTexts with the Clicked Data

Now that you have the data object, just call setText() on your target EditTexts to fill them with the corresponding values. For example:

// Initialize your EditTexts (do this once, maybe in onCreate/onViewCreated)
EditText etId = findViewById(R.id.et_target_id);
EditText etName = findViewById(R.id.et_target_name);
EditText etDescription = findViewById(R.id.et_target_description);

// Set the values from the clicked data object
etId.setText(String.valueOf(clickedData.getId())); // Convert int to String if needed
etName.setText(clickedData.getName());
etDescription.setText(clickedData.getDescription());

4. Check for Common Pitfalls

If it's still not working, look for these common issues:

  • Null Pointer Exceptions: Verify that your EditText IDs match exactly what's in your layout file, and that you're initializing them after the view is inflated (e.g., in onCreate for Activities, onViewCreated for Fragments).
  • Incorrect Model Getters: Make sure your YourDataModel class has properly implemented getter methods (like getName(), getId()) that return the correct data.
  • Empty Data Object: Add a null check for clickedData to avoid crashes if the adapter returns an empty item.

Full Example Snippet

Here's how all the pieces fit together in your Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

    // Initialize your data list and custom adapter
    ArrayList<YourDataModel> dataList = fetchYourData(); // Your Volley/JSON fetch logic here
    YourCustomAdapter adapter = new YourCustomAdapter(this, dataList);
    
    // Set up ListView
    ListView customListView = findViewById(R.id.your_listview_id);
    customListView.setAdapter(adapter);

    // Initialize EditTexts once
    EditText etId = findViewById(R.id.et_target_id);
    EditText etName = findViewById(R.id.et_target_name);

    // Handle item clicks
    customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            YourDataModel clickedData = (YourDataModel) adapter.getItem(position);
            if (clickedData != null) {
                etId.setText(String.valueOf(clickedData.getId()));
                etName.setText(clickedData.getName());
            }
        }
    });
}

If you run into specific errors (like logcat messages), feel free to share those details, but this should cover the core fix for your issue.

内容的提问来源于stack exchange,提问作者shivani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:02:26