2016-08-16 26 views
1

我想要分割數據,所以我可以在不同的TextView顯示它如何onpostexecute方法的AsyncTask的數據拆分成陣列

如我在圖像數據給予需要顯示成這些不同的TextView 我能夠讓一個TextView的但不超過一個textviews數據

PHP代碼:

<?php 
    require "init.php"; 
    $user_name=$_POST["login_name"]; 
    $user_pass=$_POST["login_pass"]; 

    $sql = "SELECT name,user_name,PRN,cast,dob,admission_category,nationality,permnent_address,local_address,roll_no,class,sub_cast,birth_place,mobile_no,email,branch FROM user_info where user_name like '$user_name'"; 
    $result = $conn->query($sql); 

    if ($result->num_rows > 0) { 
     // output data of each row 
     while($row = $result->fetch_assoc()) { 
     echo "name: " . $row["name"]; 

     } 
    } else {[enter image description here][1] 
     echo "login failed try again"; 
    } 
    $conn->close(); 
    ?>* 

ImportFragment.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class ImportFragment extends Fragment { 
ImageView imageView2,imageView7; 
    SQLiteDatabase db; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View myInflatedView = inflater.inflate(R.layout.fragment_import, container, false); 
     Intent i = getActivity().getIntent(); 
     String name = i.getStringExtra("sey"); 
     Toast.makeText(getActivity(), "welcome: " + name, Toast.LENGTH_LONG).show(); 


     imageView2 = (ImageView) myInflatedView.findViewById(R.id.imageView2); 
     TextView textView=(TextView)myInflatedView.findViewById(R.id.textView7); 
     textView.setText(name); 
     db=this.getActivity().openOrCreateDatabase("tedt.db", Context.MODE_PRIVATE,null); 
     db.execSQL("create table if not exists tb (a blob)"); 

     Cursor c=db.rawQuery("select * from tb",null); 
     getImage(); 
     if(c.moveToNext()) 
     { 
      byte[] image =c.getBlob(0); 
      Bitmap bmp= BitmapFactory.decodeByteArray(image,0,image.length); 
      imageView2.setImageBitmap(bmp); 

      Toast.makeText(getActivity(),"get success",Toast.LENGTH_SHORT).show(); 
     } 
     Button button=(Button)myInflatedView.findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       FragmentManager fm = getFragmentManager(); 
       fm.beginTransaction().replace(R.id.contentframe,new ExportFragment()).commit(); 

      } 
     }); 

    return myInflatedView; 
    } 

    public void getImage() 
    { 
     Cursor c=db.rawQuery("select * from tb",null); 
     if(c.moveToNext()) 
     { 
      byte[] image =c.getBlob(0); 
      Bitmap bmp= BitmapFactory.decodeByteArray(image,0,image.length); 
      imageView2.setImageBitmap(bmp); 

      Toast.makeText(getActivity(),"get success",Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

BackgroundTask

class BackgroundTask extends AsyncTask<String, Void, String> { 
     AlertDialog alertDialog; 
     TextView textView1; 

     Context ctx; 
     BackgroundTask(Context ctx) 
     { 
      this.textView1=textView1; 
      this.ctx=ctx; 
     } 
     @Override 
     protected void onPreExecute() 
     { 
      alertDialog=new AlertDialog.Builder(ctx).create(); 
      alertDialog.setTitle("Login Information..."); 

     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      if(result.equals("registration success")) { 
       Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
      } 
      else 
      { 
       // alertDialog.setMessage(result); 
       // alertDialog.show(); 
       Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
       Intent intent=new Intent(ctx,drawer.class); 
       intent.putExtra("sey",result); 
       String a=result; 
       ctx.startActivity(intent); 
      } 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      String reg_url="http://10.0.2.2/webapp/register.php"; 
      String login_url="http://10.0.2.2/webapp/login.php"; 

      String method=params[0]; 
      if(method.equals("register")) 
      { 
       String name=params[1]; 
       String user_name=params[2]; 
       String user_pass=params[3]; 
       try { 
        URL url=new URL(reg_url); 
        HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection(); 
        httpURLConnection.setRequestMethod("POST"); 
        httpURLConnection.setDoOutput(true); 
        OutputStream os=httpURLConnection.getOutputStream(); 
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(os,"UTF-8")); 
        String data= URLEncoder.encode("user","UTF-8")+ "="+URLEncoder.encode(name,"UTF-8")+"&"+ 
          URLEncoder.encode("user_name","UTF-8")+ "="+URLEncoder.encode(user_name,"UTF-8")+"&"+ 
          URLEncoder.encode("user_pass","UTF-8")+ "="+URLEncoder.encode(user_pass,"UTF-8"); 
        bufferedWriter.write(data); 
        bufferedWriter.flush(); 
        bufferedWriter.close(); 
        os.close(); 
        InputStream is=httpURLConnection.getInputStream(); 
        is.close(); 
        return "registration success"; 
       } 
       catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      else if(method.equals("login")) 
      { 

       String login_name=params[1]; 
       String login_pass=params[2]; 
       try { 
        URL url=new URL(login_url); 
        HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection(); 
        httpURLConnection.setRequestMethod("POST"); 
        httpURLConnection.setDoOutput(true); 
        httpURLConnection.setDoInput(true); 
        OutputStream outputStream=httpURLConnection.getOutputStream(); 
        BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
        String data=URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+ 
          URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8"); 
        bufferedWriter.write(data); 
        bufferedWriter.flush(); 
        bufferedWriter.close(); 
        outputStream.close(); 

        InputStream inputStream=httpURLConnection.getInputStream(); 
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1")); 
        String response=""; 
        String line=""; 
        while ((line=bufferedReader.readLine())!=null) 
        { 
         response+=line; 
        } 

        bufferedReader.close(); 
        inputStream.close(); 
        httpURLConnection.disconnect(); 
        return response; 

       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      return null; 
     } 
    } 

回答

0

如果數據的長度和結構是未知的

可以像下面顯示它。

  1. 數據的結構應當像(XML,JSON)

    { 「名稱」: 「托馬斯」, 「時代」:12, 「地址」: 「倫敦」 }

每個元素都應該包含一個關鍵字,以便您可以使用該關鍵字生成TextView(用於標籤)。

  1. 由於json數據中的數據長度是動態的,TextView將以動態方式生成。 (就像這樣how to add dynamic array of textviews to a tablelayout through table row in android?

但請記住,如果沒有任何自定義樣式的android中顯示的某些數據。佈局不僅醜陋,而且難以理解。如果來自服務器的所有數據(包括服務器的項目ID或敏感信息)都顯示在移動設備上,可能會受到攻擊。爲了最好地使用數據和您的安全,您應該逐個獲取數據到每個文本視圖(findViewById)。如果這種方法對您來說很麻煩,您可以告別並使用android數據綁定(https://developer.android.com/topic/libraries/data-binding/index.html)。 但是,此庫與JDK 8尚不兼容。在使用前請仔細考慮。