如何显示从ActivityMain到Realm的列表?如何在Main2Activity上使用EditText添加Realm中的新播放器?
P.S。 是否需要创建一个类App扩展Application? 我花了4天没有结果
Class MainActivity:在这里我想在Realm中添加List播放器 ,这是默认设置。
package com.first.anew;
import android.content.Intent;
import android.graphics.Canvas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.Menu;
import android.view.MenuItem;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import io.realm.Realm;
import pl.fanfatal.swipecontrollerdemo.R;
public class MainActivity extends AppCompatActivity {
private PlayersDataAdapter mAdapter;
SwipeController swipeController = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setPlayersDataAdapter();
setupRecyclerView();
}
private void setPlayersDataAdapter() {
List<Player> players = new ArrayList<>();
Intent i = getIntent();
String text = i.getStringExtra ( "TextBox" );
if (text != null) {
Player pl = new Player();
pl.setName(text);
players.add(players.size(), pl);
}
try {
InputStreamReader is = new InputStreamReader(getAssets().open("players.csv"));
BufferedReader reader = new BufferedReader(is);
reader.readLine();
String line;
String[] st;
while ((line = reader.readLine()) != null) {
st = line.split(",");
Player player = new Player();
player.setName(st[0]);
players.add(player);
}
} catch (IOException e) {
}
mAdapter = new PlayersDataAdapter(players);
}
private void setupRecyclerView() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(mAdapter);
swipeController = new SwipeController(new SwipeControllerActions() {
@Override
public void onRightClicked(int position) {
// mAdapter.players.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyItemRangeChanged(position, mAdapter.getItemCount());
}
});
ItemTouchHelper itemTouchhelper = new ItemTouchHelper(swipeController);
itemTouchhelper.attachToRecyclerView(recyclerView);
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
swipeController.onDraw(c);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_cat1) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}
Main2Activity类:在这里,我使用EditText在Realm中添加了新播放器(列表播放器)
package com.first.anew;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import io.realm.Realm;
import pl.fanfatal.swipecontrollerdemo.R;
public class Main2Activity extends AppCompatActivity {
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
addListenerOnButton();
}
public void addListenerOnButton() {
editText = (EditText) findViewById(R.id.edit_text);
button = (Button) findViewById(R.id.b1);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent ( Main2Activity.this, MainActivity.class );
intent.putExtra ( "TextBox", editText.getText().toString() );
startActivity(intent);
}
}
);
}
}
类播放器
package com.first.anew;
import io.realm.RealmObject;
public class Player extends RealmObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
PlayersDataAdapter类:没有领域的适配器
package com.first.anew;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import pl.fanfatal.swipecontrollerdemo.R;
class PlayersDataAdapter extends RecyclerView.Adapter<PlayersDataAdapter.PlayerViewHolder> {
public List<Player> players;
public class PlayerViewHolder extends RecyclerView.ViewHolder {
private TextView name;
public PlayerViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
}
}
public PlayersDataAdapter(List<Player> players) {
this.players = players;
}
@Override
public PlayerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_view, parent, false);
return new PlayerViewHolder(itemView);
}
@Override
public void onBindViewHolder(PlayerViewHolder holder, int position) {
Player player = players.get(position);
holder.name.setText(player.getName());
}
@Override
public int getItemCount() {
return players.size();
}
}