将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener
Posted
技术标签:
【中文标题】将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener【英文标题】:Passing Data from Fragment listview item to Activity string variable onitemclicklistener 【发布时间】: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);
提前谢谢你,我很抱歉我的英语不好
【问题讨论】:
【参考方案1】:我认为您的问题来自于此:String selectedFromList =
(lv.getItemAtPosition(position).toString());
lv.getItemAtPosition(position)
返回一个 Object,它是 listView Adapter 的一个项目!
在这个对象上调用toString()
不会返回任何好处:)
OnClickListener 有一个 View in 参数,它是父视图,在您的情况下,是您单击的项目视图。 您可以检索保存在 OnClickListener 中的名称字段的 TextView 并获取此 TextView 的字符串,如下所示:
@Override
public void onItemClick(AdapterView<?> parent, View view, int postion, long id)
String name = ((TextView) view.findViewById(R.id.myNameField)).getText().toString();
希望对你有帮助:)
PS : 下次也发布你的 XML 代码 ;)
【讨论】:
【参考方案2】:我修好了,改一下:
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;
到这里:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
position, long id)
String selectedFromList=
String.valueOf(arrayList.get(position).getIdd());
Intent i = new Intent(getActivity(), Test.class);
i.putExtra("name", selectedFromList);
startActivity(i);
);
return rootView;
【讨论】:
以上是关于将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener的主要内容,如果未能解决你的问题,请参考以下文章