2016-05-13 99 views
0

我們通過從共享首選項中獲取值來創建列表視圖。工作流程將如此。在行視圖的末尾添加動態生成列表的靜態按鈕

  1. 客戶登錄到應用程序,並點擊收藏夾標籤。對於第一次模式視圖將出現切換開關。
  2. 客戶可以選擇切換開關來添加他們最喜歡的選項並保存。
  3. 在下次登錄時,當客戶點擊收藏夾選項卡時,列表將顯示出他之前選擇的選項。
  4. 現在我們想要在按鈕上添加一個靜態按鈕(用於編輯),點擊按鈕後將顯示帶有切換開關頁面的模態視圖。

但是,當我試圖在我的XML文件中添加按鈕時,我正在每行中獲取按鈕。您在這方面的幫助將非常可觀。

佈局(XML文件)

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="2dp"  

android:layout_marginTop="15dp" 
> 


<ImageView 
    android:id="@+id/logo" 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:layout_marginTop="5dp" 
    > 

</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="14dp" > 
</TextView> 

</LinearLayout> 

dummy.java

import kvbank.kvb_epassbook.util.MobileArrayAdapter; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 
import android.view.View; 
import android.widget.ListView; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.view.KeyEvent; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import java.util.ArrayList; 
public class dummy extends ListActivity { 
//String[] MOBILE_OS = new String[15] ; 
ArrayList<String> MOBILE_OS = new ArrayList<String>(); 


@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    final SharedPreferences sp = PreferenceManager 
      .getDefaultSharedPreferences(this); 

    int count1 = sp.getInt("count", -1); 
    String cc = String.valueOf(count1); 
    System.out.println(cc); 

    if((count1 == 0) || (count1==-1)){ 
     Intent i = new Intent(this, favorites.class); 
     startActivity(i); 

      Toast.makeText(getApplicationContext(), "No favorites Set!!! =)", 
         Toast.LENGTH_LONG).show(); 
    } 


    else 
    { 


      SharedPreferences.Editor editor = sp.edit(); 

boolean ad = sp.getBoolean("accdet", false); 
if(ad == true){ 
MOBILE_OS.add("AccountDetails") ; 
} 
boolean as =sp.getBoolean("accsumm", false); 
if(as == true){ 
MOBILE_OS.add("AccountSummary") ; 
} 
boolean ast =sp.getBoolean("accstmt", false); 
if(ast == true){ 
MOBILE_OS.add("AccountStatement") ;  
} 
boolean crl =sp.getBoolean("CRL", false); 
if(crl == true){ 
MOBILE_OS.add("Cash Remitance Limit") ; 
} 
boolean cse =sp.getBoolean("CSE", false); 
if(cse == true){ 
MOBILE_OS.add("Cheque Status Enquiry") ;  
} 
boolean ic =sp.getBoolean("inwchq", false); 
if(ic == true){ 
MOBILE_OS.add("Inward Cheque") ; 
} 
boolean uca =sp.getBoolean("Unclramt", false); 
if(uca == true){ 
MOBILE_OS.add("uncleared Amount") ; 
} 
boolean dt =sp.getBoolean("depdet", false); 
if(dt == true) 
{ 
MOBILE_OS.add("Deposit Details") ; 
}  

setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));  

    } 

} 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    //get selected items 
    String selectedValue = (String) getListAdapter().getItem(position); 
    Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); 

} 
} 

MobileArrayAdapter

import java.util.ArrayList; 
    import kvbank.kvb_ebook.R; 
    import android.content.Context; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.ImageView; 
    import android.widget.TextView; 

public class MobileArrayAdapter extends ArrayAdapter<String> { 
private final Context context; 
private final ArrayList<String> values; 

public MobileArrayAdapter(Context context, ArrayList<String> mOBILE_OS) { 
    super(context, R.layout.list_mobile, mOBILE_OS); 
    this.context = context; 
    this.values = mOBILE_OS; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.list_mobile, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); 
    textView.setText(values.get(position)); 

    // Change icon based on name 
    String s = values.get(position); 


