public class GithubTab extends Fragment implements AdapterView.OnItemClickListener {
ListView repoListView;
private ListAdapter adapter;
private List<RepositoryItem> repoListItems;
private List<String> repoNameList;
private List<String> userNameList;
private List<String> descriptionList;
private TextView tvData;
private static final String TAG = "Github Tab";
Button buttonHit;
TextView resultText;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.github_tab, container, false);
repoListView = (ListView) view.findViewById(R.id.repoList);
repoListItems = new ArrayList<>();
repoNameList = new ArrayList<>();
userNameList = new ArrayList<>();
descriptionList = new ArrayList<>();
adapter = new ListAdapter(getContext(), repoListItems);
repoListView.setAdapter(adapter);
tvData = (TextView) view.findViewById(R.id.tvJsonItem);
// Clickable: able to open the GitHub webpage of the re
repoListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "Clicked id" + view.getTag(), Toast.LENGTH_SHORT).show();
}
});
new JSONTask().execute("https://api.github.com/users/whyjay17/repos");
for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), "ddd"));
}
return view;
}
public class JSONTask extends AsyncTask<String, String, String> {
@Override
// Any non-UI thread process is running in this method. After completion, it sends the result to OnPostExecute
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
.... Code Hidden ....
return retreivedJson;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//cant close null
if (connection != null) {
// close both connection and the reader
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void formatJSONArray(String results){
try {
JSONArray jsonArray = new JSONArray(results);
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
if(jsonObject.optString("name") != null) {
//tvData.append(jsonObject.getString("name"));
repoNameList.add(jsonObject.getString("name"));
//Toast.makeText(getContext(), "1 " + repoNameList.get(1), Toast.LENGTH_SHORT).show();
}
if(jsonObject.optJSONObject("owner") != null){
JSONObject ownerObject=jsonObject.getJSONObject("owner");
if(ownerObject.optString("login")!=null) {
//tvData.append(ownerObject.getString("login"));
userNameList.add(ownerObject.getString("login"));
//ownerObject.append(ownerObject.getString("avatar_url"));
}
}
}
}catch (JSONException jsonException){
}
}
/*
* Called after the background computation finishes. Result of doInBackground is passed in as a parameter.
*
* */
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/* for JSONArray data*/
if(result!=null && !result.isEmpty()) {
formatJSONArray(result);
}
}
}
}
上面的代码基本上尝试从https://api.github.com/users/famous/repos解析JSON数据,将一些特定信息(repo name,id,description)添加到相应的列表中,并尝试在我创建的listView上显示该信息。
当我对信息进行硬编码时(意味着listView本身没有问题),listView可以正常工作,但是当我尝试将列表中的数据放入其中时(其中包含已解析的JSON信息并且我测试了它是实际上在列表中,它给了我一个空列表。
我该如何做到这一点?
答案 0 :(得分:3)
数据来自异步,因此在onCreateView()
内,列表数据可能还没有准备好添加到适配器。
您需要在onPostExecute()
方法之后将添加元素的代码移动到formatJSONArray()
,然后调用notifyDatasetChange()
使ListView无效
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/* for JSONArray data*/
if(result!=null && !result.isEmpty()) {
formatJSONArray(result);
for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i,
repoNameList.get(i), userNameList.get(i), "ddd"));
}
adapter.notifyDatasetChanged();
}
}
答案 1 :(得分:1)
for(int i = 0; i < repoNameList.size(); i++) {
repoListItems.add(new RepositoryItem(i,repoNameList.get(i),userNameList.get(i), "ddd"));
}
adapter.notifyDataSetChanged();
` 在
之前添加此行}catch (JSONException jsonException){
答案 2 :(得分:1)
您可以在 formatJSONArray 方法中调用adapter.notifiDatasetchange():
adapter = new ListAdapter(getContext(), repoListItems);
repoListView.setAdapter(adapter);
如果它不起作用,您可以在 formatJSONArray 方法中再次设置适配器
Private Sub cryptshift()
'SHIFTING ALL CHARACTERS IN ARRAY ONE SPACE TO THE LEFT
Dim temporaryStorageA As [String] = charArray(0)
Dim temporaryStorageB As [String]() = New [String](charArray.Length - 1) {}
For i As Integer = 1 To charArray.Length - 1
temporaryStorageB(i - 1) = charArray(i)
charArray(i - 1) = temporaryStorageB(i - 1)
Next
charArray(charArray.Length - 1) = temporaryStorageA
'CLEARING LABEL54 AND REALIGNING ARRAY TO LABEL53
Label54.Text = Nothing
For Each c In charArray
Label54.Text = Label54.Text & c & "-"
Next
'DECIPHERING
Dim mess As String = TextBox1.Text
Dim result As String = ""
For i As Integer = 0 To mess.Length - 1
Dim c As Char = mess(i)
Dim itemindex As Integer = Array.IndexOf(charArray2, c)
'**This IF Statement allows letters to be deciphered but also allows other characters such as punctuation, numbers and spaces to go through without any altering.**
If charArray2.Contains(c) Then
result &= charArray(itemindex)
Else
result &= c
End If
Next
TextBox2.Text = result
End Sub
它对我有用。我希望它可以帮助你解决问题!