Android TextView不会更新

时间:2014-02-10 10:31:35

标签: android multithreading

我知道有很多这类问题,但这是我的问题 我有一个带有图像的图库,当我点击时,会有ImageView在图库中显示所选图像 我还有一个TextView女巫会显示一条消息 当我点击图库中的图片时,我的ImageView会获取图片,TextTiew不会更新其数据。
我试图将代码放在try/catch块中,并且不会抛出任何异常 在我的日志中,TextView具有我提供的确切文本 我尝试使用新线程,但结果是一样的:TextView没有更新,但我的ImageView没有更新。
我尝试使用runOnUiThread(据我所知它与Thread相同),但仍然没有。
我也从TextView更改为EditText,但我遇到了同样的问题 每个人都说:“使用一个线程” - 我做了,但它没有用 我的想法用完了......

public class VODInterface extends Activity {

public static String[] values;

private static int itemPosition;

public static Activity thisActivity;

public Gallery gallery;
public ListView listView;
public LinearLayout MovieView;
public ImageView imgview;

public boolean submenus=false;
public boolean listviewVisibility = true;

private Handler handler = new Handler() ;

private SharedPreferences ep;

public ArrayList<VODObject> vod;
private ArrayList<Bitmap> tempimg;

private static int MovieSetNumber = 1;

public static int VODIndexPosition = 0;

public VODInterface(){

}
public void onDestroy(){
    MainActivity.inChild=false;
    super.onDestroy();  
}
public void onCreate(Bundle bundle){
    super.onCreate(bundle);

    this.setContentView(R.layout.listview2);

    ep = getSharedPreferences("Settings", 0);

    listView = (ListView) this.findViewById(R.id.listView1);

    MovieView = (LinearLayout) this.findViewById(R.id.movieView);

    imgview = (ImageView)this.findViewById(R.id.imageView2);

    // Defined Array values to show in ListView
    values = new String[] { "A..Z",
                                     "Category",
                                     "Most Rated", 
                                     "Most Watched", 
                                     "Recently"
                                    };


    //this shows whitch movie should me load images
    //we show always only 10 movies
    MovieSetNumber = 1;
    // Define a new Adapter
    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter); 

    // ListView Item Click Listener
    listView.setOnItemClickListener(new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {

           // ListView Clicked item index
              itemPosition = position;
           handler.post(goDeep);

          }

     }); 
}

private Runnable goDeep = new Runnable(){

    public void run(){
        new goDeep().execute("");
    }
};
class goDeep extends AsyncTask<String, String, String>{
    ProgressDialog mProgressDialog = new ProgressDialog(VODInterface.this);
    String result="";
    boolean failLoadVOD=false;

