2017-10-08 9 views
1

listView에서 작업 중이고 두 번째 텍스트 (대화 상자로 version_number)를 변경하고 싶습니다. 따라서 사용자가 목록 항목을 클릭하면 EditText 필드가있는 대화 상자가 나타나고 '예'(양수 값)를 입력하면 텍스트 값이 사용자가 입력 한 텍스트로 변경됩니다.대화 상자를 사용하여 listView에서 TextView의 텍스트를 변경하는 방법

여기 내 코드

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

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> 

답변

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