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

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

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 關於RecyclerView新增項目
610





發文: 12
積分: 0
於 2019-03-30 14:48 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
小妹我在寫一個app練習已經建立好FloatingActionButton
也確認putExtra是
正確的 小妹我是打算可以利用putExtra傳送的資料
在MainActivity上的Recyclerview新增一條項目 可是傳送後沒有建立任何新東西
Logcat也沒有錯誤 請各位大老幫小妹我看看 感恩
因為FloatingActionButton沒有問題所以我就只貼幾個部分的內容
MainActivity
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
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList<Itemdata>  data;
    private MyAdapt myAdapter;
    String s,s1;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        data=new ArrayList<>();
        recyclerView=findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        myAdapter=new MyAdapt(R.layout.itemlayout,data);
        recyclerView.setAdapter(myAdapter);
        intent=getIntent();
        if(intent.hasExtra("addname")&intent.hasExtra("addnumber")) {
            initData();
        }
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Add new Contact?", Snackbar.LENGTH_LONG)
                        .setAction("Yes", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent=new Intent(MainActivity.this,AdditemActivity.class);
                                startActivity(intent);
                            }
                        }).show();
            }
        });
    }
    private void initData() {
        s=intent.getStringExtra("addname");
        s1=intent.getStringExtra("addnumber");
        myAdapter.addData(data.size(),s,s1);
        Log.v("countData:",""+data.size()+s+s1);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_delete) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MyAdapt
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
public class MyAdapt extends BaseQuickAdapter<Itemdata, BaseViewHolder> {
    ArrayList<Itemdata> data=new ArrayList<>();
    public MyAdapt(int layoutResId, @Nullable ArrayList<Itemdata> data) {
        super(layoutResId, data);
    }
    @Override
    protected void convert(BaseViewHolder helper, Itemdata item) {
        helper.addOnClickListener(R.id.recyclerview).setText(R.id.textname,item.getText())
                .setText(R.id.textnumber,item.getText1()).setChecked(R.id.ckeckdel,item.ischeck())
                .addOnClickListener(R.id.ckeckdel);
    }
    public void addData(int i,String s,String s1){
        Itemdata itemdata=new Itemdata();
        itemdata.setText(s);
        itemdata.setText1(s1);
        itemdata.setCheck(false);
        data.add(i,itemdata);
        notifyItemInserted(i);
    }
    public void removeData(int i){
        data.remove(i);
        notifyItemRemoved(i);
        notifyDataSetChanged();
    }
}

ItemData
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Itemdata {
    String s,s1;
    Boolean b;
    public String getText() {
        return s;
    }
    public void setText(String s){
        this.s=s;
    }
    public String getText1() {
        return s1;
    }
    public void setText1(String s1){
        this.s1=s1;
    }
    public boolean ischeck() {
        return b;
    }
    public void setCheck(boolean b){
        this.b=b;
    }
}

itemlayout.xml
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="120dp"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/textname"
        android:layout_width="120dp"
        android:layout_height="match_parent"
        android:text="a"
        android:textAlignment="gravity"
        android:gravity="center_vertical|center_horizontal"
        android:singleLine="true"/>
    <TextView
        android:id="@+id/textnumber"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:text="b"
        android:textAlignment="gravity"
        android:gravity="center_vertical|center_horizontal"
        android:singleLine="true"/>
    <CheckBox
        android:id="@+id/ckeckdel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|center_vertical"
        android:visibility="invisible"/>
</LinearLayout>

content_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>


reply to postreply to post
作者 Re:關於RecyclerView新增項目 [Re:610]
kkk12955





發文: 2
積分: 0
於 2019-06-03 17:35 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
https://github.com/CymChad/BaseRecyclerViewAdapterHelper/issues/2670

上面是參考文章

主要是套件問題 如果建構時沒有資料就會無法顯示
可以的使用原生或是其它套件


reply to postreply to post
作者 Re:關於RecyclerView新增項目 [Re:610]
kkk12955





發文: 2
積分: 0
於 2019-06-03 18:41 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
接觸安卓開發滿一年 可能不是理解很全面

我不太清楚整體的需求 以下是我認為正確的 有任何問題可以在提出 Smile

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
public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList<Itemdata> data = new ArrayList<>();
    private MyAdapt myAdapter;
    String s,s1;
    private Intent intent;
    private FloatingActionButton fab;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        initialView();
        setListener();
    }
 
    private void initialView(){
        fab = findViewById(R.id.fab);
        recyclerView=findViewById(R.id.recyclerview);
        myAdapter=new MyAdapt(R.layout.itemlayout,data);
        myAdapter.setHeaderAndEmpty(true);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(myAdapter);
        intent = getIntent();
 
        if(intent.hasExtra("addname")&intent.hasExtra("addnumber")) {
            getExtraData();
        }
 
    }
 
    private void setListener(){
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳轉
                
            }
        });
 
    }
    
    private void getExtraData(){
        s=intent.getStringExtra("addname");
        s1=intent.getStringExtra("addnumber");
        Itemdata itemdata=new Itemdata();
        itemdata.setText(s);
        itemdata.setText1(s1);
        itemdata.setCheck(false);
        data.add(itemdata);
        myAdapter.notifyDataSetChanged();
        Log.v("countData:",""+data.size()+s+s1);
    }
}


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

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8