我正在构建一个应用程序,我创建了一个listview,我可以从mysql数据库中检索数据。它的工作非常好。 但问题是我想用数据库中的详细信息加载另一个活动中每个项目的数据。 有人可以帮我吗?我真的卡住了,我不知道怎么做。 这是我的java代码:
public class GererUtilisateur extends AppCompatActivity
{
private ArrayList<ClassListItem> itemArrayListU;
private MyAppAdapterU myAppAdapterU;
private ListView listViewU;
private boolean succes = false;
private static final String DB_URL="jdbc:mysql://192.168.1.2/test3_10032018";
private static final String USER="";
private static final String PASS="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gerer_utilisateur);
listViewU=(ListView) findViewById(R.id.listuser);
itemArrayListU = new ArrayList<ClassListItem>();
SynDataU orderData = new SynDataU();
orderData.execute();
}
private class SynDataU extends AsyncTask<String,String,String> {
String msg="";
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... strings) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
if (connection == null) {
succes = false;
} else {
String query = "SELECT id_utilisateur , nom , prenom , fonction FROM utilisateur";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
if (rs != null) {
while (rs.next()) {
try {
itemArrayListU.add(new ClassListItem(rs.getInt("id_utilisateur"), rs.getString("nom"), rs.getString("prenom") , rs.getString("fonction")));
} catch (Exception ex) {
ex.printStackTrace();
}
}
succes = true;
}
}
}catch (Exception e) {
e.printStackTrace();
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
msg = writer.toString();
succes = false;
}
return msg;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(GererUtilisateur.this,"Synchronising","ListView Loading !!!",true);
}
@Override
protected void onPostExecute(String s) {
progressDialog.dismiss();
if (succes = false){
}else{
try{
myAppAdapterU = new MyAppAdapterU(itemArrayListU,GererUtilisateur.this);
listViewU.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listViewU.setAdapter(myAppAdapterU);
}catch (Exception ex){
}
}
}
}
public class MyAppAdapterU extends BaseAdapter {
public class ViewHolder{
TextView id;
TextView prenom;
TextView nom;
TextView fonction;
}
public List<ClassListItem> decisionList;
public Context context;
ArrayList<ClassListItem> arrayList;
private MyAppAdapterU(List<ClassListItem> apps,Context context){
this.decisionList= apps;
this.context=context;
arrayList= new ArrayList<ClassListItem>();
arrayList.addAll(decisionList);
}
@Override
public int getCount() {
return decisionList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder = null;
if(rowView==null){
LayoutInflater inflater = getLayoutInflater();
rowView= inflater.inflate(R.layout.list_content,parent,false);
viewHolder= new ViewHolder();
viewHolder.id=rowView.findViewById(R.id.tv_id);
viewHolder.nom=rowView.findViewById(R.id.tv_nom);
viewHolder.prenom=rowView.findViewById(R.id.tv_prenom);
viewHolder.fonction=rowView.findViewById(R.id.tv_fonc);
rowView.setTag(viewHolder);
}else
{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.id.setText(decisionList.get(position).getId()+"");
viewHolder.nom.setText(decisionList.get(position).getNom()+"");
viewHolder.prenom.setText(decisionList.get(position).getPrenom()+"");
viewHolder.fonction.setText(decisionList.get(position).getFonction()+"");
return rowView;
}
}
public void ajout(View view){
Intent i = new Intent(getApplicationContext(),AjouterUtil.class);
startActivity(i);
}
这是我的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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mootaz.mobilevoting.GererUtilisateur"
android:background="@drawable/bg">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_ajout"
android:layout_marginTop="25dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="10dp"
card_view:cardBackgroundColor="@color/BlueGrayLight"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listuser"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_ajout"
style="@style/RaisedButtonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="22dp"
android:layout_marginTop="98dp"
android:onClick="ajout"
android:text="Ajouter Utilisateur" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:fontFamily="cursive"
android:text="Gerer les utilisateurs"
android:textColor="@color/primary"
android:textSize="28dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
</RelativeLayout>
答案 0 :(得分:0)
在您的FirstActivity中添加此内容。
listViewU.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Movie movie = movieList.get(position);
// retrieve from movie whatever you want
movie.getName();
Toast.makeText(getApplicationContext(),"Name: "+movie.getName(),Toast.LENGTH_LONG).show();
// creaet intent and add data to it.
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("name",movie.getName());
startActivity(intent);
}
});
在你的第二个活动中获取这样的数据。
// get info from intent
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");