使用获取ID更改ImageView内容

时间:2016-09-28 13:25:11

标签: java android import dynamic-programming dynamic-content

ID&并且说明:

ID's and states

当我尝试更改任何项目的状态时:

When i try to change state of any item

我试图从数据库中选择项目并获取他们的ID来更改imageview。但它总是会获得最后的物品价值,这会导致一切都像最后一样。

以下是以下代码:

`package orhanapp.sqlapp;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Context;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Rooms extends Activity {

    Temiz temizz=new Temiz();

    ConnectionClass connectionClass;
    String z;
    EditText edtId, edtNo;
    TextView text,textviewisim;
    ProgressBar pbbar;
    ListView lstpro;
    String proid;
    ImageView img;

    Temiz temiz=new Temiz();
    String s,s1,s2,s3;



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


        connectionClass = new ConnectionClass();
        edtId = (EditText) findViewById(R.id.edtId);
        edtNo = (EditText) findViewById(R.id.edtNo);

        text=(TextView)findViewById(R.id.text);
        text.setText(getIntent().getExtras().getString("veri"));

        textviewisim=(TextView)findViewById(R.id.textviewisim);
        textviewisim.setText(getIntent().getExtras().getString("veriisim"));

        pbbar = (ProgressBar) findViewById(R.id.pbbar);
        pbbar.setVisibility(View.GONE);
        lstpro = (ListView) findViewById(R.id.lstproducts);
        proid = "";




        Rooms.FillList fillList = new Rooms.FillList();
        fillList.execute("");


    }



    public class FillList extends AsyncTask<String, String, String> {
        String z = "";

        List<Map<String, String>> prolist  = new ArrayList<Map<String, String>>();

        @Override
        protected void onPreExecute() {

            pbbar.setVisibility(View.VISIBLE);
        }




        @Override


        protected void onPostExecute (String r){

            pbbar.setVisibility(View.GONE);
            Toast.makeText(Rooms.this, r, Toast.LENGTH_SHORT).show();


            String[] from = {"A", "B", "C", "D"};
            int[] views = {R.id.lblproid, R.id.lblproname, R.id.lblId, R.id.lblState};
            final SimpleAdapter Temiz = new SimpleAdapter(Rooms.this,
                    prolist, R.layout.activity_temiz, from,
                    views);

            final SimpleAdapter Dolu = new SimpleAdapter(Rooms.this,
                    prolist, R.layout.activity_dolu, from,
                    views);

            final SimpleAdapter Kirli = new SimpleAdapter(Rooms.this,
                    prolist, R.layout.activity_kirli, from,
                    views);

            final SimpleAdapter Arizali = new SimpleAdapter(Rooms.this,
                    prolist, R.layout.activity_arizali, from,
                    views);




            if (s.startsWith("0")) {

                lstpro.setAdapter(Temiz);

            } else if (s.startsWith("1")) {

                lstpro.setAdapter(Dolu);

            } else if (s.startsWith("2")) {

                lstpro.setAdapter(Kirli);

            } else if (s.startsWith("3")) {

                lstpro.setAdapter(Arizali);
            }




            lstpro.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {  

                    HashMap<String, Object> obj = (HashMap<String, Object>) Temiz
                            .getItem(arg2);

                    proid = (String) obj.get("A");
                    String proname = (String) obj.get("B");
                    String prodesc = (String) obj.get("C");
                    String state = (String) obj.get("D");
                    edtId.setText(prodesc);
                    edtNo.setText(proname);



                    HashMap<String, Object> obj1 = (HashMap<String, Object>) Dolu
                            .getItem(arg2);

                    proid = (String) obj.get("A");
                    String proname1 = (String) obj.get("B");
                    String prodesc1 = (String) obj.get("C");
                    String state1 = (String) obj.get("D");
                    edtId.setText(prodesc1);
                    edtNo.setText(proname1);


                    HashMap<String, Object> obj2 = (HashMap<String, Object>) Kirli
                            .getItem(arg2);

                    proid = (String) obj.get("A");
                    String proname2 = (String) obj.get("B");
                    String prodesc2 = (String) obj.get("C");
                    String state2 = (String) obj.get("D");
                    edtId.setText(prodesc2);
                    edtNo.setText(proname2);

                    HashMap<String, Object> obj3 = (HashMap<String, Object>) Arizali
                            .getItem(arg2);

                    proid = (String) obj.get("A");
                    String proname3 = (String) obj.get("B");
                    String prodesc3 = (String) obj.get("C");
                    String state3 = (String) obj.get("D");
                    edtId.setText(prodesc3);
                    edtNo.setText(proname3);


                    Intent intent = new Intent(Rooms.this, StateRoom.class);
                    intent.putExtra("veri2", edtId.getText().toString());
                    intent.putExtra("veriOdano", edtNo.getText().toString());
                    startActivity(intent);

                }
            });

        }

        String id=text.getText().toString();

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

            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    z = "Error in connection with SQL server";
                } else {

                    String query = "select * from deneme2 where Id="+id+"";
                    PreparedStatement ps = con.prepareStatement(query);
                    ResultSet rs = ps.executeQuery();

                    ArrayList<String> data1 = new ArrayList<String>();

                    while (rs.next()) {


                        Map<String, String> datanum = new HashMap<String, String>();

                        datanum.put("A", rs.getString("Id"));
                        datanum.put("B", rs.getString("OdaNo"));
                        datanum.put("C", rs.getString("bolId"));
                        datanum.put("D", rs.getString("Durum"));
                        prolist.add(datanum);


                        s = datanum.get("D");



                    }

                    z = "Success";
                }
            } catch (Exception ex) {
                z = "Error retrieving data from table";

            }

            return z;
        }


    }



}`

1 个答案:

答案 0 :(得分:0)

while (rs.next() && rs.first()) {


                        Map<String, String> datanum = new HashMap<String, String>();

                        datanum.put("A", rs.getString("Id"));
                        datanum.put("B", rs.getString("OdaNo"));
                        datanum.put("C", rs.getString("bolId"));
                        datanum.put("D", rs.getString("Durum"));
                        prolist.add(datanum);


                        s = datanum.get("D");



                    }

将此代码替换为现有