JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java ME、Google Android 平台與 JavaCard 討論區 » Android  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 ListView無法刪除指定的那行
CookieLee





發文: 1
積分: 0
於 2016-09-06 02:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟是個菜鳥,第一次發文問問題,麻煩大神相助~

編譯通過可以執行,就不提供logcat了

問題如下
我把自訂的layout : classname.xml 要灌到listview裡面
classname只有兩個TextView元件,分別是 name(作為listview的item標題) & delete(作為刪除指定那行item的按鈕)
因為delete沒辦法刪除指定那行,所以我又多加了一個listview的setOnItemLongClickListener來試試看
delete和長按 兩種都用position做指標
但是實際使用卻都會刪除到最新add的item
請問各位大神,是不是程式碼哪邊出了問題呢?

PS.原本delete是用Button,改TestView是因為灌進listview之後長按沒辦法使用

程式碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public class TestClassName extends Activity {
  private Button newone, nextpage;
  private ListView mListView;
  private MyAdapter mAdapter;
  private ArrayList<Object> mList;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testclassname);
 
    findView();
    setAction();
    mAdapter = new MyAdapter();
    mListView.setAdapter(mAdapter);
  }
 
  private class MyAdapter extends BaseAdapter {
    private ArrayList<Integer> mList;
 
    public MyAdapter() {
      mList = new ArrayList<>();
    }
 
    public void addItem(Integer i) {
      mList.add(i);
    }
 
    public void removeItem(int index) {
      mList.remove(index);
    }
 
    @Override
    public int getCount() {
      return mList.size();
    }
 
    @Override
    public Object getItem(int position) {
      return null;
    }
 
    @Override
    public long getItemId(int position) {
      return 0;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      final int index = position;
      View v = convertView;
      Holder holder;
      if (v == null) {
        v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.classname, null);
        holder = new Holder();
        holder.name = (TextView) v.findViewById(R.id.Name);
        holder.delete = (TextView) v.findViewById(R.id.delete);
        v.setTag(holder);
      } else {
        holder = (Holder) v.getTag();
      }
      holder.name.setOnClickListener(nameClickHandler);
      holder.delete.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          mAdapter.removeItem(index);
          mAdapter.notifyDataSetChanged();
        }
      });
      return v;
    }
 
    class Holder {
 
      public TextView delete;
      public LinearLayout layout;
      TextView name;
    }
  }
 
  private OnClickListener nameClickHandler = new OnClickListener() {
    @Override
    public void onClick(View v) {
      final TextView delBtn = (TextView) v;
      final int id = delBtn.getId();
      String sn = delBtn.getText().toString();
      final EditText editname = new EditText(TestClassName.this);
      editname.setText(sn);
      new AlertDialog.Builder(TestClassName.this).setView(editname)
 
          .setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              String Edit = editname.getText().toString();
              delBtn.setText(Edit);
            }
          }).setNegativeButton("取消", null).setTitle("輸入班別名稱").show();
    }
  };
 
  private void findView() {
    newone = (Button) findViewById(R.id.newone);
    nextpage = (Button) findViewById(R.id.nextpage);
    mListView = (ListView) findViewById(R.id.list);
  }
 
  private void setAction() {
    mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        new AlertDialog.Builder(TestClassName.this).setMessage("確定要刪除?")
            .setPositiveButton("確定", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                mAdapter.removeItem(position);
                mAdapter.notifyDataSetChanged();
              }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
 
              }
            }).show();
        return false;
      }
    });
 
    nextpage.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(TestClassName.this, CalendarPic.class);
        startActivity(intent);
        finish();
      }
    });
 
    newone.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        mAdapter.addItem(mAdapter.getCount() + 1);
        mAdapter.notifyDataSetChanged();
      }
    });
  }
}


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
687 ListView無法刪除指定的那行 CookieLee 4316 2016-09-06 02:43
583 Re:ListView無法刪除指定的那行 lnmlee 131 2016-09-06 14:26
» JWorld@TW »  Java ME、Google Android 平台與 JavaCard 討論區 » Android

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8