2016-07-21 6 views
0

私は現在、mysqlデータベースからデータを取得し、生のJSON形式で表示するアプリケーションを持っています。私は現在、このデータをString変数にプッシュし、それを特定のアクティビティのListviewに表示しています。 問題は、このデータを表示しようとすると、私のリストビューにデータが入力されていないことです。私は変数が空ではないと確信していますifステートメントがこれをキャプチャしています。ここでAndroid - リストビュー内のアダプタからのデータを表示

はMainActivityコードの抜粋です:

//Methods to grab information from abhandym_DB database 
public void getJSON(View view){ 
new BackgroundTask().execute(); 
} 

public void parseJSON(View view){ 
if(JSON_String==null){ 
    Toast.makeText(getApplicationContext(), "First Get Json", Toast.LENGTH_LONG).show(); 
}else{ 
    Intent intent = new Intent(this,Test.class); 
    intent.putExtra("JSON_Data",JSON_String); 
    startActivity(intent); 
} 
} 

class BackgroundTask extends AsyncTask<Void,Void,String>{ 

    String json_url; 

    @Override 
    protected void onPreExecute() { 
     json_url = "http://abhandyman.x10host.com/json_get_data.php"; 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     try { 
      URL url = new URL(json_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); 
      InputStream inputSteam = httpURLConnection.getInputStream(); 
      BufferedReader buffereredReader = new BufferedReader(new InputStreamReader(inputSteam)); 
      StringBuilder stringBuilder = new StringBuilder(); 
      while((JSON_String = buffereredReader.readLine())!=null){ 
      stringBuilder.append(JSON_String+"\n"); 
      } 
      buffereredReader.close(); 
      inputSteam.close(); 
      httpURLConnection.disconnect(); 
      return stringBuilder.toString().trim(); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     TextView textView = (TextView)findViewById(R.id.fragment1_textview_JSONAPPEAR); 
     textView.setText(result); 
     JSON_String = result; 
    } 
} 

ここにここに私のTest.java

public class Test extends AppCompatActivity { 
String JSON_String; 
JSONObject jsonObject; 
JSONArray jsonArray; 
DataAdapter dataAdapter; 
ListView listView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test_layout); 
    listView = (ListView)findViewById(R.id.test_listView); 
    dataAdapter = new DataAdapter(this, R.layout.row_layout); 
    listView.setAdapter(dataAdapter); 
    JSON_String = getIntent().getExtras().getString("JSON_Data"); 
    try { 
     jsonObject = new JSONObject(JSON_String); 
     jsonArray = jsonObject.getJSONArray("server_response"); 
     int count = 0; 
     String jobid,problem,resolution; 
     while(count<jsonObject.length()){ 
      JSONObject JO = jsonArray.getJSONObject(count); 
      jobid = JO.getString("jobid"); 
      problem = JO.getString("problem"); 
      resolution = JO.getString("resolution"); 
      Data data = new Data(jobid,problem,resolution); 
      dataAdapter.add(data); 
      count++; 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

}

のためのコードは私のDataAdapterのコードです:

public class DataAdapter extends ArrayAdapter{ 
List list = new ArrayList(); 
public DataAdapter(Context context, int resource) { 
    super(context, resource); 
} 

public void add(Data object) { 
    super.add(object); 
    list.add(object); 
} 

@Override 
public int getCount() { 
    return list.size(); 
} 

@Override 
public Object getItem(int position) { 
    return list.get(position); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row; 
    row = convertView; 
    DataHolder dataHolder; 
    if(row == null){ 
     LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = layoutInflater.inflate(R.layout.row_layout,parent,false); 
     dataHolder = new DataHolder(); 
     dataHolder.tx_jobid = (TextView) row.findViewById(R.id.tx_jobid); 
     dataHolder.tx_problem = (TextView) row.findViewById(R.id.tx_problem); 
     dataHolder.tx_resolution = (TextView) row.findViewById(R.id.tx_resolution); 
     row.setTag(dataHolder); 
    }else{ 
    dataHolder = (DataHolder)row.getTag(); 
    } 
    Data data = (Data)this.getItem(position); 
    dataHolder.tx_jobid.setText(data.getJobid()); 
    dataHolder.tx_problem.setText(data.getProblem()); 
    dataHolder.tx_resolution.setText(data.getResolution()); 
    return row; 
} 

static class DataHolder{ 
TextView tx_jobid,tx_problem,tx_resolution; 
} 

}

「Parse JSON」ボタンをクリックすると表示される内容です。

listView empty after population

任意のヘルプまたはそのがはるかに高く評価されるだろう表示されない理由について助言!

ありがとうございます!

+0

マッピングされたただ一つの名前/値を持つ '()'アダプタに追加します。 'this.notifyDatasetChanged()' –

+0

それは動作するように見えました! 100%ではなく、私のデータベースに4つのデータエントリのうちの1つだけを表示します。 JSONの生データを取得すると、内部に1つ以上のエントリがはっきりと表示されます。何か案は?あなたの投稿に感謝します。Adil –

+0

常に最初の値を表示しますか?または最後ですか? jsonObject.length()が正しい値を返すことを確認しましたか? –

答えて

0

それにデータを追加する前に、あなたはリストビューにアダプタを設定していると思うあなたの問題はここにあると思われる:

while(count<jsonObject.length()){ 

あなたは配列の要素数を使用してループするが、使用していませんマッピングされたキーの数:1である値オブジェクト(「server_response」)、あなたはこの行を変更する必要があります。

while(count<jsonArray.length()){ 

最初の要素が表示されているのは jsonObject.length()なので、要素が1つしかないので 1が返されるため、

が表示されます。 DOC、JSONObject、長さ()メソッドから

は、このオブジェクトの名前/値のマッピングの数を返します。

とあなたのケースで

はあなたの方法では( "server_response":[array items...]

+0

ありがとう、これは私の問題を解決しました! –

+0

あなたは歓迎されて、喜んで助けました。 –

0

Test.javaをチェックインします。私は

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test_layout); 
     listView = (ListView)findViewById(R.id.test_listView); 
     dataAdapter = new DataAdapter(this, R.layout.row_layout); 

     JSON_String = getIntent().getExtras().getString("JSON_Data"); 
     try { 
      jsonObject = new JSONObject(JSON_String); 
      jsonArray = jsonObject.getJSONArray("server_response"); 
      int count = 0; 
      String jobid,problem,resolution; 
      while(count<jsonObject.length()){ 
       JSONObject JO = jsonArray.getJSONObject(count); 
       jobid = JO.getString("jobid"); 
       problem = JO.getString("problem"); 
       resolution = JO.getString("resolution"); 
       Data data = new Data(jobid,problem,resolution); 
       dataAdapter.add(data); 
       count++; 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     listView.setAdapter(dataAdapter); //change effected 
    } 
+0

それは違いを生じさせていないようです:( –

関連する問題