Is it possible to display data from a query MySQL
in different textViews
without using a listView
, taking into account that my query MySQL
gives me only one row as a result?
So far I have the following:
MainActivity.java
public class MainActivity extends AppCompatActivity {
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> employeeList;
private ProgressDialog pDialog;
private static final String url_tareas = "http://localhost/test/get_employee.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_EMPLOYEES = "employees";
private static final String TAG_NAME = "name";
public static final String TAG_DESIGNATION = "designation";
JSONArray employees = null;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeList = new ArrayList<>();
new MainActivity.LoadAllProducts().execute();
list = (ListView) findViewById(R.id.listView);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List params = new ArrayList();
JSONObject json = jsonParser.makeHttpRequest(url_tareas, "GET", params);
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
employees = json.getJSONArray(TAG_EMPLOYEES);
for (int i = 0; i < employees.length(); i++) {
JSONObject c = employees.getJSONObject(i);
String name = c.getString(TAG_NAME);
String designation = c.getString(TAG_DESIGNATION);
Log.d("","designation1= "+designation);
HashMap map = new HashMap();
map.put(TAG_NAME, name);
map.put(TAG_DESIGNATION, designation);
employeeList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("","Error= "+e);
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(MainActivity.this,employeeList,R.layout.single_post,new String[] {
TAG_NAME,
TAG_DESIGNATION,
},
new int[] {
R.id.txt_name,
R.id.txt_designation_SINGLE_POST,
});
list.setAdapter(adapter);
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.testbd.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />
</RelativeLayout>
single_post.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="1dp">
<TextView
android:text="Name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="71dp"
android:id="@+id/textView2"
android:textSize="18sp"
android:textColor="@android:color/black" />
<TextView
android:text="Designation:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_marginTop="46dp"
android:id="@+id/textView3"
android:textSize="18sp"
android:textColor="@android:color/black" />
<TextView
android:text="designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView3"
android:layout_alignStart="@+id/txt_name"
android:id="@+id/txt_designation_SINGLE_POST"
android:textSize="18sp"
android:textColor="@android:color/black" />
<TextView
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="86dp"
android:id="@+id/txt_name"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_above="@+id/textView3"
android:layout_alignParentEnd="true" />
</RelativeLayout>