Android ListView和Volley(Android ListView and Volley)
非常新的android和凌空(双重麻烦:))
在尝试使用一组字符串的listview(它工作,我能够看到结果)和从这些在线测试API之一检索(它工作,我看到结果)
当我试图将两者结合起来时(来自凌空的响应,解析它并将其传递给适配器)并且没有任何显示。 有人会告诉我一些结合volley和listview的实用教程,或者帮助向我展示如何让适配器在listview中正确显示响应。
希望有人有时间帮忙。
我的Mainactivity.java
package com.example.web.listviewexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends AppCompatActivity { //Array of strings... String[] mobileArray = {"Android", "Iphone", "Windows", "WebOs", "BlackBerry", "Max OS x"}; TextView results; String JsonURL ="https://reqres.in/api/users/2"; String data=""; //define the volley request queue. It handles requests RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get the listview that will communicate with the adapter ListView listexample = (ListView) findViewById(R.id.mobile_list); //prepare the adapter final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); //attach the adapter listexample.setAdapter(adapter); //Volley stuff --cricket_007 Stackoverflow.com answer final JsonObjectRequest obj= new JsonObjectRequest(Request.Method.GET, JsonURL, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { // parse the response response = response.getJSONObject("data"); // add things to the adapter adapter.add(response.toString()); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error){error.printStackTrace(); } }); Volley.newRequestQueue(this).add(obj); } }
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity" > <ListView android:id="@+id/mobile_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout
activity_listview.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="25sp" android:padding="10dp" android:textStyle="bold"> </TextView>
Very new to android and volley (double trouble :))
After trying the listview with an array of strings(it worked and I was able to see the result) and volley to retrieve from one of those online test api (it worked and I see the result)
When I attempted to combine the two (the response from volley, parse it and pass it to the adapter) and nothing shows. Would someone kindly point me to some solid tutorial combining volley and listview or help to show me how to get the adapter to display the response correctly in the listview.
Hope someone out there with time to help.
My Mainactivity.java
package com.example.web.listviewexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import org.json.JSONException; import org.json.JSONObject; public class MainActivity extends AppCompatActivity { //Array of strings... String[] mobileArray = {"Android", "Iphone", "Windows", "WebOs", "BlackBerry", "Max OS x"}; TextView results; String JsonURL ="https://reqres.in/api/users/2"; String data=""; //define the volley request queue. It handles requests RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get the listview that will communicate with the adapter ListView listexample = (ListView) findViewById(R.id.mobile_list); //prepare the adapter final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); //attach the adapter listexample.setAdapter(adapter); //Volley stuff --cricket_007 Stackoverflow.com answer final JsonObjectRequest obj= new JsonObjectRequest(Request.Method.GET, JsonURL, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { // parse the response response = response.getJSONObject("data"); // add things to the adapter adapter.add(response.toString()); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener(){ @Override public void onErrorResponse(VolleyError error){error.printStackTrace(); } }); Volley.newRequestQueue(this).add(obj); } }
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".MainActivity" > <ListView android:id="@+id/mobile_list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout
activity_listview.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="25sp" android:padding="10dp" android:textStyle="bold"> </TextView>
原文:https://stackoverflow.com/questions/43288977
最满意答案
如果您只想显示单击行中“文本”列的值,您应该能够执行以下操作:
else if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit") { Form2 form = new Form2(); form.textBox1.Text = ((DataGridView)sender).Rows[e.RowIndex].Cells["Text"].Value.ToString(); form.Show(); Hide(); }
这会从名为“Text”的相关单元格中获取值,并将其放入新表单文本框的Text中(假设该文本框名为“textBox1”)。
请注意,对于此事件处理程序,发件人是单击的网格。 通过属性名称“dataGridView1”引用也是有效的,但我认为使用处理程序的参数是一种更好的形式。
If you just want to show the value of the "Text" column from the row that was clicked, you should be able to do something like this:
else if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit") { Form2 form = new Form2(); form.textBox1.Text = ((DataGridView)sender).Rows[e.RowIndex].Cells["Text"].Value.ToString(); form.Show(); Hide(); }
This grabs the value from the related cell called "Text" and puts in into the Text of the new form's textbox (assumes that textbox is called "textBox1".
Note for this event handler, the sender is the grid that was clicked on. It's also valid to reference by the property name "dataGridView1", but I think it's a little better form to use the handler's arguments.
相关问答
更多-
您正在为ajax回调函数中的元素分配相同的数据。 你需要遍历从AJAX调用返回的数据并将其分配给各种元素 for(i=0;i此函数首先创建SQL命令,使用SqlCommand.ExecuteReader()执行它,然后使用SqlDataReader.Read()检查是否有任何结果,然后将结果第一行的第一列显示到Textbox: private void button1_Click(object sender, EventArgs e) { sc.Open(); cmd = new SqlCommand("SELECT maincategory_name FROM maincategory" ...而不是做: grdDistProcessing.Rows[0].DefaultCellStyle.BackColor = Color.BurlyWood; grdDistProcessing.Rows[0].DefaultCellStyle.ForeColor = Color.Black; 对grdDistProcessing使用CellFormatting事件(显示类别),如下所示: private void grdDistProcessing_CellFormatting(object sender, ...
选择员工姓名,他/她的ID将显示在PHP的文本框中(Choose employee name and his/her ID will show in the textbox in PHP)[2023-08-05]
您可以直接在Employee Select Box的选项中构建这种功能(顺便提一下,您的代码中有2个关闭Select标签)。 无论如何,为此,您可以利用HTML5 data属性,如下所示:您需要提供DataKeyNames="MessageNumber" ,如下所示好的,所以,没有什么可做的。 必须添加这个。 protected void btnSelectPerson_Click(object sender, EventArgs e) { TextBox t2 = (TextBox)gvProjectResource.FooterRow.FindControl("t2"); t2.Text = ddlPersons.SelectedItem.Text; mpFindPerson.Hide(); } ...使用jQuery绑定事件处理程序而不是“onfoo”元素属性 最好将处理程序绑定到“select”元素,因为有一些方法可以选择不涉及“click”事件的选项: (一些文本使stackoverflow显示下面的代码块) $(function() { $('#title').change(function() { if ($(this).val() == 'Other') $('#otherTitle').show(); else $('#otherTitle').h ...如何在C#中单击文本框中的某一行时选择整行/行(How can i select the whole row/line when clicking on a line in a textbox in C#)[2024-04-20]
// now you need to know what character index starts that line var start = textBox.GetFirstCharIndexOfCurrentLine(); // and with the line already in hand we can calculate the ending index var end = start + textBox.Lines(textBox.GetLineFromCharIndex(start)) ...如何从Datagirdview中选择Row并在文本框中显示?(How to select Row from Datagirdview and show in textbox?)[2023-08-01]
如果您只想显示单击行中“文本”列的值,您应该能够执行以下操作: else if (dataGridView1.Columns[e.ColumnIndex].Name == "Edit") { Form2 form = new Form2(); form.textBox1.Text = ((DataGridView)sender).Rows[e.RowIndex].Cells["Text"].Value.ToString(); form.Show(); Hide(); } 这 ...试试这个: