gpt4 book ai didi

java - android listview 的详细 View

转载 作者:行者123 更新时间:2023-12-02 11:21:50 24 4
gpt4 key购买 nike

我有一个显示属性名称的listView,它运行良好,但在显示每个公告的详细 View 时遇到问题。当您单击 listView 中的项目时,我收到空的 Activity_details,并且调试器中出现以下错误:

W/System.err: org.json.JSONException: Value No of type java.lang.String cannot be converted to JSONArray
W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
W/System.err: at com.example.said.populate.DetailsActivity$GetHttpResponse.doInBackground(DetailsActivity.java:205)
W/System.err: at com.example.said.populate.DetailsActivity$GetHttpResponse.doInBackground(DetailsActivity.java:180)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:831)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

这是用于连接的 PHP 文件:

 <?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfig.php';

$StudentID= $_POST['propertyid'];

// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM estate2 where id = '$propertyid'" ;

$result = $conn->query($sql);

if ($result->num_rows >0) {


while($row[] = $result->fetch_assoc()) {

$tem = $row;

$json = json_encode($tem);

}

} else {
echo "No Results Found.";
}
echo $json;

$conn->close();
}
?>

下面是我的详细信息 Activity :

public class DetailsActivity extends AppCompatActivity {

HttpParse httpParse = new HttpParse();
ProgressDialog pDialog;

// Http Url For Filter Student Data from Id Sent from previous activity.
String HttpURL = "https://sultonkhuja1111.000webhostapp.com/MobApp/detail.php";


String finalResult ;
HashMap<String,String> hashMap = new HashMap<>();
String ParseResult ;
HashMap<String,String> ResultHash = new HashMap<>();
String FinalJSonObject ;
TextView NAME,PHONE_NUMBER,CLASS;
String NameHolder, NumberHolder, ClassHolder;
Button UpdateButton, DeleteButton;
String TempItem;
ProgressDialog progressDialog2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);

NAME = (TextView)findViewById(R.id.textName);
PHONE_NUMBER = (TextView)findViewById(R.id.textPhone);
CLASS = (TextView)findViewById(R.id.textClass);

UpdateButton = (Button)findViewById(R.id.buttonUpdate);
DeleteButton = (Button)findViewById(R.id.buttonDelete);

//Receiving the ListView Clicked item value send by previous activity.
TempItem = getIntent().getStringExtra("ListViewValue");

//Calling method to filter Student Record and open selected record.
HttpWebCall(TempItem);

}


//Method to show current record Current Selected Record
public void HttpWebCall(final String PreviousListViewClickedItem){

class HttpWebCallFunction extends AsyncTask<String,Void,String> {

@Override
protected void onPreExecute() {
super.onPreExecute();

pDialog = ProgressDialog.show(DetailsActivity.this,"Loading Data",null,true,true);
}

@Override
protected void onPostExecute(String httpResponseMsg) {

super.onPostExecute(httpResponseMsg);

pDialog.dismiss();

//Storing Complete JSon Object into String Variable.
FinalJSonObject = httpResponseMsg ;

//Parsing the Stored JSOn String to GetHttpResponse Method.
new GetHttpResponse(DetailsActivity.this).execute();

}

@Override
protected String doInBackground(String... params) {

ResultHash.put("propertyid",params[0]);

ParseResult = httpParse.postRequest(ResultHash, HttpURL);

return ParseResult;
}
}

HttpWebCallFunction httpWebCallFunction = new HttpWebCallFunction();

httpWebCallFunction.execute(PreviousListViewClickedItem);
}


// Parsing Complete JSON Object.
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;

public GetHttpResponse(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0)
{
try
{
if(FinalJSonObject != null)
{
JSONArray jsonArray = null;

try {
jsonArray = new JSONArray(FinalJSonObject);

JSONObject jsonObject;

for(int i=0; i<jsonArray.length(); i++)
{
jsonObject = jsonArray.getJSONObject(i);

// Storing Student Name, Phone Number, Class into Variables.
NameHolder = jsonObject.getString("property_name").toString() ;
NumberHolder = jsonObject.getString("price").toString() ;
ClassHolder = jsonObject.getString("description").toString() ;

}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result)
{

// Setting Student Name, Phone Number, Class into TextView after done all process .
NAME.setText(NameHolder);
PHONE_NUMBER.setText(NumberHolder);
CLASS.setText(ClassHolder);

}
}

}

并显示AllEstatesActivity:

public class ShowingAllEstatesActivity extends AppCompatActivity {

ListView SubjectListView;
ProgressBar progressBarSubject;
String ServerURL = "http://sultonkhuja1111.000webhostapp.com/MobApp/Estate.php";
List<String> IdList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_showing_all_estates);

SubjectListView = (ListView)findViewById(R.id.listview1);

progressBarSubject = (ProgressBar)findViewById(R.id.progressBar);

new GetHttpResponse(ShowingAllEstatesActivity.this).execute();

//Adding ListView Item click Listener.
SubjectListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// TODO Auto-generated method stub

Intent intent = new Intent(ShowingAllEstatesActivity.this, DetailsActivity.class);

// Sending ListView clicked value using intent.
intent.putExtra("ListViewValue", IdList.get(position).toString());

startActivity(intent);

//Finishing current activity after open next activity.
finish();

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_listing, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()){
case R.id.addNew:
startActivity(new Intent(this, AddPropertyActivity.class));
break;
}
return true;
}

private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
public Context context;

String ResultHolder;

List<subjects> subjectsList;

public GetHttpResponse(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0)
{
HttpServicesClass httpServiceObject = new HttpServicesClass(ServerURL);
try
{
httpServiceObject.ExecutePostRequest();

if(httpServiceObject.getResponseCode() == 200)
{
ResultHolder = httpServiceObject.getResponse();

if(ResultHolder != null)
{
JSONArray jsonArray = null;

try {
jsonArray = new JSONArray(ResultHolder);

JSONObject jsonObject;

subjects subjects;

subjectsList = new ArrayList<subjects>();

for(int i=0; i<jsonArray.length(); i++)
{
subjects = new subjects();

jsonObject = jsonArray.getJSONObject(i);

IdList.add(jsonObject.getString("propertyid").toString());

subjects.SubjectName = jsonObject.getString("property_name");

subjectsList.add(subjects);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServiceObject.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result)

{
progressBarSubject.setVisibility(View.GONE);

SubjectListView.setVisibility(View.VISIBLE);

if(subjectsList != null)
{
ListAdapter adapter = new ListAdapter(subjectsList, context);

SubjectListView.setAdapter(adapter);
}
}
}

}

这是获取 HttpUrl、detail.php 的代码:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

include 'DatabaseConfig.php';

$propertyid= $_POST['propertyid'];

// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM estate2 where propertyid = '$propertyid'";

$result = $conn->query($sql);

if ($result->num_rows >0) {


while($row[] = $result->fetch_assoc()) {

$tem = $row;

$json = json_encode($tem);

}

} else {
echo "No Results Found.";
}
echo $json;

$conn->close();
}
?>

最佳答案

我认为您的 HttpURL 看起来不太好。如果此 url 未给出 JSON 响应,则会产生相同的问题。

关于java - android listview 的详细 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49855094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com