【问题标题】:I am new in android,i want to delete a row on clicking delete button that are shown in front of each row in list view我是 android 新手,我想在单击列表视图中每行前面显示的删除按钮时删除一行
【发布时间】:2016-08-31 07:23:51
【问题描述】:

这是可能的 XML 类随机值,我们在其中创建要删除的行

randomvalues.xml

  <LinearLayout
android:id="@+id/linear"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/addbtn">

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:src="@drawable/img1"
        />

   <LinearLayout
        android:layout_width="255dp"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#339966"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/adress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Address"
                android:textColor="#606060" />
        </LinearLayout>
    </LinearLayout>        

    <ImageButton
        android:id="@+id/removebtn"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:src="@drawable/remove"/>       
</LinearLayout>
</LinearLayout>

这是我的 activity_main XML,我在其中使用列表视图来显示我在随机值 XML 文件中创建的行

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.chaqeel.taskviews.MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linear">

<ImageButton
    android:id="@+id/addbtn"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:src="@drawable/add"
    android:layout_marginLeft="280dp"/>
</LinearLayout>


<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/linear"
   >

</ListView>
</RelativeLayout>

这是 MainActivity.java,我们在其中使用数组来显示值

MainActivity.java

package com.example.chaqeel.taskviews;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class MainActivity extends Activity {

ListView lv;

String[] Names = {"Aqeel", "Ali", "Ansar", "Usama", "Farhad"};
 String[] Address = {"Chakwal", "Rawalpindi", "Islamabad", "Lahore",       
"Multan"};
int[] Images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, 
R.drawable.img4, R.drawable.img5};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listview);
    lv.setAdapter(new dataListAdapter(Names, Address, Images));
}

class dataListAdapter extends BaseAdapter {
    String[] Name, Addres;
    int[] imge;

    /*dataListAdapter() {
        Name = null;
        Addres = null;
        imge=null;
    }*/

    public dataListAdapter(String[] text, String[] text1, int[] text3) {
        Name = text;
        Addres = text1;
        imge = text3;
    }

    public int getCount() {
        return Name.length;
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup  
     parent) {

        LayoutInflater inflater = getLayoutInflater();
        final View row;
        row = inflater.inflate(R.layout.randomvalues, parent, false);
        final TextView Name, Addres;
        ImageView imge;
        Name = (TextView) row.findViewById(R.id.name);
        Addres = (TextView) row.findViewById(R.id.adress);
        imge = (ImageView) row.findViewById(R.id.img);
        Name.setText(Names[position]);
        Addres.setText(Address[position]);
        imge.setImageResource(Images[position]);

      final   ArrayList<String> lvv= new ArrayList<>();
        Collections.addAll(lvv,Names);
     //  Collection.addAll(lvv,Address);

    ImageButton dltbutton = (ImageButton) findViewById(R.id.removebtn);
    dltbutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            lvv.remove(Names);
            lvv.remove(Address);
            notifyDataSetChanged();
        }
    });

    return (row);
    }    
}
}

