Android customListView扩展了BaseAdapter

时间:2016-03-03 12:01:16

标签: java android json listview android-volley

我正在使用volley解析服务器的JSON输出。我创建了BaseAdapter扩展{ "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":"" } ,当我点击该行时,我需要调用另一个显示json剩余数据的活动。

这是JSON输出

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);
}

这是主要的活动代码

some_text <- "Hel-_l0o W#oRr^ld"
gsub("[^[:alpha:] ]", "", some_text)#will return all characters
gsub("[^[:lower:] ]", "", some_text)#will return only lower characters alongwith space
gsub("[^[:upper:] ]", "", some_text)#will return higher case characters alongwith space

}`

2 个答案:

答案 0 :(得分: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

就是这样 问候。

答案 1 :(得分:0)

{{1}}

现在在新的Activity中获取数据:

{{1}}