0

I have a listview that shows some information that I get from Github API, I set onItemClickListener for each item so I can display some information about them on alert dialog.

Before you flag this with Duplicate I want to say that none of these solution has worked for me How to get the value of a Listview item which is clicked in android?

I tried this:

call.enqueue(new Callback<List<GithubRepo>>() {
      @Override public void onResponse(@NonNull Call<List<GithubRepo>> call,
          Response<List<GithubRepo>> response) {

        if (response.isSuccessful()) {

          List<GithubRepo> repos = response.body();
          listViewRepo.setAdapter(new GitHubRepoAdapter(ReposActivity.this, repos));
        }
      }

listViewRepo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        String val = (String) parent.getItemAtPosition(position);

        AlertDialog.Builder adb = new AlertDialog.Builder(ReposActivity.this);

        adb.setMessage(val).show();
      }
    });  

But I get classCastException

java.lang.ClassCastException: us.egek.repofinderforgithub.GithubRepo cannot be cast to java.lang.String at us.egek.repofinderforgithub.ReposActivity$2.onItemClick(ReposActivity.java:63) at android.widget.AdapterView.performItemClick(AdapterView.java:299) at android.widget.AbsListView.performItemClick(AbsListView.java:1162) at android.widget.AbsListView$PerformClick.run(AbsListView.java:2953) at android.widget.AbsListView$3.run(AbsListView.java:3708) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:149) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method)

Adapter:

public class GitHubRepoAdapter extends ArrayAdapter<GithubRepo> {

  private Context context;
  private List<GithubRepo> values;

  public GitHubRepoAdapter(Context context, List<GithubRepo> values) {
    super(context, R.layout.list_item, values);

    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    if (row == null) {
      LayoutInflater inflater =
          (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.list_item, parent, false);
    }

    TextView textView = (TextView) row.findViewById(R.id.list_item_pagination_text);

    GithubRepo item = values.get(position);
    String message = item.getName();
    textView.setText(message);

    return row;
  }
}
Ege Kuzubasioglu
  • 5,991
  • 12
  • 49
  • 85

2 Answers2

0
String S = view.getContext().toString();
Sanjay Hadiya
  • 864
  • 9
  • 21
  • I'm not sure this answers the question. All you're doing is running `toString()` on the context associated with a view. What does this accomplish? Please edit your answer and elaborate. – Jon Jun 12 '17 at 17:55
0

Use this:

String val = (String) repos.get(position).getName();

Then you can use this value in your dialog

sumit
  • 1,047
  • 1
  • 10
  • 15