【发布时间】:2018-04-14 22:10:12
【问题描述】:
我正在尝试将 ListView 项从片段传递到活动中的字符串变量“url”,意图成功创建活动,但是当我尝试传递数据时遇到问题。 片段类有一个带有 json 数据的列表视图,它显示图片、标题和 名称 的列表。当用户选择一个项目时,它会将他们带到活动中。我希望他们的选择将 name 从列表视图转移到其他活动中的 url 变量。因此片段中的网址从“https://.....id=”更改为“https://.....id=”+name。 这将使 recyclerview 更改为单击任何列表视图项的新 url。
我的片段:
public class Accueil extends Fragment {
ArrayList<articles> arrayList;
ListView lv;
public static String URL1=null;
class ReadJSON extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
return readURL(params[0]);
}
@Override
protected void onPostExecute(String content) {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(content);
} catch (JSONException e1) {
e1.printStackTrace();
}
JSONArray jsonArray = null;
try {
jsonArray = jsonObject.getJSONArray("articles");
} catch (JSONException e1) {
e1.printStackTrace();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject articlesobject = null;
try {
articlesobject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
try {
arrayList.add(new articles(
articlesobject.getString("picture"),
articlesobject.getString("title"),
articlesobject.getString("name")
));
} catch (JSONException e1) {
e1.printStackTrace();
}
CustomListAdaper adaper = new CustomListAdaper(
getActivity().getApplicationContext(),
R.layout.custom_list_layout, arrayList
);
lv.setAdapter(adaper);
}
}
private String readURL(String theURL) {
StringBuilder content = new StringBuilder();
try {
URL url = new URL(theURL);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
arrayList = new ArrayList<>();
View rootView = inflater.inflate(R.layout.accueil, container, false);
lv = (ListView) rootView.findViewById(R.id.ListView1);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
new Accueil.ReadJSON().execute("http://wach.ma/mobile/home.php");
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long id) {
String selectedFromList =
(lv.getItemAtPosition(position).toString());
Intent i = new Intent(getActivity(), Test.class);
i.putExtra("name", selectedFromList);
startActivity(i);
}
});
return rootView;
}
}
我的活动:
public class Test extends AppCompatActivity {
public String URL_DATA="http://wach.ma/mobile/category.php?id=";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
Intent i = getIntent();
String s1 = i.getStringExtra("name");
URL_DATA=URL_DATA+s1;
loadRecyclerViewData();
}
private void loadRecyclerViewData(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Chargement...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
URL_DATA, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray("articles");
for(int i = 0; i<array.length();i++){
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("picture"),
o.getString("name"),
o.getString("city"),
o.getString("add_time"),
o.getString("price")
);
listItems.add(item);
}
adapter = new Myadapter(listItems, getApplicationContext());
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(getApplicationContext(),
volleyError.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
提前谢谢你,我很抱歉我的英语不好
【问题讨论】:
标签: android listview android-fragments android-intent