EditText在RecyclerView Adapter中不起作用。随机数据显示在设备上

时间:2016-04-19 19:45:44

标签: android

我正在制作一个将电影名称作为用户输入的应用程序,在输入之后将显示一个新屏幕,显示电影的列表项目,该电影的列表显示在" http://www.omdbapi.com/& #34;就像IMDB一样。在点击任何列表项目(电影名称)时,该电影的详细信息将显示在另一个屏幕中,一切正常,然后才能获得输入,我之前硬编码电影名称,但现在我希望用户输入inout但我认为有'编辑文本中的问题我无法从用户那里获得输入。

MainActivity Class

public class MainActivity extends AppCompatActivity {
private static String LOG_TAG = MainActivity.class.getSimpleName();
private EditText editText;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            validateInput();
        }
    });

}

private void validateInput() {
    String input = editText.getText().toString();

    if (input != null || !input.equals("")) {
        Intent intent = new Intent(this, MovieFragment.class);
        intent.putExtra(Intent.EXTRA_TEXT, input);
        startActivity(intent);
    }


    setContentView(R.layout.activity_main);
    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) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@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_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

MovieFragment Class

public class MovieFragment extends Fragment implements OnNetworkCallHandled, RecyclerViewAdapter.OnClickListener {
private String movieName;
private Button button;
private RecyclerViewAdapter adapter;
private RecyclerView recyclerView;
private EditText editText;

private DataWrapper list;

public MovieFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Intent intent = getActivity().getIntent();
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);

    if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
        movieName = intent.getStringExtra(Intent.EXTRA_TEXT);
        ((TextView) rootView.findViewById(R.id.detail_text)).setText(movieName);
    }
    getMovieDetails();
    return rootView;

}

private void getMovieDetails() {
    MovieTask movieAsyncTask = new MovieTask(this, movieName);
    movieAsyncTask.execute();
}

@Override
public void onNetworkcallSuccess(Object object) {
    System.out.println("On success");
    String result = (String) object;
    Gson gson = new Gson();
    list = gson.fromJson(result, DataWrapper.class);
    adapter = new RecyclerViewAdapter(list.getSearch(), getContext(), this);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(adapter);
    System.out.println("On success");
}

@Override
public void onNetworkcallFailure(Object object) {
    System.out.println("On failure");
}

@Override
public void onClick(View view, int position, long itemId) {
    startActivity(position);
    System.out.println();
}

private void startActivity(int position) {
    Intent intent = new Intent(getActivity(), DetailActivity.class);
    Bundle extras = new Bundle();
    extras.putString("Title",list.getSearch().get(position).getTitle());
    extras.putString("Year", list.getSearch().get(position).getYear());
    intent.putExtras(extras);
    startActivity(intent);
}

}

DetailActivity Class

public class DetailActivity extends AppCompatActivity {
private TextView Title;
private TextView Year;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();


    Title = (TextView) findViewById(R.id.Title);
    Year = (TextView) findViewById(R.id.Year);

    if (intent != null) {
        String title = extras.getString("Title");
        String year=extras.getString("Year");
        Title.setText(title);
        Year.setText(year);
        System.out.println();
    }
}

@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;
}

}

MovieTask扩展AsyncTask类

public class MovieTask extends AsyncTask<Void, Void, String> {
private String LOG_TAG = MovieTask.class.getSimpleName();
private String response = "";
private String movieName;
private OnNetworkCallHandled onNetworkCallHandled;


public MovieTask(OnNetworkCallHandled onNetworkCallHandled, String movieName) {
    this.onNetworkCallHandled = onNetworkCallHandled;
    this.movieName = movieName;


}

@Override
protected String doInBackground(Void... params) {
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String MovieJsonStr = null;


    try {
        final String MOVIE_BASE_URL = "http://www.omdbapi.com/?";
        final String s = "s";
        final String plot = "plot";
        final String r = "r";
        final String page = "page";

        Uri builtUri = Uri.parse(MOVIE_BASE_URL).buildUpon()
                .appendQueryParameter(s, movieName)
                .appendQueryParameter(plot, "full")
                .appendQueryParameter(r, "json")
                .appendQueryParameter(page, "1")
                .build();

        URL url = new URL(builtUri.toString());

        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();
        if (inputStream == null) {
            return null;
        }
        reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line + "\n");
        }

        if (buffer.length() == 0) {
            return null;
        }
        response = buffer.toString();

    } catch (Exception e) {

    }

    return response;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if (result != null || !result.equals("")) {
        onNetworkCallHandled.onNetworkcallSuccess(result);

    } else {
        onNetworkCallHandled.onNetworkcallFailure(true);
    }
}

}

RecyclerViewAdapter类

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MovieDetailsHolder> {

private ArrayList<MovieDetails> movieDetails;
private Context context;
private OnClickListener onClick;

RecyclerViewAdapter(ArrayList<MovieDetails> movieDetails, Context context, OnClickListener onClick) {
    this.onClick = onClick;
    this.movieDetails = movieDetails;
    this.context = context;
}

@Override
public MovieDetailsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
    return new MovieDetailsHolder(view);
}

@Override
public void onBindViewHolder(MovieDetailsHolder holder, final int position) {
    holder.tVMovieName.setText(movieDetails.get(position).getTitle());
    Picasso
            .with(context)
            .load(movieDetails.get(position).getPoster())
            .into(holder.iVPoster);

    holder.setOnClickListener(onClick);
}

@Override
public int getItemCount() {
    return movieDetails.size();
}

public static class MovieDetailsHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private OnClickListener onClickListener;

    CardView cv;
    TextView tVMovieName;
    ImageView iVPoster;

    MovieDetailsHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cardView);
        tVMovieName = (TextView) itemView.findViewById(R.id.tVMovieName);
        iVPoster = (ImageView) itemView.findViewById(R.id.iVPoster);

        itemView.setOnClickListener(this);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        onClickListener.onClick(v, getPosition(), getItemId());
    }
}

public interface OnClickListener {
    public void onClick(View view, int position, long itemId);
}

}

1 个答案:

答案 0 :(得分:0)

您将MovieFragment视为活动

        Intent intent = new Intent(this, MovieFragment.class);
        intent.putExtra(Intent.EXTRA_TEXT, input);
        startActivity(intent);

但是MovieFragment是一个片段。您需要在活动中添加/替换MovieFragment

此外,setContentView(R.layout.activity_main);

中无需再次validateInput()