我正在执行此部分代码并尝试解析数据并设置自定义基础适配器,之后我想使列表成为可嵌入的...
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class StreamPage extends ListActivity {
String UrlForStream;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.streampage);
RestImplimentationMethods urlretriver = new RestImplimentationMethods();
UrlForStream = urlretriver.GenerateFullUrlforStream(getIntent()
.getStringExtra("secret"));
ParsingForStream(UrlForStream);
Log.d("****Stream****URL", UrlForStream);
}
private void ParsingForStream(String urlForStream2) {
// TODO Auto-generated method stub
new LongOperation().execute(UrlForStream);
}
private class LongOperation extends AsyncTask<String, Void, String> implements OnItemClickListener {
private static final String TAG_streamable = "streamable";
private static final String TAG_Title = "title";
private static final String TAG_Description = "description";
private static final String TAG_Image = "image";
StreamPersingClass Spc;
JSONArray streamable = null;
ArrayList<StreamPersingClass> searchResultss = new ArrayList<StreamPersingClass>();
final ListView lv1 = getListView();
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONParser jparser = new JSONParser();
JSONObject json = jparser.getJSONFromUrl(params[0]);
try {
streamable = json.getJSONArray(TAG_streamable);
for (int i = 0; i < streamable.length(); i++) {
Spc = new StreamPersingClass();
JSONObject c = streamable.getJSONObject(i);
Spc.setTitle(c.getString(TAG_Title));
Spc.setDescription(c.getString(TAG_Description));
Spc.setImage(c.getString(TAG_Image));
searchResultss.add(Spc);
Log.d("****Stream***Title***", Spc.getTitle());
Log.d("****Stream***Description***", Spc.getDescription());
Log.d("****Stream**Image Title****", Spc.getImage());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
lv1.setAdapter(new MyCustomBaseAdapter(StreamPage.this,
searchResultss));
lv1.setOnItemClickListener(this);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
}
}
}
我现在要做什么?任何建议.....我已经尝试了另一个代码,其中没有asyn任务运行良好...
答案 0 :(得分:0)
此代码应该在ListActivity中,即。在StreamPage
类
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
}
编辑:
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class StreamPage extends ListActivity implements OnItemClickListener {
String UrlForStream;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.streampage);
RestImplimentationMethods urlretriver = new RestImplimentationMethods();
UrlForStream = urlretriver.GenerateFullUrlforStream(getIntent()
.getStringExtra("secret"));
ParsingForStream(UrlForStream);
setOnItemClickListener(this);
Log.d("****Stream****URL", UrlForStream);
}
private void ParsingForStream(String urlForStream2) {
// TODO Auto-generated method stub
new LongOperation().execute(UrlForStream);
}
private class LongOperation extends AsyncTask<String, Void, String> {
private static final String TAG_streamable = "streamable";
private static final String TAG_Title = "title";
private static final String TAG_Description = "description";
private static final String TAG_Image = "image";
StreamPersingClass Spc;
JSONArray streamable = null;
ArrayList<StreamPersingClass> searchResultss = new ArrayList<StreamPersingClass>();
final ListView lv1 = getListView();
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
JSONParser jparser = new JSONParser();
JSONObject json = jparser.getJSONFromUrl(params[0]);
try {
streamable = json.getJSONArray(TAG_streamable);
for (int i = 0; i < streamable.length(); i++) {
Spc = new StreamPersingClass();
JSONObject c = streamable.getJSONObject(i);
Spc.setTitle(c.getString(TAG_Title));
Spc.setDescription(c.getString(TAG_Description));
Spc.setImage(c.getString(TAG_Image));
searchResultss.add(Spc);
Log.d("****Stream***Title***", Spc.getTitle());
Log.d("****Stream***Description***", Spc.getDescription());
Log.d("****Stream**Image Title****", Spc.getImage());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
lv1.setAdapter(new MyCustomBaseAdapter(StreamPage.this,
searchResultss));
lv1.setOnItemClickListener(this);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
不要在asynctask中设置适配器只需调用notifydatasetchanged()。然后尝试点击事件。