2015-10-08 3 views
0

У меня есть две кнопки внизу i.e после завершения списка.Событие нажатия кнопки с просмотром пользовательского списка

Я использую пользовательский список, который отображает элементы списка с альтернативными цветами.

как установить setOnClickListener как для кнопки в нижней части?

public class PieMainActivity extends Activity { 

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

    mRes = getResources(); 
    new Random(new Date().getTime()); 

    solbtn = (Button)findViewById(R.id.solution); 

    String recive ; 

    Bundle b = getIntent().getExtras(); 
    final int piewrong=b.getInt("piewrong"); 
    final int pieright=b.getInt("pieright"); 
    final int pieunclick=b.getInt("pieunclick"); 

    setContentView(R.layout.piechart_result); 


    recive = pieright+"/20"; 
    mPie.setUnit(recive); 



    data1 = new ArrayList<String>(); 
    fillData() ; 

    listtotal =getIntent().getStringArrayListExtra("listtotal"); 
    timeuse =getIntent().getStringArrayListExtra("timeused"); 

    adapter1 = new ListAdapter(this, data1, listtotal,timeuse); 
    ListView lvMain = (ListView) findViewById(R.id.list); 
    lvMain.setAdapter(adapter1); 


    lvMain.setOnItemClickListener(new OnItemClickListener() { 



     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 
      int questionno = position+1; 

       Bundle b = new Bundle();     
       b.putInt("questionno",questionno); 
       b.putInt("piewrong",piewrong); 
       b.putInt("pieright",pieright); 
       b.putInt("pieunclick",pieunclick); 
       Intent in=new Intent(PieMainActivity.this,Solution.class); 
       in.putStringArrayListExtra("listtotal", (ArrayList<String>) listtotal); 
       in.putStringArrayListExtra("timeused", (ArrayList<String>) timeuse); 
       in.putExtras(b); 
       startActivity(in); 
       finish(); 

     } 
    }); 


} 
void fillData() { 
    for (int i = 1; i <= 20; i++) { 
     data1.add("Question " + i); 
    } 
} 
void fillcmp() { 
    for (int i = 1; i <= 20; i++) { 
     cmp.add("cmp " + i); 
    } 
} 
} 

XML файл

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res/com.staritsolutions.apptitude" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_horizontal" 
android:orientation="vertical" > 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_weight="1.0" 

android:orientation="vertical" > 
<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="5dp" 
      android:layout_weight="1.0"> 
<ListView 
    android:id="@+id/list" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
     > 
     </ListView> 


    </LinearLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="5dp" 
      android:orientation="horizontal" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp"> 

     <Button 
     android:id="@+id/home" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="bottom|center_horizontal" 
     android:layout_marginLeft="6dp" 
     android:layout_marginRight="3dp" 
     android:textSize="12dp" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:gravity="center" 

     android:text="Main Menu" 
     android:background="@drawable/btn_blue" 
     android:textColor="#fff" 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/solution" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="bottom|center_horizontal" 
     android:layout_marginLeft="6dp" 
     android:layout_marginRight="3dp" 
     android:textSize="12dp" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:gravity="center" 

     android:text="Solutions" 
     android:background="@drawable/btn_blue" 
     android:textColor="#fff" 
     android:textStyle="bold" /> 
</LinearLayout> 

</LinearLayout> 

BaseAdapter файл

public class ListAdapter extends BaseAdapter{ 
Context ctx; 
LayoutInflater lInflater; 
List<String> data1; 
List<String> cmp; 
List<String> timeused; 

ListAdapter(Context context, List<String> data1 ,List<String> cmp ,List<String> timeused) { 
    ctx = context; 
    this.data1 = data1; 
    this.cmp = cmp; 
    this.timeused = timeused; 
    lInflater = (LayoutInflater) ctx 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 

    return data1.size(); 
} 
public int getCount1() { 
    return cmp.size(); 
} 


@Override 
public Object getItem(int position) { 
    return data1.get(position); 
} 
public Object getItem1(int position1) { 
    return cmp.get(position1); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 
public long getItemId1(int position1) { 
    return position1; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     view = lInflater.inflate(R.layout.listitem, parent, false); 
    } 
    /*if (position % 2 == 0) { 
     view.setBackgroundResource(R.drawable.artists_list_backgroundcolor); 
    } else { 
     view.setBackgroundResource(R.drawable.artists_list_background_alternate); 
    }*/ 
    TextView text = (TextView) view.findViewById(R.id.heading); 
    int no = position; 
    if(cmp.get(position).equals("GREEN")) 
    { 
     text.setTextColor(Color.parseColor("#00D50E")); 
    }else if(cmp.get(position).equals("RED")) 
    { 
     text.setTextColor(Color.parseColor("#e84040")); 
    }else 
    { 
     text.setTextColor(Color.parseColor("#B441E9")); 
    } 

    ((TextView) view.findViewById(R.id.heading)).setText(data1.get(position)); 
    ((TextView) view.findViewById(R.id.duration)).setText(timeused.get(position)); 
    return view; 
    //String.valueOf(value); 
} 
} 
+0

где 2 кнопки? В основном xml или в пользовательском адаптере? Можете ли вы разместить piechart_result xml и свой собственный адаптер? – Ivan

ответ

0

Таким образом, эти кнопки не являются частью вашего ListView? Ну, чем в вашем onCreate сделать что-то вроде:

solbtn = (Button)findViewById(R.id.solution); 
solbth.setOnClickListener(toolbar.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      yourMethod(); 
     } 
    }); 

И то же самое касается второй кнопки.

Или вы можете сделать свою активность реализовать View.OnClickListener, после выполнения этого вы можете установить клики слушатель, как это:

//this two lines in your OnCreate 
button1.setOnClickListener(this); 
button2.setOnClickListener(this); 

//somewhere later in the code 

@Override 
public void onClick(View v) { 
    //do something 
} 
+0

кнопки не входят в список просмотров – Vishal

+0

Ну, тогда попробуйте код, который я предложил, и дайте мне знать, если это сработает! – Kistamushken

0

Просто добавьте clickListeners в методе onCrete():

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

    ..... 

    solbtn = (Button) findViewById(R.id.home); 
    homebtn = (Button) findViewById(R.id.solution); 

    solbtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       // do stuff for solution button 
      } 
    }); 

    homebtn.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       // do stuff for home button 
      } 
    }); 

    ..... 
} 
Смежные вопросы