ニュースフィードをシミュレートするRecyclerView
があります。 RecyclerView
の項目をクリックすると、この項目のテキストが別の TextView
に表示されます。ニュースフィード内の投稿のテキストはArrayList
に保存されます。私は多くの時間のためにArrayList
の値をFragment
のTextView
に渡すことを試みましたが、実際には表示されませんでした。ここに私のコードだ:ArrayListのとRecylcerViewArrayListから別のフラグメントに値を渡す方法
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.TextView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
public class Talkscreen extends AppCompatActivity {
public static LinearLayoutManager rvLayoutManagerTalkFragment;
public static RecyclerView recyclerViewTalkFragment;
public static RecyclerView.Adapter rvadapterTalkFragment;
public static ArrayList<String> postText;
public static String userName = "Benutzer 1";
public static String anzahlKommentare = "1.243";
public static String anzahlLikes = "15540";
public static String currentDateTimeString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_talkscreen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(), PostScreen.class);
startActivity(intent);
}
});
TextView meineaktivitaeten = (TextView) findViewById(R.id.meineAktivitaeten);
meineaktivitaeten.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), MeineAktivitaeten.class);
startActivity(intent);
}
});
postText = new ArrayList<>();
postText.addAll(Arrays.asList("TEXT 1",
"TEXT 2",
"TEXT 3",
"TEXT 4"));
currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
recyclerViewTalkFragment = (RecyclerView) findViewById(R.id.recycleviewTalkFragment);
rvLayoutManagerTalkFragment = new LinearLayoutManager(this);
recyclerViewTalkFragment.setLayoutManager(rvLayoutManagerTalkFragment);
rvadapterTalkFragment = new RvAdapterKlasseTalkFragment();
recyclerViewTalkFragment.setAdapter(rvadapterTalkFragment);
}
@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_talkscreen, 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.einstellungen) {
startActivity(new Intent(this, Einstellungen.class));
}
if (id == R.id.information) {
startActivity(new Intent(this, Information.class));
}
return super.onOptionsItemSelected(item);
}
}
ArrayListの値が
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class MeinePosts extends Fragment {
public static MeinePosts newInstance(){
MeinePosts fragment = new MeinePosts();
return fragment;
}
public MeinePosts() {
}
TextView postedText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_meine_posts, container, false);
postedText = (TextView) rootView.findViewById(R.id.postTextMeinePosts);
return rootView;
}
}
を示すことになっている断片と
RecyclerViewアダプタ
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
public class RvAdapterKlasseTalkFragment extends RecyclerView.Adapter<RvAdapterKlasseTalkFragment.ViewHolderKlasseTalkFragment>{
public static class ViewHolderKlasseTalkFragment extends RecyclerView.ViewHolder{
TextView userName;
TextView postText;
// IMAGE FÜR DEN POST FEHLT!!!!!!!!!
TextView zeit;
TextView anzahlKommentare;
TextView anzahlLikes;
ImageButton moreButton;
TextView loeschen;
public ViewHolderKlasseTalkFragment (View itemView){
super(itemView);
userName = (TextView) itemView.findViewById(R.id.userName);
postText = (TextView) itemView.findViewById(R.id.postText);
zeit = (TextView) itemView.findViewById(R.id.zeit);
anzahlKommentare = (TextView) itemView.findViewById(R.id.anzahlKommentare);
anzahlLikes = (TextView) itemView.findViewById(R.id.anzahlLikes);
moreButton = (ImageButton) itemView.findViewById(R.id.moreButton);
loeschen = (TextView) itemView.findViewById(R.id.loeschen);
}
}
@Override
public ViewHolderKlasseTalkFragment onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.iten_layout_post, null);
return new ViewHolderKlasseTalkFragment(itemView1);
}
@Override
public void onBindViewHolder(final ViewHolderKlasseTalkFragment viewHolderKlasse,final int i) {
viewHolderKlasse.userName.setText(TalkscreenFragment.userName);
viewHolderKlasse.userName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ProfilePopUp.class);
v.getContext().startActivity(intent);
}
});
viewHolderKlasse.postText.setText(TalkscreenFragment.postText.get(i));
viewHolderKlasse.zeit.setText(TalkscreenFragment.currentDateTimeString);
viewHolderKlasse.anzahlKommentare.setText(TalkscreenFragment.anzahlKommentare);
viewHolderKlasse.anzahlLikes.setText(TalkscreenFragment.anzahlLikes);
viewHolderKlasse.moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ContextMenuPopUp.class);
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return TalkscreenFragment.postText.size();
}
}
活動
ありがとうございました!
で
申し訳ありませんが、私はアンドロイドのプログラミングに比較的新しいです、あなたは私があなたが意味するものの例を教えてくれますか? – Ron
[link](https://www.youtube.com/watch?v=VyyGP_d0Ia8)この例をご覧ください。 –