【问题讨论】:

    标签: android


    【解决方案1】:

    在 onClickListener 中尝试以下代码

    youradapter.notifyDataSetChanged();

    【讨论】:

      【解决方案2】:

      在删除按钮 onClickListener 试试这个

      Names = ArrayUtils.removeElement(Names,Names[position]);
      Address = ArrayUtils.removeElement(Address,Address[position]);
      notifyDataSetChanged();
      

      虽然建议使用 OOP 概念(如类)来保存名称、地址和图像的数组。

      【讨论】:

        【解决方案3】:

        试试下面的代码:

        dltbutton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
        
        
                    Name = ArrayUtils.removeElement(Names, Names[position]);
                    Address = ArrayUtils.removeElement(Address, Address[position]);
                    imge = ArrayUtils.removeElement(imge, imge[position]);
                    notifyDataSetChanged();
                }
            });
        

        【讨论】:

          【解决方案4】:

          改变这个:

              ImageButton dltbutton = (ImageButton) findViewById(R.id.removebtn);
              dltbutton.setTag(position);
              dltbutton.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
          
                     Integer index = (Integer) view.getTag();
                      lvv.remove(index.intValue());  
                      notifyDataSetChanged();
          
              }
          });
          

          【讨论】:

            【解决方案5】:

            你是查克瓦尔人吗?我是查克瓦里安人。 StackOverflow 再次告诉我,我的答案不符合他们的质量要求。

            【讨论】:

              【解决方案6】:

              您需要更好地使用 Model 类,如下所示:-

              创建模型类 MyModel.class

              import java.util.ArrayList;
              import java.util.List;
              
              public class MyModel {
                  String Name, Address;
                  int image;
              
              
                  public MyModel(String name, String address, int image) {
                      Name = name;
                      Address = address;
                      this.image = image;
                  }
              
                  public String getName() {
                      return Name;
                  }
              
                  public String getAddress() {
                      return Address;
                  }
              
                  public int getImage() {
                      return image;
                  }
              }
              

              然后添加适配器类见下文:

              import android.app.Activity;
              import android.os.Bundle;
              import android.view.LayoutInflater;
              import android.view.View;
              import android.view.ViewGroup;
              import android.widget.BaseAdapter;
              
              import android.widget.ImageButton;
              import android.widget.ImageView;
              import android.widget.ListView;
              import android.widget.TextView;
              
              import java.util.ArrayList;
              
              import java.util.List;
              
              
              public class MainActivity extends Activity {
              
              
              ListView lv;
              
              String[] Names = {"Aqeel", "Ali", "Ansar", "Usama", "Farhad"};
               String[] Address = {"Chakwal", "Rawalpindi", "Islamabad", "Lahore",       
              "Multan"};
              int[] Images = {R.drawable.ic_menu_camera, R.drawable.ic_menu_gallery, R.drawable.ic_menu_manage,
              R.drawable.ic_menu_send, R.drawable.ic_menu_share};
              private List<MyModel> myModel=new ArrayList<>();
              private DataListAdapter dataListAdapter;
              
              
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.act_main);
                  lv = (ListView) findViewById(R.id.listview);
              
                  for(int i=0;i<Names.length;i++){
                      myModel.add( new MyModel(Names[i],Address[i],Images[i]));
                  }
                  dataListAdapter=new DataListAdapter(myModel);
                  lv.setAdapter(dataListAdapter);
              
              
              }
              
              class DataListAdapter extends BaseAdapter {
                 private List<MyModel> myModel=new ArrayList<>();
              
                  public DataListAdapter(List<MyModel> myModel) {
                    this.myModel=myModel;
              
                  }
              
                  public int getCount() {
              
                      return myModel.size();
                  }
              
                  public Object getItem(int arg0) {
              
                      return null;
                  }
              
                  public long getItemId(int position) {
              
                      return position;
                  }
              
                  public View getView(final int position, View convertView, final ViewGroup
                   parent) {
              
                      LayoutInflater inflater = getLayoutInflater();
                      final View row;
                      row = inflater.inflate(R.layout.randomevalue, parent, false);
                      final TextView Name, Addres;
                      ImageView imge;
                      Name = (TextView) row.findViewById(R.id.name);
                      Addres = (TextView) row.findViewById(R.id.adress);
                      imge = (ImageView) row.findViewById(R.id.img);
                      Name.setText(myModel.get(position).getName());
                      Addres.setText(myModel.get(position).getAddress());
                      imge.setImageResource(myModel.get(position).getImage());
              
                  ImageButton dltbutton = (ImageButton) row.findViewById(R.id.removebtn);
                  dltbutton.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
                          myModel.remove(position);
                          notifyDataSetChanged();
                      }
                  });
              
              
                  return (row);
                  }
              
              
              }
              
                  public void addMore(View v)
                  {
                      myModel.add( new MyModel("Mahesh","India",R.drawable.ic_menu_share));
                      dataListAdapter.notifyDataSetChanged();
              
                  }
              
              
              }
              

              public void addMore(View v)
                  {
                      myModel.add( new MyModel("Mahesh","India",R.drawable.ic_menu_share));
                      dataListAdapter.notifyDataSetChanged();
              
                  }
              

              android:onClick="addMore" 添加到您的添加按钮

              它会很好快乐编码:)

              【讨论】:

              • 谢谢你能告诉我如何在点击我在活动主 xml 文件中显示的添加按钮时从数组列表中获取随机值
              • 检查上面的最新代码更新,不要忘记点击向上箭头。
              • 对不起先生..我从列表视图中的插入数据中随机获取数据,我存储在 mymodel 类中...点击添加按钮
              【解决方案7】:
              @Override
              public boolean onContextItemSelected(MenuItem item) {
                  // TODO Auto-generated method stub
                  AdapterContextMenuInfo info =(AdapterContextMenuInfo) item.getMenuInfo();
                  pos=info.position;
                  deleteditem=myList.get(pos);
              
                  if(item.getTitle()=="Delete")
                  {   
                      String delete = myList.get(pos);
                      File f = new File(path + "/"+ delete);
              
                      if (f != null && f.exists()) 
                      {
                          f.delete();
                      }
              
                      myList.remove(pos); 
                      adapter. notifyDataSetChanged();
              
                      Toast.makeText(getApplicationContext(), "item has deleted",Toast.LENGTH_LONG).show();
              }
              

              【讨论】:

                猜你喜欢
                • 2013-09-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-04-21
                • 1970-01-01
                • 2021-07-08
                • 1970-01-01
                • 2012-07-18
                相关资源
                最近更新 更多