2017-06-12 107 views
0

我有一個列表視圖,顯示我從Github API獲得的一些信息,我爲每個項目設置了onItemClickListener,因此我可以在警報對話框中顯示關於它們的一些信息。如何獲取ListView項目的值並將其顯示在AlertDialog上?

您舉報這個有重複之前,我想說的是,沒有這些解決方案已經爲我工作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) 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 )Android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:149) at android.app.ActivityThread.main(ActivityThread.java()) at android.app.ActivityThread.main :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) 在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; 
    } 
} 
+1

郵政完整的logcat使用此值。 –

+1

你如何設置你的listView與一個字符串數組或它是一個自定義列表 – sumit

+0

如果您使用的是自定義的適配器,那麼也發佈該類。 –

回答

0

使用此:

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

然後你就可以在對話框

+0

這一個工作後,我做了我的清單公開 –

-1
String S = view.getContext().toString(); 
+0

我不確定這是否回答此問題。你所做的只是在與視圖相關的上下文中運行'toString()'。這完成了什麼?請編輯你的答案並詳細說明。 – Jon

相關問題