如何获取listview项的值并将其显示在AlertDialog上?

时间:2017-06-12 15:32:51

标签: android listview

我有一个列表视图,显示了我从Github API获得的一些信息,我为每个项目设置了onItemClickListener,因此我可以在警告对话框中显示有关它们的一些信息。

在你用Duplicate标记之前我想说这些解决方案都不适用于我How to get the value of a Listview item which is clicked in android?

我试过了:

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();
      }
    });  

但我得到classCastException

  

java.lang.ClassCastException:us.egek.repofinderforgithub.GithubRepo   无法强制转换为java.lang.String                                                                                    在   us.egek.repofinderforgithub.ReposActivity $ 2.onItemClick(ReposActivity.java:63)                                                                                    在android.widget.AdapterView.performItemClick(AdapterView.java:299)                                                                                    在android.widget.AbsListView.performItemClick(AbsListView.java:1162)                                                                                    在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2953)                                                                                    在android.widget.AbsListView $ 3.run(AbsListView.java:3708)                                                                                    在android.os.Handler.handleCallback(Handler.java:733)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:149)                                                                                    在android.app.ActivityThread.main(ActivityThread.java:5257)                                                                                    at java.lang.reflect.Method.invokeNative(Native Method)                                                                                    在java.lang.reflect.Method.invoke(Method.java:515)                                                                                    在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                                    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)                                                                                    在dalvik.system.NativeStart.main(本地方法)

适配器:

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

2 个答案:

答案 0 :(得分:0)

String S = view.getContext().toString();

答案 1 :(得分:0)

使用此:

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

然后您可以在对话框中使用此值