    if (s.equals("AccountDetails")) { 
     imageView.setImageResource(R.drawable.acctinf); 
    } else if (s.equals("AccountStatement")) { 
     imageView.setImageResource(R.drawable.acctstat); 
    } else if (s.equals("AccountSummary")) { 
     imageView.setImageResource(R.drawable.acctsum); 
    } else if(s.equals("Cash Remitance Limit")){ 
     imageView.setImageResource(R.drawable.cashret1); 
    } else if(s.equals("Cheque Status Enquiry")){ 
     imageView.setImageResource(R.drawable.chqbkenq); 
    } else if(s.equals("Inward Cheque")){ 
     imageView.setImageResource(R.drawable.inwchq); 
    } else if(s.equals("uncleared Amount")){ 
     imageView.setImageResource(R.drawable.uncl1); 
    } else if(s.equals("Deposit Details")){ 
     imageView.setImageResource(R.drawable.tddet); 
    } else if(s.equals("Deposit Summary")){ 
     imageView.setImageResource(R.drawable.tdsum); 
    } else if(s.equals("Deposit Statement")){ 
     imageView.setImageResource(R.drawable.dep_stmt); 
    } else if(s.equals("Loan Details")){ 
     imageView.setImageResource(R.drawable.lndet); 
    } else if(s.equals("Loan Summary")){ 
     imageView.setImageResource(R.drawable.lnsumm1); 
    } else if(s.equals("Loan Statement")){ 
     imageView.setImageResource(R.drawable.lnsumm1); 
    } else if(s.equals("Alert Maintenance")){ 
     imageView.setImageResource(R.drawable.alertreg); 
    } else if (s.equals("Forex Rates")){ 
     imageView.setImageResource(R.drawable.forex1); 
    } 
    else if(s.equals("Cheque Book Request")) 
    { 
    imageView.setImageResource(R.drawable.chqreq) ; 
    } 



    return rowView; 
} 
} 
+0

?意思是低於名單? –

+0

不在列表的最後。該列表將滾動,但按鈕應該是靜態的 –

+0

分享您的活動佈局。 –

回答

1

創建一個XML文件dummy.xml粘貼在該文件下面的代碼:

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

<ListView 
    android:id="@+id/mylist" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"/> 

<Button 
    android:id="@+id/btn_static" 
    android:layout_gravity="center" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="STATIC"/> 
</LinearLayout> 

更改dummy.java象下面這樣:

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.Toast; 
import java.util.ArrayList; 

import kvbank.kvb_epassbook.util.MobileArrayAdapter; 

public class dummy extends Activity { 
//String[] MOBILE_OS = new String[15] ; 
ArrayList<String> MOBILE_OS = new ArrayList<String>(); 
Button btn_static; 
ListView mylist; 
MobileArrayAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.dummy); 

    mylist = (ListView)findViewById(R.id.mylist); 

    //This is your Button use it any where you want..!! 

    btn_static = (Button)findViewById(R.id.btn_static); 

    final SharedPreferences sp = PreferenceManager 
      .getDefaultSharedPreferences(this); 

    int count1 = sp.getInt("count", -1); 
    String cc = String.valueOf(count1); 
    System.out.println(cc); 

    if ((count1 == 0) || (count1 == -1)) { 
     Intent i = new Intent(this, favorites.class); 
     startActivity(i); 

     Toast.makeText(getApplicationContext(), "No favorites Set!!! =)", 
       Toast.LENGTH_LONG).show(); 
    } else { 


     SharedPreferences.Editor editor = sp.edit(); 

     boolean ad = sp.getBoolean("accdet", false); 
     if (ad == true) { 
      MOBILE_OS.add("AccountDetails"); 
     } 
     boolean as = sp.getBoolean("accsumm", false); 
     if (as == true) { 
      MOBILE_OS.add("AccountSummary"); 
     } 
     boolean ast = sp.getBoolean("accstmt", false); 
     if (ast == true) { 
      MOBILE_OS.add("AccountStatement"); 
     } 
     boolean crl = sp.getBoolean("CRL", false); 
     if (crl == true) { 
      MOBILE_OS.add("Cash Remitance Limit"); 
     } 
     boolean cse = sp.getBoolean("CSE", false); 
     if (cse == true) { 
      MOBILE_OS.add("Cheque Status Enquiry"); 
     } 
     boolean ic = sp.getBoolean("inwchq", false); 
     if (ic == true) { 
      MOBILE_OS.add("Inward Cheque"); 
     } 
     boolean uca = sp.getBoolean("Unclramt", false); 
     if (uca == true) { 
      MOBILE_OS.add("uncleared Amount"); 
     } 
     boolean dt = sp.getBoolean("depdet", false); 
     if (dt == true) { 
      MOBILE_OS.add("Deposit Details"); 
     } 

     adapter = new MobileArrayAdapter(this, MOBILE_OS); 

     mylist.setAdapter(adapter); 

     mylist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 

       String selectedValue = (String) adapter.getItem(position); 
       Toast.makeText(dummy.this, selectedValue, Toast.LENGTH_SHORT).show(); 

       return false; 

      } 
     }); 

    } 

} 
} 
要在末尾添加按鈕
+1

謝謝janki ....代碼......只能在明天試試.......將更新狀態 –

+0

這工作正常。但我選擇了9個切換開關(限於9個)並點擊保存。每次它只顯示7個項目,並且不會進一步滾動 –

+0

謝謝我解決了這個問題。它是我保存共享偏好時的錯誤。非常感謝您的支持和幫助。你的回答幫助我,並將其標記爲答案。 –

相關問題