我有意从另一个类获取String[]中的数据。我在列表视图中显示我收到的数据。当我单击自定义适配器上的deleteshop按钮时,我希望从String数组中删除我单击的数据。我该怎么做?
我的代码;
final ListView list = findViewById(R.id.list);
final ArrayList<SubjectData> arrayList = new ArrayList<SubjectData>();
final String[] cartList = getIntent().getStringArrayExtra("saleData");
arrayList.add(new SubjectData("", ""));
final int cartListLength = cartList.length;
int counter = 0;
String lastItem = "";
for (String e : cartList) {
counter += 1;
if (e == "" || e == null) {
lastItem = cartList[counter-3];
break;
}
String productPhoto = "";
switch (e) {
case "1 PC GREEN COLA x 10.00 TL":
productPhoto = "cc";
break;
default:
productPhoto = "";
break; }
arrayList.add(new SubjectData(e, productPhoto));;
}
arrayList.remove(arrayList.size()-1);
arrayList.remove(arrayList.size()-1);
final CustomAdapter customAdapter = new CustomAdapter(this, arrayList);
list.setAdapter(customAdapter);
nextButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
nextButton.setImageResource(R.drawable.next_down);
break;
case MotionEvent.ACTION_UP:
nextButton.setImageResource(R.drawable.next_up);
Intent myIntent = new Intent(getApplicationContext(), PrinterManagerActivity.class);
myIntent.putExtra("saleData", cartList);
startActivityForResult(myIntent, 0);
//
break;
}
return true;
}
});
SubjectData模型类别:
String SubjectName;
String Image;
public SubjectData(String subjectName, String image) {
this.SubjectName = subjectName;
this.Image = image;
}
自定义适配器;
class CustomAdapter implements ListAdapter {
ArrayList<SubjectData> arrayList;
Context context;
public CustomAdapter(Context context, ArrayList<SubjectData> arrayList) {
this.arrayList=arrayList;
this.context=context;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final SubjectData subjectData=arrayList.get(position);
if(convertView==null){
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView=layoutInflater.inflate(R.layout.list_row, null);
TextView tittle=convertView.findViewById(R.id.title);
ImageView imag=convertView.findViewById(R.id.list_image);
ImageView
deleteshop=convertView.findViewById(R.id.deleteshop);
deleteshop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (context instanceof SaleSummary) {
((SaleSummary)context).deleteItem(position);
int selectedFromList = (int) list.getItemAtPosition(position);
String gettextdata = tittle.getText().toString();
Toast.makeText(context, gettextdata""+"the product has been deleted
", Toast.LENGTH_LONG).show();
}
}
});
tittle.setText(subjectData.SubjectName);
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(subjectData.Image, "drawable",
context.getPackageName());
imag.setImageResource(resourceId);
}
return convertView;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return arrayList.size();
}
@Override
public boolean isEmpty() {
return false;
}
列表视图设计;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:gravity="center"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:layout_gravity="center"
android:id="@+id/deleteshop"
android:src="@drawable/negative"
android:layout_width="25dp"
android:layout_height="25dp" ></ImageView>
</LinearLayout>
这基本上是从数组中删除数据,这是正常的编程/数据结构任务。
您可以在cartList上运行for循环,当您遇到要删除的项目时,您可以简单地用下一个项目覆盖该项目,直到到达列表的末尾,然后将最后一个项目标记为null。
for(int i ==0; i < cartList.length(); i++){
if(cartList[i] == selectedItem){
for(int j = i j < cartList.length(); j++){
cartList[j] == cartList[j++];
}
// mark last item as null
cartList[cartList.length()-1] = null;
break;
}
}
现在您所需要的只是活动和适配器之间的侦听器,每当用户单击任何项上的删除项时,该侦听器都会触发逻辑。