我有一个CustomAdapter ListView
和TabLayout
的活动,有三个标签TrainerTab1,TrainerTab2,TrainerTab3。单击某个项目时,它应移至TrainerTab1 Fragment
的下一页。
我需要将ListView
onClick()
中的ID传递给片段页面。我使用Bundle
来传递值,但项目不起作用。当我点击某个项目时,它没有响应,也没有显示任何错误。
我的自定义ListView
Class
是:
public class Trainer extends AppCompatActivity {
String tabUrl = "http://adoxsolutions.in/numuww/services/trainers";
private GridView gridView;
ArrayList<HashMap<String, String>> alist = new ArrayList<>();
private TrainAdapter adapter;
private ProgressDialog mprogress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trainer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
new Train().execute();
}
private class Train extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
mprogress = new ProgressDialog(Trainer.this);
mprogress.setMessage("Loading...");
mprogress.setIndeterminate(false);
mprogress.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(tabUrl);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("POST");
//
System.out.println("Response Code:" + connect.getResponseCode());
InputStream in = new BufferedInputStream(connect.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);
Log.d("VALUE:", response);
JSONObject obj = new JSONObject(response);
JSONArray jsArray = obj.optJSONArray("Trainers");
for (int k = 0; k < jsArray.length(); k++) {
HashMap<String, String> map = new HashMap<String, String>();
obj = jsArray.getJSONObject(k);
map.put("id", obj.getString("id"));
map.put("name", obj.getString("name"));
map.put("logo", obj.getString("img"));
alist.add(map);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
gridView = (GridView) findViewById(R.id.trainerGrid);
adapter = new TrainAdapter(getBaseContext(), alist);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mprogress.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.newc, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
return true;
}
else if(id == android.R.id.home){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
class TrainAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
public TrainAdapter(Context c, ArrayList<HashMap<String, String>> list){
context = c;
MyArr = list;
}
@Override
public int getCount() {
return MyArr.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.trainer_list, null);
}
TextView id= (TextView) convertView.findViewById(R.id.trainerId);
ImageView image = (ImageView) convertView.findViewById(R.id.trainerImage);
TextView text = (TextView) convertView.findViewById(R.id.trainerTexts);
try {
image.setImageBitmap(loadBitmap(MyArr.get(position).get("logo")));
text.setText(MyArr.get(position).get("name"));
id.setText(MyArr.get(position).get("id"));
if (((position - 9) / 3) % 9 == 0) {
ImageView adimg = (ImageView) convertView.findViewById(R.id.trainadBanner);
adimg.setScaleType(ImageView.ScaleType.FIT_XY);
adimg.getLayoutParams().height=150;
adimg.getLayoutParams().width=300;
adimg.setImageResource(R.drawable.mainad);
}
} catch (Exception e) {
e.printStackTrace();
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle h=new Bundle();
h.putString("id",MyArr.get(position).get("id"));
Fragment tt1=new TrainerTab1();
tt1.setArguments(h);
}
});
// }else{
// convertView=inflater.inflate(R.layout.trainer_list, parent,false);
// ImageView image= (ImageView) convertView.findViewById(R.id.trainerImage);
// TextView text= (TextView) convertView.findViewById(R.id.trainerTexts);
// try{
// image.setImageResource(R.drawable.ic_action_name);
// text.setText(MyArr.get(position).get("name"));
// if(position % 9 == 0){
// ImageView adimg= (ImageView) convertView.findViewById(R.id.trainadBanner);
// adimg.setImageResource(R.drawable.mainad);
// }
//
// } catch(Exception e){
// e.printStackTrace();
// }
return convertView;
}
private static final String TAG = "ERROR";
private static final int IO_BUFFER_SIZE = 4 * 1024;
private static Bitmap loadBitmap(String tabUrl) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(tabUrl).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + tabUrl);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(TAG, "Could not close stream", e);
}
}
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
}
我的标签片段:
public class TrainerTab1 extends Fragment {
String turl="http://adoxsolutions.in/numuww/services/trainer";
TextView tname,tplace,tsex;
public TrainerTab1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView= inflater.inflate(R.layout.fragment_trainer_tab1, container, false);
final String tid=getArguments().getString("id");
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
tname= (TextView) rootView.findViewById(R.id.trainerCourse);
tplace= (TextView) rootView.findViewById(R.id.trainerPlace);
tsex= (TextView) rootView.findViewById(R.id.trainerSex);
ImageView photo= (ImageView) rootView.findViewById(R.id.trainer_photo);
}
}
答案 0 :(得分:0)
在onClickListener
内
将包添加到片段后。
FragmentManager fragmentManager = Trainer.this.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.someIDForYourFrameLayout, tt1).commit();
答案 1 :(得分:0)
你能不能精确地指出在哪里使用它。我的意思是我不明白如何将fragment的id添加到transaction.add( R.id.someIDForYourFrameLayout ,tt1).commit(); 我试过这样:
Bundle h=new Bundle();
h.putString("id",MyArr.get(position).get("id"));
Fragment tt1=new TrainerTab1();
tt1.setArguments(h);
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(R.id.trainerFragment,tt1).commit();
这是我的fragment_trainer_tab_1布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollbarSize="1dp"
android:id="@+id/trainerFragment"
tools:context="com.example.anu.numuww.TrainerTab1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="12dp"
android:background="@color/blue">
<ImageView
android:id="@+id/trainer_photo"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:contentDescription="@string/trainers"
android:src="@mipmap/ic_launcher"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/trainerCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="12dp"
android:textColor="#ffffff"
android:textSize="22sp"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/trainerSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginTop="5dp"
android:textColor="#ffffff"
android:textSize="17sp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/trainerPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textColor="#ffffff"
android:textSize="17sp"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F5F5F5"
android:gravity="center_horizontal"
android:padding="7dp"
android:orientation="horizontal">
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/global"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/messenger"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/google_plus"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/inst_detail"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/telegram"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/facebook"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/twitter"/>
<ImageView
android:layout_width="38dp"
android:layout_height="38dp"
android:padding="2dp"
android:src="@drawable/inst_instagram"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="About Trainer"
android:textColor="@color/blue"
android:textSize="19sp"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/para"
android:layout_marginTop="4dp"
android:layout_marginBottom="2dp"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>