2016-03-03 102 views
0

我正在使用volley從服務器解析JSON輸出。我創建了一個customList擴展BaseAdapter,當我點擊行時,我需要調用另一個顯示json剩餘數據的活動。Android customListView extend BaseAdapter

這裏是JSON輸出

{ 
    "id":"12", 
    "company_name":"Kartik", 
    "company_logo":"", 
    "job_title":"php developer", 
    "experience":"2 to 3", 
    "location":"city", 
    "walkin_date":"5:11:2016", 
    "salary":"3.4-4.4L pa", 
    "qualification":"B.E", 
    "address":"" 
} 

在這裏被CustomList視圖

`public class CustomLIstAdapter extends BaseAdapter 
    { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<Movie> movieList; 
    ImageLoader imageLoader = Controller.getInstance().getImageLoader(); 


public CustomLIstAdapter(Activity activity, List<Movie> movieItems) { 
    this.activity = activity; 
    this.movieList = movieItems; 

} 

public int getCount() { 
    return movieList.size(); 
} 


public Object getItem(int location) { 
    return movieList.get(location); 

} 


// this will retrive the current touch id 
@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    if (inflater == null) 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    if (convertView == null) 
     convertView = inflater.inflate(R.layout.list_row, null); 


    if (imageLoader == null) 
     imageLoader = Controller.getInstance().getImageLoader(); 
    NetworkImageView thumbnail = (NetworkImageView) convertView.findViewById(R.id.thumbnail); 
    TextView title = (TextView) convertView.findViewById(R.id.title); 
    TextView rating = (TextView) convertView.findViewById(R.id.rating) 
    TextView genre = (TextView) convertView.findViewById(R.id.genre); 
    TextView year = (TextView)convertView.findViewById(R.id.releaseYear); 



    Movie m = movieList.get(position); 
    thumbnail.setImageUrl(m.getCompanyLogo(), imageLoader) 
    title.setText(m.getCompanyName()); 
    rating.setText(m.getTitle()); 


    year.setText("Walking Date : " + m.getWalkinDate()); 

    return convertView; 
} 


    } 

這裏的代碼在主活動代碼

`public class MainActivity extends AppCompatActivity { 

    private static final String TAG = MainActivity.class.getSimpleName(); 




private static final String url="http://10.0.3.2:80/demoApi/api/users"; 



private ProgressDialog pDialog; 
private List<Movie> movieList = new ArrayList<Movie>(); 
private ListView listView; 
private CustomLIstAdapter adapter; 
private SwipeRefreshLayout swipeRefreshLayout; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 




    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setVisibility(View.INVISIBLE); 


    // accessing the layout of the sumeeth refresh. 

    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh); 
    listView = (ListView) findViewById(R.id.list); 
    adapter = new CustomLIstAdapter(this, movieList); 
    listView.setAdapter(adapter); 




    /*Testing on swipe listener.*/ 
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      retriveDataJob(); 

     } 
    }); 


    pDialog = new ProgressDialog(this); 
    // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 





    // swipeRefreshLayout.setOnRefreshListener(this); 
    swipeRefreshLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      swipeRefreshLayout.setRefreshing(true); 

      //call which method to refresh. 
      retriveDataJob(); 
     } 
    }); 


} 

private void retriveDataJob() { 

    //this method will call the server and update the list of things 
    swipeRefreshLayout.setRefreshing(true); 

    JsonArrayRequest movieReq = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 
        hidePDialog(); 


        for (int i = 0; i < response.length(); i++) { 
         try { 

          JSONObject obj = response.getJSONObject(i); 
          Movie movie = new Movie(); 
          movie.setCompanyName(obj.getString("company_name")); 
          movie.setCompanyLogo(obj.getString("company_logo")); 
          movie.setTitle(obj.getString("job_title")); 
          movie.setWalkinDate(obj.getString("walkin_date")); 
          movie.setLocation(obj.getString("location")); 
          movieList.add(movie); 

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

        } 
        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
        swipeRefreshLayout.setRefreshing(false); 

       } 


       //stop the refresher 


      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
      hidePDialog(); 

      //stopHere 
      swipeRefreshLayout.setRefreshing(false); 
     } 
    }); 

    // Adding request to request queue 
    Controller.getInstance().addToRequestQueue(movieReq); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    hidePDialog(); 
} 

private void hidePDialog() { 
    if (pDialog != null) { 
     pDialog.dismiss(); 
     pDialog = null; 
    } 
} 












@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}`

+0

編輯問題與解析代碼 –

+0

不要誤會我的問題是,當我點擊它必須表現出更多的剩餘刷卡JSON數據customList固定的活動。 –

+0

我需要知道如何編碼當我點擊該列表,我需要顯示完整的輸出JSON數據在另一個活動。 – sum33th

回答

0

下面是一些提示爲你的困惑,從而定義在片段活動JSON的響應,公共 即

public MyJsonresponse mResponse; 

,比實施監聽到你的列表視圖

lv.setOnItemClickListener(new OnItemClickListener() 
    { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View v, int position, 
     long arg3) 
    { 
     //Call Your fragment to show full json data detail. 

     // do what you intend to do on click of listview row 
    } 
    }); 

在fragment.ex創建一個方法

private MainFragmentActivity mainActivity() { 
    return ((MainFragmentActivity) getActivity()); 
} 

後,你可以叫你像mainActivity第二屏幕或片段內的片段活動響應()。mResponse

這就是它 關心。

+0

我需要在哪裏實施? MainActivity或CustomListActivity。 – sum33th

+0

從哪裏設置適配器 –

0
lv.setOnItemClickListener(new OnItemClickListener() 
    { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View v, int position, 
     long arg3) 
    { 

    //get all the data from list and send to next activity where you can view your data as you want: 

     Intent i1=new Intent(A.this, B.class); 
     i1.putExtra("data", lv.get(position).getData()); 
     startActivity(i1); 

    } 
    }); 

現在,在新的活動得到數據:

String data=getIntent.getStringExtra("data");