2016-10-03 10 views
0

私はナビゲーションドロワーの操作を使用しています。私はURLからデータを取得した後、カスタマイズされたリストを表示したい。ここに私のナビゲーション引き出し活動があります。Androidとカスタムリストビューでの解析

たとえば、ユーザーがナビゲーションドロワの[割り当て]タブをクリックすると、URLのデータを表示するカスタマイズされたリストビューで割り当てアクティビティが表示されます。

@Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
    if (id == R.id.nav_dashboard) { 
    switch (item.getItemId()) {case R.id.nav_dashboard: 
    startActivity(new Intent(this, AdminDashBoardActivity.class)); 
        // Handle the camera action 
      } } 
else if (id == R.id.nav_allocations) { 
    switch (item.getItemId()) {case R.id.nav_allocations: 
       startActivity(new Intent(this, 

     AdminAllocationActivity.class)); 
       break; 
       // Handle the camera action 
      } 

     } else if (id == R.id.nav_courseoutline) { 

      switch (item.getItemId()) { 
       case R.id.nav_courseoutline: 
        startActivity(new Intent(this, CourseOutlineActivity.class)); } 
     } else if (id == R.id.nav_ranklist) { 
       switch (item.getItemId()) {case R.id.nav_ranklist: 
        startActivity(new Intent(this,AllocatedTrainerActivity .class)); 
     }} 
// else if (id == R.id.nav_send) { 
// 
//  } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

とき割り当て]タブの[ユーザー]をクリックしアロケーション活動オープンでカスタマイズされたリストに解析されたデータの結果を示すが、それは起きていません。私を案内してください。私の割り当て活動はここにあります。

package vu.bc110201891.btg.AdminActivities; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ListView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import vu.bc110201891.btg.Adapters.AdminAllocationAdapter; 
import vu.bc110201891.btg.AsyncTasks.AdminAllocationTask; 
import vu.bc110201891.btg.Models.AdminAllocation; 
import vu.bc110201891.btg.R; 

public class AdminAllocationActivity extends AppCompatActivity { 
    String json_string; 
    JSONObject jsonObject; 
    JSONArray jsonArray; 
    ListView listView; 
    AdminAllocationAdapter contactAdapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_admin_allocation); 

     listView= (ListView) findViewById(R.id.listView); 

     contactAdapter=new AdminAllocationAdapter(this,R.layout.activity_admin_allocation); 
     listView.setAdapter(contactAdapter); 

     json_string=getIntent().getExtras().getString("json_data"); 
     try { 
      jsonObject=new JSONObject(json_string); 
      jsonArray=jsonObject.getJSONArray("courseDataSet"); 

      int count=0; 
      String name,email,moblile; 

      while (count<jsonArray.length()){ 


       JSONObject JO=jsonArray.getJSONObject(count); 
       name=JO.getString("id"); 
       email= JO.getString("company_id"); 
       moblile=JO.getString("user_id"); 
       AdminAllocation contacts=new AdminAllocation(name,email,moblile); 
       contactAdapter.add(contacts); 
       count++; 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

ここは私のアドバイスです。

package vu.bc110201891.btg.Adapters; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

import vu.bc110201891.btg.Models.AdminAllocation; 
import vu.bc110201891.btg.R; 

/** 
* Created by bc120402700 on 9/23/2016. 
*/ 
public class AdminAllocationAdapter extends ArrayAdapter { 




    List list=new ArrayList(); 
    public AdminAllocationAdapter(Context context, int resource) { 
     super(context, resource); 
    } 



    public void add(AdminAllocation 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; 
     AllocationHolder contactHolder; 
     if (row==null){ 


      LayoutInflater layoutInflater= (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row=layoutInflater.inflate(R.layout.row_allocation,parent,false); 
      contactHolder=new AllocationHolder(); 
      contactHolder.tx_name= (TextView) row.findViewById(R.id.textView2); 
      contactHolder.tx_email= (TextView) row.findViewById(R.id.textView3); 
//   contactHolder.tx_mobile= (TextView) row.findViewById(R.id.tx_mobile); 
      row.setTag(contactHolder); 
     } 

     else { 

      contactHolder=(AllocationHolder)row.getTag(); 


     } 
     AdminAllocation contacts= (AdminAllocation) this.getItem(position); 
     contactHolder.tx_name.setText(contacts.getName()); 
     contactHolder.tx_email.setText(contacts.getEmail()); 
     contactHolder.tx_mobile.setText(contacts.getMobile()); 
     return row; 
    } 
static class AllocationHolder{ 
TextView tx_name,tx_email,tx_mobile; 
} 
} 

ここは私のasynタスクです。

package vu.bc110201891.btg.AsyncTasks; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.widget.Button; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import vu.bc110201891.btg.R; 

/** 
* Created by bc120402700 on 9/23/2016. 
*/ 
public class AdminAllocationTask extends AsyncTask<Void, Void, String> { 
    String json_url; 
@Override 
    protected void onPreExecute() { 
     json_url = "http://mantis.vu.edu.pk/bridging_the_gap/public/AllocateAdminService"; 
    } 
    String JSON_STRING; 
    @Override 
    protected String doInBackground(Void... voids) { 
try { 
URL url = new URL(json_url); 
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      InputStream inputStream = httpURLConnection.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuilder stringBuilder = new StringBuilder(); 

      while ((JSON_STRING = bufferedReader.readLine()) != null) { 

       stringBuilder.append(JSON_STRING + "\n"); 

      } 
      bufferedReader.close(); 
      inputStream.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) { 
super.onPostExecute(result);} 
} 

これは私のモデルクラスです。リストビューへ

package vu.bc110201891.btg.Models; 
public class AdminAllocation { 
private String name,email,mobile; 
public AdminAllocation(String name, String email, String mobile){ 
     this.name=name; 
     this.email=email; 
this.mobile=mobile; 
} public String getName() { 
     return name; 
    } 
public void setName(String name) { 
     this.name = name; 
    } 
public String getMobile() { 
     return mobile; 
    }public void setMobile(String mobile) { 
     this.mobile = mobile; 
    }public String getEmail() { 
     return email; } 
public void setEmail(String email) { 
     this.email = email; 
    } 
} 

答えて

0

セットアダプタの値を取得した後

listView.setAdapter(contactAdapter); 

json_string=getIntent().getExtras().getString("json_data"); 
     try { 
      jsonObject=new JSONObject(json_string); 
      jsonArray=jsonObject.getJSONArray("courseDataSet"); 

      int count=0; 
      String name,email,moblile; 

      while (count<jsonArray.length()){ 


       JSONObject JO=jsonArray.getJSONObject(count); 
       name=JO.getString("id"); 
       email= JO.getString("company_id"); 
       moblile=JO.getString("user_id"); 
       AdminAllocation contacts=new AdminAllocation(name,email,moblile); 
       contactAdapter.add(contacts); 
       count++; 
      } 


     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    listView.setAdapter(contactAdapter); 
+0

はまだエラーを与え、表示されていないこのコーディング

json_string=getIntent().getExtras().getString("json_data"); try { jsonObject=new JSONObject(json_string); jsonArray=jsonObject.getJSONArray("courseDataSet"); int count=0; String name,email,moblile; while (count<jsonArray.length()){ JSONObject JO=jsonArray.getJSONObject(count); name=JO.getString("id"); email= JO.getString("company_id"); moblile=JO.getString("user_id"); AdminAllocation contacts=new AdminAllocation(name,email,moblile); contactAdapter.add(contacts); count++; } } catch (JSONException e) { e.printStackTrace(); } 

正しいコードの後に​​次の行を追加します結果。これは私の誤りです。 – waleed

+0

nullオブジェクト参照で仮想メソッド 'java.lang.String android.os.Bundle.getString(java.lang.String)'を呼び出そうとしました at vu.bc110201891.btg.AdminActivities.AdminAllocationActivity.onCreate(AdminAllocationActivity.java: 33) – waleed

+0

任意の整数またはdouble値をStringとして読み取る必要があります。それを確認してください –