gpt4 book ai didi

java - 如何使用对话框更改 listView 中 TextView 的文本

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:50 24 4
gpt4 key购买 nike

我正在处理 listView,我想通过对话框更改第二个文本,即 version_number。因此,当用户单击列表项时,会出现一个对话框,其中包含 EditText 字段,当他输入“yes”(正值)时,文本值将更改为用户输入的文本。

这是我的代码

HealthActivity.java

public class HealthActivity extends AppCompatActivity {

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

final ArrayList<List> disease = new ArrayList<List>();

disease.add(new List("BP", "120/80", R.drawable.blood_pressure));
disease.add(new List("Sugar Level(before meal)", "90", R.drawable.sugar));
disease.add(new List("Sugar Level(after meal)", "130", R.drawable.sugar1));
disease.add(new List("Pulse Rate", "70", R.drawable.heart_rate));
disease.add(new List("Heart Rate", "70", R.drawable.heart_rate));


ListAdapter listAdapter = new ListAdapter(this, disease);

// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// activity_numbers.xml layout file.
ListView listView = (ListView) findViewById(R.id.listview_flavor);

// Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
// {@link ListView} will display list items for each word in the list of words.
// Do this by calling the setAdapter method on the {@link ListView} object and pass in
// 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
listView.setAdapter(listAdapter);

}
}

List.java

public class List {

private String mDiseaseName;
private String mPersonStatus;
private int mImageResourceId;

public List(String diseaseName,String personStatus,int imageResourceId){
mDiseaseName=diseaseName;
mPersonStatus=personStatus;
mImageResourceId = imageResourceId;

}

public String getDiseaseName(){
return mDiseaseName;
}

public String getPersonStatus(){
return mPersonStatus;
}

public int getImageResourceId() {
return mImageResourceId;
}

}

ListAdapter.java

public class ListAdapter extends ArrayAdapter<List> {

private static final String LOG_TAG =ListAdapter.class.getSimpleName();

/**
* This is our own custom constructor (it doesn't mirror a superclass constructor).
* The context is used to inflate the layout file, and the list is the data we want
* to populate into the lists.
*
* @param context The current context. Used to inflate the layout file.
* @param androidFlavors A List of AndroidFlavor objects to display in a list
*/
public ListAdapter(Activity context, ArrayList<List> androidFlavors) {
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
super(context, 0, androidFlavors);
}

/**
* Provides a view for an AdapterView (ListView, GridView, etc.)
*
* @param position The position in the list of data that should be displayed in the
* list item view.
* @param convertView The recycled view to populate.
* @param parent The parent ViewGroup that is used for inflation.
* @return The View for the position in the AdapterView.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}

// Get the {@link AndroidFlavor} object located at this position in the list
List currentAndroidFlavor = getItem(position);

// Find the TextView in the list_item.xml layout with the ID version_name
TextView nameTextView = (TextView) listItemView.findViewById(R.id.version_name);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
nameTextView.setText(currentAndroidFlavor.getDiseaseName());

// Find the TextView in the list_item.xml layout with the ID version_number
TextView numberTextView = (TextView) listItemView.findViewById(R.id.version_number);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
numberTextView.setText(currentAndroidFlavor.getPersonStatus());

ImageView iconView=(ImageView) listItemView.findViewById(R.id.image);

iconView.setImageResource(currentAndroidFlavor.getImageResourceId());

// Return the whole list item layout (containing 2 TextViews and an
ImageView)
// so that it can be shown in the ListView
return listItemView;
}

}

activity_health.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview_flavor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

/>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="16dp"
/>

<TextView
android:id="@+id/version_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"/>

<TextView
android:id="@+id/version_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#000000"/>

</LinearLayout>

最佳答案

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {
//show your dialog and do this code in the 'yes' callback
List data= disease.get(position);
data.setPersonStatus("your edit text data");
listAdapter.notifyDataSetChanged();
}
});

关于java - 如何使用对话框更改 listView 中 TextView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46632756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com