我使用listview来显示工作正常的公司的名称,描述和部门。但是我想从列表视图中选择一个特定的项目作为描述和部门,并将其传递给两个不同的片段,这些片段显示在一个viewpager。 但我不知道如何只选择描述并将其传递给相应的片段,将部门项目传递给部门片段。
显示listview的类文件:
public class CompanyAct extends AppCompatActivity{
private static final String TAG = MainActivity.class.getSimpleName();
private static final String url = "https://api.myjson.com/bins/2ggcs";
private ProgressDialog pDialog;
private List<Company> companydetails = new ArrayList<Company>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companyprimarydetails);
listView = (ListView) findViewById(R.id.companylist);
adapter = new CustomListAdapter(this, companydetails);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Company company=new Company();
// Company company = new Company(obj.getInt("companyID"),obj.getString("comapnyName"),obj.getString("companyOwner"),obj.getString("companyStartDate"),obj.getString("companyDescription"),departments);
company.setCompanyid(obj.getInt("companyID"));
company.setCompanyname(obj.getString("comapnyName"));
company.setCompanyowner(obj.getString("companyOwner"));
company.setStartdate(obj.getString("companyStartDate"));
company.setDescription(obj.getString("companyDescription"));
company.setDepartments(obj.getString("companyDepartments"));
companydetails.add(company);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
CustomAdapter:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Company> companyItems;
Company m;
Context context;
public CustomListAdapter(Activity activity, List<Company> companyItems) {
this.activity = activity;
this.companyItems = companyItems;
}
@Override
public int getCount() {
return companyItems.size();
}
@Override
public Object getItem(int location) {
return companyItems.get(location);
}
@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.companyprimaryitems, null);
TextView companyid=(TextView)convertView.findViewById(R.id.companyid);
TextView companyname = (TextView) convertView.findViewById(R.id.companyname);
TextView companyowner = (TextView) convertView.findViewById(R.id.companyowner);
TextView companystartdate = (TextView) convertView.findViewById(R.id.comapnystartdate);
TextView comapnydesc = (TextView) convertView.findViewById(R.id.companydesc);
TextView companylists=(TextView)convertView.findViewById(R.id.comapanylists);
m = companyItems.get(position);
companyid.setText(String.valueOf(m.getCompanyid()));
companyname.setText(m.getCompanyname());
companyowner.setText(m.getCompanyowner());
companystartdate.setText(m.getStartdate());
comapnydesc.setText(m.getDescription());
//rating.setText("Rating: " + String.valueOf(m.getRating()));
/* String departments = "";
for (String str : m.getDepartments()) {
departments += str + ", ";
}
departments = departments.length() > 0 ? departments.substring(0,
departments.length() - 2) : departments; */
companylists.setText(m.getDepartments());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
m=companyItems.get(position);
String des=m.getDescription()+"#"+m.getDepartments();
Intent i=new Intent(context,DetailsActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle bundle = new Bundle();
bundle.putString("key", des);
i.putExtras(bundle);
context.startActivity(i);
// DescriptionFragment fragobj = new DescriptionFragment();
// fragobj.setArguments(bundle);
// Intent intent = new Intent(context, DetailsActivity.class);
// intent.putExtra("description", m.getDescription());
// intent.putExtra("department", m.getDepartments());
// context.startActivity(intent);
}
});
// release year
return convertView;
}
FragmentActivity:
public class DetailsActivity extends FragmentActivity {
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
String receivingdata;
CharSequence Titles[]={"Description","Departments"};
int Numboftabs =2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidingpanel);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
tabs.setViewPager(pager);
}
}
DescriptionFragment:
public class DescriptionFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.description_frag, container, false);
// String strtext = getArguments().getString("edttext");
TextView desc=(TextView)v.findViewById(R.id.tvdescription);
return super.onCreateView(inflater, container, savedInstanceState);
}
public static DescriptionFragment newInstance(String text) {
DescriptionFragment f = new DescriptionFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
部门片段:
public class DepartmentsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.description_frag, container, false);
TextView dept=(TextView)v.findViewById(R.id.tvdepartments);
return super.onCreateView(inflater, container, savedInstanceState);
}
public static DepartmentsFragment newInstance(String text) {
DepartmentsFragment f = new DepartmentsFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
我甚至试图将值从customadapter传递给fragmentactivity但是它没有显示空指针异常。我想知道如何从列表中选择一个特定的项目并将其传递给两个不同的片段。 / p>
答案 0 :(得分:0)
转到您的适配器,在getview方法中,您只需将onclick listner设置为描述文本,如
comapnydesc.setOnClickListner(new .....)
感谢。