2016-03-01 51 views
0

這裏是我的Java:如何製作某個自定義的listview項目,發送給某個活動?

public class Cantos extends AppCompatActivity { 

ListView lv; 
Context context; 

ArrayList cantoList; 
public static String[] cantos = {"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare", 
     "5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven", 
     "11: Alabad A Dios"}; 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(null); 
    toolbar.setNavigationIcon(getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); 
    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(Cantos.this, MainMenu.class); 
      startActivity(i); 
     } 
    }); 



} 

private void initTypeface() { 

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/AftaSerifThin-Regular.otf"); 
    TextView text = (TextView) findViewById(R.id.toolbarCantos); 
    text.setTypeface(myTypeface); 

} 
} 

這是我其他的Java與BaseAdapter(自定義的ListView):

public class CustomAdapter extends BaseAdapter { 

String[] result; 
Context context; 
private static LayoutInflater inflater = null; 


public CustomAdapter(Cantos cantos, String[] cantos1) { 

    result = cantos1; 
    context = cantos; 
    inflater = (LayoutInflater)context. 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 


@Override 
public int getCount() { 
    return result.length; 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

public class Holder 
{ 
    TextView tv; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Holder holder = new Holder(); 
    View rowView; 

    rowView = inflater.inflate(R.layout.cantos_list, null); 
    holder.tv = (TextView) rowView.findViewById(R.id.textCanto); 
    holder.tv.setText(result[position]); 


    Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf"); 
    holder.tv.setTypeface(myFont); 


    return rowView; 
} 


} 

我怎樣才能讓一個特定的項目送我到另一一定的活動? 提前謝謝! 對不起,如果我垃圾郵件太多!我幾乎完成了一些基礎知識!我求求你應付我!

+0

需要更多信息。什麼項目會導致新的活動?你的listview在哪裏聲明。適配器在哪裏使用? – Samnon

+0

項目「1 Abre Tu Oido」和哎呀!對不起,我刪除了意外的代碼。這裏是'context = this; lv =(ListView)findViewById(R.id.ListView); lv.setAdapter(new CustomAdapter(this,cantos));' – Alex

回答

0

使用intent.putExtra(「鑰匙」,值)& getExtra()從一個活動的數據發送到另一個活動

//try this 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 


      Intent intent; 
      intent = new Intent(CurrentActivity.this, NextActivity.class); 

       startActivity(intent) 

     } 
    }); 
+0

先生/女士感謝您的回覆,但我不是在尋找這個。我想知道如何編碼,所以當我按下我的列表視圖中的隨機項時,它會將我發送給其他活動。你會知道如何做到這一點? – Alex

1

如果你想打電話列表視圖項目的選擇另一個活動您可以在自定義適配器呼叫OnClick監聽器textview

代碼:

爲貴麗
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

Holder holder = new Holder(); 
View rowView; 

rowView = inflater.inflate(R.layout.cantos_list, null); 
holder.tv = (TextView) rowView.findViewById(R.id.textCanto); 
holder.tv.setText(result[position]); 

holder.tv.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Intent intent = new Intent(context,NewAcivity.class); 
        ((Activity) context).startActivity(intent); 
       } 
      }); 


Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf"); 
holder.tv.setTypeface(myFont); 


return rowView; 
} 
0

第一套適配器stview。然後執行onItem點擊事件。

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       displayAnotherListView(position); 
      } 
     }); 


public void displayAnotherListView(int position) { 


     switch (position) { 
      case 0: { 
       Intent i=new Intent(YourActivity.this,Activity_one.class); 
       startActivity(ii); 
       break; 
      } 
      case 1: { 

       Intent one=new Intent(YourActivity.this,Activity_two.class); 
       startActivity(one); 
       break; 
      } 
      case 2: { 
       Intent two = new Intent(YourActivity.this, Activity_three.class); 
       startActivity(two); 
       break; 
      } 
} 
} 

你可以爲每個項目做到這一點,但如果你想通過相同的意圖所有沒有必要作出方法查找位置,只是通過對項目的點擊意圖。

+0

另外確保爲你的listview設置適配器。 – Garry

+0

它不起作用。 – Alex

相關問題