简体   繁体   English

Android customListView扩展BaseAdapter

[英]Android customListView extend BaseAdapter

I am working on parsing JSON output from server using volley. 我正在使用Volley解析服务器的JSON输出。 I have created a customList extending BaseAdapter ,When i click on the row i need to call another activity which show remaining data of json. 我创建了一个customList延伸BaseAdapter ,当我点击该行我需要调用另一个活动,其显示JSON的剩余数据。

Here is the JSON output 这是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":""
}

Here is the code of CustomList view 这是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;
}


  }

Here is the main Activity code 这是主要的活动代码

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

}` }`

Here is some hint for your confusion so define your response of json in fragment activity as public ie 这是您困惑的一些提示,因此将您在片段活动中对json的响应定义为public即

public MyJsonresponse mResponse;

and than implement listener to your listview 而不是实现侦听器到您的列表视图

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

Create one method in your fragment.ex 在fragment.ex中创建一个方法

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

after that you can call your fragment activity response inside your second screen or fragment like mainActivity().mResponse 之后,您可以在第二个屏幕或片段中调用您的片段活动响应,如mainActivity()。mResponse

This is it Regards. 这是问候。

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

    }
   });

Now in new Activity get the data: 现在在新的活动中获取数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM