I’m trying to make it so that once the user click on an item in my listview, it goes to another class that has been dynamically created for the occasion. To add on, I’m creating a contact list App and when the user click on the App it shows Texts and call history that’s gone through our system; I’m not sure how to do this, I guess the Garbage collecter method work work well, although, I think it’s the wrong context.
Thanks 🙂
Code:
public class ChatService extends ListActivity {
String GotPass;
String GotUname;
public static final String PREFS_NAME = "MyPregs";
private GetTask getTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getTask = new GetTask();
getTask.execute();
}
public class GetTask extends AsyncTask<Void, Void, ReturnModel> {
@Override
protected ReturnModel doInBackground(Void... params) {
return load();
}
@Override
protected void onPostExecute(ReturnModel result) {
if(result.passworderror == true)
{
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "yaya", Toast.LENGTH_SHORT).show();
ListView lv = getListView();
lv.setTextFilterEnabled(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, result.getheadlines());
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "Yaya, we clicked on something", Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
});
}
}
}
private ReturnModel load() {
ReturnModel returnModel = new ReturnModel();
BufferedReader in = null;
String data = null;
Bundle gotData = getIntent().getExtras();
if (gotData != null) {
GotPass = gotData.getString("key!");
GotUname = gotData.getString("key!!");
}
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String username = settings.getString("key1", null);
String password = settings.getString("key2", null);
// username = "irock97"; // unremark to test like you got username from prefs..
if (username != null && username.equals("irock97")) {
returnModel.setPassworderror(false);
}
else
{
returnModel.setPassworderror(true);
return returnModel;
}
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://gta5news.com/login.php");
try {
// Add user name and password
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("HttpPost", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = "";
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
List<String> headlines = new ArrayList<String>();
headlines.add(data);
returnModel.setheadlines(headlines);
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return returnModel;
}
public class ReturnModel {
private List<String> headlines;
private boolean passworderror;
public List<String> getheadlines() {
return headlines;
}
public void setheadlines(List<String> headlines) {
this.headlines = headlines;
}
public boolean getPassworderror() {
return passworderror;
}
public void setPassworderror(boolean passworderror) {
this.passworderror = passworderror;
}
}
}
Inflate an xml initial View (Pre setup that is equal on each of your dynamically created layouts) (inflate means format xml to View, simplified).
now you can modify newView for your wishes and add new View Elements.
To do so you have to reference your initial Layout, where you want to add things.
For Example:
you can for example create a new Textview and add it to the myIniitalLL.
you can now create custom private methods in your activity to create the logical part. This should be called in onItemClickListener.
It is also possible to create dynamic TextViews via loop, if you dont now how much textviews you will need. I would preffer a ArrayList as parameter for that.