2017-01-06 2 views
1

私は基本的なタスク管理アプリケーションを作成しています。私はListViewで動作するためにベースアダプタを使用しています。私はリストからチェックされた値を(いくつか)削除することができますか?また、長いクリックで1つの値だけを削除する方法はありますか? これは私のアダプターと一緒に使えますか?私はいくつかの選択肢を試しましたが、何も動かなかった ここに私のコードです。BaseAdapterで構築されたListViewからいくつかの値を削除します。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <Button 
     android:id="@+id/btn1" 
     android:layout_width="50dp" 
     android:layout_height="40dp" 
     android:layout_alignParentRight="true" 
     android:text="\u2713" 
     android:background="@drawable/add_btn" 
     android:onClick="knopka2"/> 
    <EditText 
     android:id="@+id/input" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/btn1" 
     android:hint="put your task here"/> 
    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" 
     android:divider="@drawable/deriver" 
     android:layout_below="@+id/input" 
     android:layout_centerHorizontal="true" 
     android:choiceMode="multipleChoice" /> 
    <TextView 
     android:id="@+id/ttl" 
     android:layout_below="@id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/tryClear" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/ttl" 
     android:onClick="tryClear" 
     /> 
</RelativeLayout> 

これが私の活動です:

public class MyAdapter extends BaseAdapter { 
    private final Context context;//Context for view creation 
    private final String[] data;//raw data 
    public MyAdapter(String[] data, Context context){ 
     this.data=data; 
     this.context=context; 
    } 
    //how many views to create 
    public int getCount() { 
     return data.length; 
    } 
    //item by index (position) 
    public Object getItem(int i) { 
     return data[i]; 
    } 
    //ID => index of given item 
    public long getItemId(int i) { 
     return i; 
    } 
    //called .getCount() times - for each View of item in data 
    public View getView(int i, View recycleView, ViewGroup parent) { 
     if(recycleView==null)recycleView = LayoutInflater.from(context).inflate(R.layout.item,null); 
     ((TextView)recycleView).setText(data[i]);//show relevant text in reused view 
     return recycleView; 
    } 
} 

そして、これは、リストビュー内の項目のように見える方法です:

public class Trird extends AppCompatActivity { 
     Button btn; 
     EditText input4; 
     TextView ttl; 
     ListView list; 
     public static ArrayList<String> taskList = new ArrayList<String>(); 
     SparseBooleanArray sparseBooleanArray; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.third); 
      input4 = (EditText) findViewById(R.id.input); 
      btn = (Button) findViewById(R.id.btn1); 
      list = (ListView) findViewById(R.id.listView); 
      ttl = (TextView)findViewById(R.id.ttl); 

     } 

     public String[] putToArray() { 
      String ag = input4.getText().toString().trim(); 
      if (ag.length() != 0) { 
       taskList.add(ag); 
       input4.setText(""); 
      } 
      String[] abc = taskList.toArray(new String[taskList.size()]); 
      return abc; 
     } 

     public void knopka2(View v) { 
      final String[] ListViewItems = putToArray(); // 
      list = (ListView) findViewById(R.id.listView); 
      list.setAdapter(new MyAdapter(ListViewItems, this)); 

      list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View tv, int i, long id) { 
        ///tv.setBackgroundColor(Color.YELLOW); 
        ///ttl.setText("selected: " + list.getAdapter().getItem(i)); 
        sparseBooleanArray = list.getCheckedItemPositions(); 

        String ValueHolder = ""; 
        int a = 0; 
        while (a < sparseBooleanArray.size()) { 
         if (sparseBooleanArray.valueAt(a)) { 
          ValueHolder += ListViewItems[sparseBooleanArray.keyAt(a)] + ","; 
         } 
         a++; 
        } 
        ValueHolder = ValueHolder.replaceAll("(,)*$", ""); 
        Toast.makeText(Trird.this, "ListView Selected Values = " + ValueHolder, Toast.LENGTH_LONG).show(); 

        if(ValueHolder!=null){ 
         taskList.remove(ValueHolder); 
        } 
       } 
      }); 
     } 

    } 

これが私のアダプタです。

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="24sp" 
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 
    /> 

答えて

0

問題は、リストビュー削除Methodeのを使用してアイテムを削除しようとしているということです。

アダプタのremove()メソッドを使用して、選択したアイテムをリストから削除するだけです。そうする

可能な方法は次のようになります。

CheckedTextView checkedText= baseAdapter.getItem([POSITION OF THE SELECTED ITEM]); 
baseAdapter.remove(checkedText); 

baseAdapter.notifyDataSetChanged(); 

希望ボタンのクリックの内側にこのコードを入れて、そこに行きます。

+0

お返事ありがとうございます。私は同様のものを試しました。しかし、これまでは、「非静的メソッドは静的コンテキストから参照できません」というメッセージが表示されました。 – KeitL

関連する問題