i do search for this issue, but i can't found anything that can help me...Here is my problem, i'm developing an App on Android, and i fetch some data from a database, inside a ListView. The point is i need that when the user click on a list item, this list item goes to another ListView. I created two listviews, but i cant call the method 'add' on the 'OnClickListener', hope you guys can help me.
Here is my class Java and the XML file where i create the ListViews
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:background="#fff" >
<LinearLayout
android:id="@+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/blue_gradient"
android:orientation="horizontal" >
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Procurar funcionarios"
android:inputType="textVisiblePassword"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ListView_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottom_layover"
android:layout_below="@+id/top_layover"
android:weightSum="1" >
<RelativeLayout
android:id="@+id/rl_ListView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:divider="@android:color/transparent"
android:scrollbars="none" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_ListView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#333"
android:layout_weight="0.5" >
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:divider="@android:color/transparent"
android:layout_toRightOf="@+id/list2"
android:scrollbars="none" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/blue_gradient"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/post_comment"
style="@style/WhiteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/black_button"
android:onClick="addComment"
android:text="@string/post_comment" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</RelativeLayout>
**JAVA CLASS**
package com.petridis.medral;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Equipe extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// php read comments script
// localhost :
// testing on your device
// put your local ip instead, on windows, run CMD > ipconfig
// or in mac's terminal type ifconfig and look for the ip under en0 or en1
// private static final String READ_COMMENTS_URL =
// "http://xxx.xxx.x.x:1234/webservice/comments.php";
// testing on Emulator:
private static final String READ_COMMENTS_URL = "http://192.168.0.14:1234/webservice/comments.php";
// testing from a real server:
// private static final String READ_COMMENTS_URL =
// "http://www.mybringback.com/webservice/comments.php";
// JSON IDS:
private static final String TAG_SUCCESS = "success";
private static final String TAG_NOME = "funcionarios";
private static final String TAG_TITLE = "Funcionario";
//Declarar search
EditText inputSearch;
// Array de JSON para buscar dados
private JSONArray mComments = null;
// manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
// Cria um array list
ArrayList listaSelecionada = new ArrayList();
ListView listview = (ListView) findViewById(R.id.list2);
//Cria um array adapter
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_build);
// Crio uma listview e vinculo com o XML
// Array de Strings
String[] values = new String[] {};
for (int i = 0; i < values.length; ++i) {
String title = values[i];
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NOME, title);
}
ListAdapter adapterZ = new SimpleAdapter(this, listaSelecionada,
R.layout.select_itens, new String[] { TAG_NOME },
new int[] { R.id.title });
listview.setAdapter(adapterZ);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// loading the comments via AsyncTask
new LoadComments().execute();
}
public void addComment(View v) {
Intent i = new Intent(Equipe.this, Menu.class);
startActivity(i);
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_NOME);
// Looping para pegar resultados
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
// Pegar o conteudo do tag
String title = c.getString(TAG_TITLE);
// Criar o HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NOME, title);
// adding HashList to ArrayList
mCommentList.add(map);
// annndddd, our JSON data is up to date same with our array
// list
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* Inserts the parsed data into the listview.
*/
private void updateList() {
// For a ListActivity we need to set the List Adapter, and in order to
// do
// that, we need to create a ListAdapter. This SimpleAdapter,
// will utilize our updated Hashmapped ArrayList,
// use our single_post xml template for each item in our list,
// and place the appropriate info from the list to the
// correct GUI id. Order is important here.
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_NOME },
new int[] { R.id.title });
// I shouldn't have to comment on this one:
setListAdapter(adapter);
// Optional: when the user clicks a list item we
// could do something. However, we will choose
// to do nothing...
ListView lv = getListView();
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
Equipe.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap item = (HashMap)getListAdapter().getItem(position);
Toast.makeText(Equipe.this, item + "selected", Toast.LENGTH_LONG).show();
Equipe.this.adapterZ.addAll(item);
Equipe.this.
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Equipe.this);
pDialog.setMessage("Loading Comments...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
// we will develop this method in version 2
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
// we will develop this method in version 2
updateList();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.petridis.medral"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.petridis.medral.Login"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.petridis.medral.Menu"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.petridis.medral.Equipe"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
</activity>
</application>
</manifest>
0 comments:
Post a Comment