    protected void onPreExecute(){
        mProgressDialog.setMessage(getString(R.string.downloading));
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        //START check if servers are Ok
        if(!Server.isOnline()){
            Server.getAvailableServer();
            Log.i("good", "");
        }

        if(!Server.isOnline()){
            Log.i("WEW", "SERVERS are off");
            return null;
        }
        //END check if servers are Ok


        //START synchronize local database with online database
        try{

            if(ep.contains("downloaded")){
                Log.i("localDatabase", "is full");
            }
            else{
                Editor ed = ep.edit();
                ed.putString("downloaded", "");
                ed.commit();

                //load data from database
                if(!MainActivity.web.parseVODAndVODCategories()){
                    failLoadVOD=true;
                }
            }

        }
        catch(NullPointerException ee)      
        {
            //lets go get datas from Tibodatabase
            Editor ed = ep.edit();
            ed.putString("downloaded", "");
            ed.commit();

            if(!MainActivity.web.parseVODAndVODCategories()){
                failLoadVOD=true;
            }   

        }
        //END synchronize local database with online database


        //START handle menus 
        if(!submenus)                       //MainMenu
        {
            if(itemPosition == 1){
                //get all movies ordered by name
                List<String> val = MainActivity.voddb.selectAllCategories();
                values = new String[val.size()+1];
                values[0] ="[...]";
                for(int i=0;i<val.size();i++){
                    values[i+1] = val.get(i);
                }
                listviewVisibility = true;
            }
            else{
                //show movies
                listviewVisibility = false;
            }
            submenus = true;

        }
        else{
            if(itemPosition == 0){
                values = new String[] { "A..Z",
                        "Category",
                        "Most Rated", 
                        "Most Watched", 
                        "Recently"
                       };
                submenus = false;
                listviewVisibility = true;
            }
            else{
                listviewVisibility = false;
            }
        }
        //END handle menu

        if(!listviewVisibility){
            getData();
            for(int i=0;i<vod.size();i++){
                if(!FileExciste(vod.get(i).Icon)){
                    try{
                        Bitmap bmp ;
                        URL newurl = new URL(vod.get(i).Icon); 
                        bmp = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
                        saveImageToSD(bmp,vod.get(i).Icon);
                        Log.i(i+"", "done");
                    }
                    catch(Exception e){
                        Log.i("exciste", "exciste");
                    }
                }
                else{
                    Log.i(i+"", "exciste");
                }

            }

        }
        runOnUiThread(new Runnable() {
             @Override
             public void run() {
                    if(!listviewVisibility){
                        listView.setVisibility(View.GONE);
                        MovieView.setVisibility(View.VISIBLE);
                        populateMovieView();
                    }
                    else{
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(VODInterface.this,
                            android.R.layout.simple_list_item_1, android.R.id.text1, values);

                            listView.setAdapter(adapter); 
                    }

            }
        });


        mProgressDialog.dismiss();

        return "";

    }
    protected void onPostExecute(){
        //start adding to cache
        super.onPostExecute("");
    }
}
private void getData(){
    vod = new ArrayList<VODObject>();
    for(int i=1;i<MainActivity.voddb.getVODCount()+1;i++){
        vod.add(MainActivity.voddb.getVOD(i+""));
        if(vod.get(i-1).Icon.contains(" ")){
        vod.get(i-1).Icon = vod.get(i-1).Icon.replaceAll(" ", "%20");
        Log.i("u korrigjua", vod.get(i-1).Icon);
        }

    }

}

    private void populateMovieView(){


gallery = (Gallery) findViewById(R.id.gallery1);

GalleryImageAdapter gia= new GalleryImageAdapter(this,vod);

gallery.setAdapter(gia);

gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, final int position, long id)           {
        // show the selected Image


        /*here is the problem
         * 
         *----------------------------/
         */
        try{
            LayoutInflater factory = getLayoutInflater();

            View view = factory.inflate(R.layout.listview2, null);

            EditText title = (EditText) view.findViewById(R.id.editText2);
            title.setText(vod.get(position).title);

            TextView desc = (TextView) view.findViewById(R.id.editText4);
            desc.setText(vod.get(position).description);
            Log.i("info", title.getText()+"    "+desc.getText());
        }

        catch(Exception ee){
            Log.i("info", ee.getMessage()+"    nnn");
        }
        /*----------------------*/
        try{

            imgview.setImageBitmap(getImageFromSD(vod.get(position).Icon));

        }
        catch(Exception ee){
        imgview.setImageResource(R.drawable.untitled);
        }
    }

  });

   }
             public static Bitmap getImageFromSD(String url){
String path = Environment.getExternalStorageDirectory().toString();
File imgFile = new  File(path+"/"+WebHelper.getFilenameFromUrl(url));
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    return myBitmap;

     }
          private void saveImageToSD(Bitmap bmp,String url) throws IOException {

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "/"+WebHelper.getFilenameFromUrl(url));

    fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    fOut.flush();
    fOut.close();

                        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName          (),file.getName());
  }
       private boolean FileExciste(String url){
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "/"+WebHelper.getFilenameFromUrl(url));  
return file.exists();

   }

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/vod"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="top" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="274dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:background="@color/translucent_bblack" >
    </ListView>

    <LinearLayout
        android:id="@+id/movieView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/translucent_black"
        android:orientation="vertical"
        android:visibility="invisible" >

        <Gallery
            android:id="@+id/gallery1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:background="@color/translucent_bblack" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/imageView2"
                android:layout_width="400dp"
                android:layout_height="fill_parent"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="@drawable/bord" />

            <LinearLayout
                android:id="@+id/MovieDesc"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="20dp"
                android:layout_marginRight="20dp"
                android:background="@color/translucent_bblack"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/editText1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="30dp"
                    android:inputType="textPersonName"
                    android:text="Title:"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp"
                    android:textStyle="bold" >

                </TextView>

                <TextView
                    android:id="@+id/editText2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/editText3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="30dp"
                    android:text="Description:"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/editText4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/editText5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="60dp"
                    android:text="Duration:"
                    android:textColor="#FFFFFF"
                    android:textSize="20sp"
                    android:textStyle="bold" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将TextViews声明为字段,如ImageView ---- “imgview”,如下所示......

public ImageView imgview;
public TextView title;
public TextView desc;

onCreate 方法初始化为“imgview”

imgview = (ImageView)this.findViewById(R.id.imageView2);

title = (TextView) view.findViewById(R.id.editText2);

desc = (TextView) view.findViewById(R.id.editText4);

并按照以下方式更新 gallery.setOnItemClickListener 代码......

gallery.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View v, final int position, long id)           {

        title.setText(vod.get(position).title);

        desc.setText(vod.get(position).description);
        Log.i("info", title.getText()+"    "+desc.getText());

        imgview.setImageBitmap(getImageFromSD(vod.get(position).Icon)); 

    }
});