2013-07-15 16 views
0

这是我的主要代码如何使用一类为多ListView项点击次数

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.layout_of_button); 
ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton1); 
ImageButton btn2 = (ImageButton)findViewById(R.id.imageButton2); 
ImageButton btn3 = (ImageButton)findViewById(R.id.imageButton3); 
ImageButton btn4 = (ImageButton)findViewById(R.id.imageButton4); 
ImageButton btn5 = (ImageButton)findViewById(R.id.imageButton5); 
ImageButton btn6 = (ImageButton)findViewById(R.id.imageButton6); 
btn1.setOnClickListener(this); 
btn2.setOnClickListener(this); 
btn3.setOnClickListener(this); 
btn4.setOnClickListener(this); 
btn5.setOnClickListener(this); 
btn6.setOnClickListener(this); 
} 


    @Override 
    public void onClick(View v) { 
    switch(v.getId()) { 
    // if one of the image buttons is pressed... 
    case R.id.imageButton1: 
    case R.id.imageButton2: 
    case R.id.imageButton3: 
    case R.id.imageButton4: 
    case R.id.imageButton5: 
    case R.id.imageButton6: 
     Intent intent = new Intent(this, Listviewact.class); 
     // pass ID of pressed button to listview-activity 
     intent.putExtra("buttonId", v.getId()); 
     startActivity(intent); 
     break; 
    // here you could place handling of other clicks if necessary...   
    } 
} 

private void setListAdapter(ArrayAdapter<String> arrayAdapter) { 
// TODO Auto-generated method stub 

} 

private ListView getListView() { 
// TODO Auto-generated method stub 
return null; 
} 
} 

这是我的列表视图代码。

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 

    public class Listviewact extends Activity { 

public void onCreate(Bundle b) { 
    super.onCreate(b); 
    setContentView(R.layout.listview_layout); 

    Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/AlexBrush-Regular-OTF.otf"); 
    TextView tv = (TextView) findViewById(R.id.textView1); 
    tv.setTypeface(tf); 
} 

public void onResume() { 
    super.onResume(); 
    int buttonId = getIntent().getIntExtra("buttonId", 0); 
    int buttonIdx = getButtonIdx(buttonId); 

    // find and set image according to buttonId 
    int imageId = IMAGE_IDS[buttonIdx];  // image to show for given button 
    ImageView imageView = (ImageView)findViewById(R.id.imageView1); 
    imageView.setImageResource(imageId); 

    // find and set listview imtes according to buttonId 
    String[] items = LISTVIEW_DATA[buttonIdx]; // listview items to show for given button 
    ListView listView = (ListView)findViewById(R.id.listView1); 
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items); 
    listView.setAdapter(adapter); 

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

      if(position == 0) 
      { 
      //code specific to first list item  
        Intent myIntent = new Intent(view.getContext(), Information.class); 
         startActivityForResult(myIntent, 0); 
      } 

      if(position == 1) 
      { 
      //code specific to 2nd list item  
        Intent myIntent = new Intent(view.getContext(), Information.class); 
         startActivityForResult(myIntent, 0); 
      } 
       } 
       }); 
      // When clicked, show a toast with the TextView text 
      //Or do whatever you need. 
} 


     public void onItemClick1(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 

     } 




private void setListAdapter(ArrayAdapter adapter) { 
    // TODO Auto-generated method stub 

} 

// a little helper to map ids to array indices 
// to be able to fetch the correct image and listview data later 
private final static int[] BUTTON_IDS = new int[] { 
    R.id.imageButton1, 
    R.id.imageButton2, 
    R.id.imageButton3, 
    R.id.imageButton4, 
    R.id.imageButton5, 
    R.id.imageButton6 
}; 

// 6 images 
private final static int[] IMAGE_IDS = new int[] { 
    R.drawable.1, 
    R.drawable.2, 
    R.drawable.3, 
    R.drawable.4, 
    R.drawable.5, 
    R.drawable.6 
}; 

// 6 different sets of strings for the listviews 
private final static String[][] LISTVIEW_DATA = new String[][] { 
    {"First A", "First B", "First C", "First D","First E","First F"}, 
    {"Second A", "Second B", "Second C"}, 
    {"Third A", "Third B", "Third C"}, 
    {"Forth A", "Forth B", "Forth C"}, 
    {"Fifth A", "Fifth B", "Fifth C"}, 
    {"Sixth A", "Sixth B", "Sixth C"}, 
}; 

    // map button id to array index 
    static private int getButtonIdx(int id) { 
    for(int i = 0; i<BUTTON_IDS.length; i++) { 
     if (BUTTON_IDS[i] == id) return i; 
    } 
    return 0; // should not happen 
} 
} 

我想什么做的,对此我很难发现它是当任何我的列表视图被点击的他们都去到相同的布局,但有不同的数据/ infromation这一形象是展示你的最好方式家伙。​​。我在想,如果我可以有一个可以重复使用的课程,而不是像50个活动那样放慢速度会很好。

图片是我想要做的。如果可能的话,在酒店一侧有文字:PETS,VIEWS,PARKING,CHILDREN。然后我想在左侧打电话给reslut 的不同信息。没有宠物停车YES等

+0

我为我正在进行的一个项目做了一件完全如此的事情。我现在不能写一个完整的答案,但创建一个Model类,它包含一个带有getters和setter的'List '。然后创建一个Adapter类,它在ListView中显示Model类。使用一种布局,但创建不同的使用模型的单独活动。 –

回答

0

那么如果你的layout是相同的,那么你可以使用相同的Activity,但那么代码可能会变得复杂。

ListView项是Google Maps Places的情况下动态生成的,还是由您事先决定的选项?如果情况是前者,那么你必须从数据库中获取数据,因为你不能为所有可能性编码,如果是后者,你也可以明确编码。

后,如果您使用的数据库的下一个页面上,然后从一个SQLite database获取数据,或者如果它的每个数据后一种情况下使用Intent.getExtra(data)并指定这些值的Textfields

如果我理解你的问题,这应该工作。

+0

这听起来就是在后面..“是ListView项目动态生成”对不起,我不明白这一点。 –

+0

我的意思是基于查询生成的listview项目?就像我在纽约查询餐馆的情况一样,我得到一个动态列表,当我在芝加哥查询他们时,我会得到另一个列表。这些是动态列表。 –

+0

您也可以使用仅显示预定义项目的布局,如“您的手机操作系统”显示1. iOS 2. Android 3. Windows 8,它们不是动态的。如果它可以帮助请upvote和标记回答:) –