2017-08-01 102 views
0

我有一個列表視圖(如下所示),它具有成分列表以及每個項目具有多少成分。我怎樣才能做到這一點,所以有一種方法可以在點擊每種成分時編輯數字值?如何通過界面編輯ListView中的項目

enter image description here

上下文菜單,並警告對話框,到目前爲止,我也算是按鈕相匹配的項目,但發現它沒有與無限的列表視圖工作。感謝任何幫助我的人!

+0

你要更改號碼,只需點擊一個項目?或者你想說像打開一個對話框並編輯值並從對話框中獲取值並更新列表? – sam

+0

後者是目標 – Keith

回答

0

如果你想保存這些值,那麼你可以創建一個子類並創建這個子類的列表。 您可以使用onItemClickListner來更新對象中的值,然後在適配器上執行notifydatasetchange,這將實現此功能。

另外,您可以通過這兩個列表中的適配器和更新要更新並做notifydatasetchange列表。

爲e.g.:-

//MainActivity 
public class MainActivity extends AppCompatActivity { 

ListView recView; 
ArrayList<Childclass> chobj=new ArrayList<>(); 

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

    recView= (ListView) findViewById(R.id.recView); 

    chobj.add(new Childclass("Tomato sauce","0.0")); 
    chobj.add(new Childclass("Chicken","0.0")); 
    chobj.add(new Childclass("Olives","0.0")); 

    ListAdapter lstadptr=new ListAdapter(MainActivity.this,chobj); 
    recView.setAdapter(lstadptr); 

    recView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 

      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this); 
      LayoutInflater inflater = MainActivity.this.getLayoutInflater(); 
      final View dialogView = inflater.inflate(R.layout.custom_dialog, null); 
      dialogBuilder.setView(dialogView); 

      final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); 

      dialogBuilder.setTitle("Custom dialog"); 
      dialogBuilder.setMessage("Enter text below"); 
      dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //do something with edt.getText().toString(); 
        chobj.get(position).setQuantity(edt.getText().toString()); 
        ((ListAdapter) recView.getAdapter()).notifyDataSetChanged(); 
       } 
      }); 
      dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //pass 
       } 
      }); 
      AlertDialog b = dialogBuilder.create(); 
      b.show(); 

     } 
    }); 

}} 

子類

public class Childclass { 

String name; 
String quantity; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getQuantity() { 
    return quantity; 
} 

public void setQuantity(String quantity) { 
    this.quantity = quantity; 
} 

public Childclass(String name, String quantity) { 

    this.name = name; 
    this.quantity = quantity; 
}} 

ListAdapter

public class ListAdapter extends ArrayAdapter { 

ArrayList<Childclass> chobj; 
Context context; 

public ListAdapter(@NonNull Context context,ArrayList<Childclass> chobj) { 
    super(context, R.layout.recyclerow, chobj); 
    this.context = context; 
    this.chobj = chobj; 
} 

@NonNull 
@Override 
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 

    View v; 
    LayoutInflater inflater = LayoutInflater.from(getContext()); 
    v = inflater.inflate(R.layout.recyclerow, parent, false); 
    TextView tv= (TextView) v.findViewById(R.id.txtrow); 
    TextView tv1= (TextView) v.findViewById(R.id.txtrow1); 

    tv.setText(chobj.get(position).getName()); 
    tv1.setText(chobj.get(position).getQuantity()); 

    return v; 
}} 

活動主

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.abhishek.kotlinprojecttest.MainActivity"> 


<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f2f2f2" 
    android:paddingTop="10dp" 
    android:id="@+id/recView"> 

</ListView> 

定製警報

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp" 
android:orientation="vertical"> 

<EditText 
    android:id="@+id/edit1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" /> 

列表行

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginBottom="3dp" 
    android:layout_marginTop="3dp" 
    android:id="@+id/rowcd"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="listTExt" 
     android:id="@+id/txtrow" 
     android:textColor="#000000" 
     android:textSize="16sp" 
     android:gravity="center"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="listTExt" 
     android:id="@+id/txtrow1" 
     android:textColor="#000000" 
     android:textSize="16sp" 
     android:gravity="center"/> 

</android.support.v7.widget.CardView> 

希望這將幫助你

相關問題